@dxos/async 0.8.4-main.f9ba587 → 0.8.4-main.fd6878d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/lib/browser/index.mjs +177 -166
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +177 -166
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/callback.d.ts +2 -1
  8. package/dist/types/src/callback.d.ts.map +1 -1
  9. package/dist/types/src/chain.d.ts +1 -1
  10. package/dist/types/src/chain.d.ts.map +1 -1
  11. package/dist/types/src/cleanup.d.ts +1 -1
  12. package/dist/types/src/cleanup.d.ts.map +1 -1
  13. package/dist/types/src/debounce.d.ts +16 -2
  14. package/dist/types/src/debounce.d.ts.map +1 -1
  15. package/dist/types/src/index.d.ts +0 -5
  16. package/dist/types/src/index.d.ts.map +1 -1
  17. package/dist/types/src/task-scheduling.d.ts.map +1 -1
  18. package/dist/types/src/testing.d.ts +13 -0
  19. package/dist/types/src/testing.d.ts.map +1 -1
  20. package/dist/types/src/timeout.d.ts +1 -1
  21. package/dist/types/src/timeout.d.ts.map +1 -1
  22. package/dist/types/src/trigger.d.ts +11 -0
  23. package/dist/types/src/trigger.d.ts.map +1 -1
  24. package/dist/types/tsconfig.tsbuildinfo +1 -1
  25. package/package.json +9 -9
  26. package/src/callback.ts +3 -3
  27. package/src/chain.ts +1 -1
  28. package/src/cleanup.ts +3 -3
  29. package/src/debounce.test.ts +69 -12
  30. package/src/debounce.ts +32 -11
  31. package/src/event-emitter.test.ts +2 -1
  32. package/src/index.ts +0 -5
  33. package/src/observable-value.test.ts +1 -1
  34. package/src/persistent-lifecycle.test.ts +1 -1
  35. package/src/task-scheduling.ts +1 -1
  36. package/src/testing.test.ts +41 -1
  37. package/src/testing.ts +53 -0
  38. package/src/timeout.ts +23 -22
  39. package/src/trigger.ts +58 -1
  40. package/src/update-scheduler.ts +1 -1
  41. package/dist/types/src/latch.d.ts +0 -11
  42. package/dist/types/src/latch.d.ts.map +0 -1
  43. package/dist/types/src/sink.d.ts +0 -6
  44. package/dist/types/src/sink.d.ts.map +0 -1
  45. package/dist/types/src/throttle.d.ts +0 -2
  46. package/dist/types/src/throttle.d.ts.map +0 -1
  47. package/dist/types/src/throttle.test.d.ts +0 -2
  48. package/dist/types/src/throttle.test.d.ts.map +0 -1
  49. package/dist/types/src/types.d.ts +0 -2
  50. package/dist/types/src/types.d.ts.map +0 -1
  51. package/dist/types/src/until.d.ts +0 -14
  52. package/dist/types/src/until.d.ts.map +0 -1
  53. package/dist/types/src/until.test.d.ts +0 -2
  54. package/dist/types/src/until.test.d.ts.map +0 -1
  55. package/src/latch.ts +0 -60
  56. package/src/sink.ts +0 -26
  57. package/src/throttle.test.ts +0 -65
  58. package/src/throttle.ts +0 -14
  59. package/src/types.ts +0 -5
  60. package/src/until.test.ts +0 -47
  61. package/src/until.ts +0 -58
@@ -1,14 +0,0 @@
1
- export type UntilCallback<T> = (resolve: (value: T) => void, reject: (error: Error) => void) => Promise<T> | void;
2
- /**
3
- * Awaits promise.
4
- */
5
- export declare const until: <T = void>(cb: UntilCallback<T>, timeout?: number) => Promise<T>;
6
- /**
7
- * Wait until promise resolves.
8
- */
9
- export declare const untilPromise: <T = void>(cb: () => Promise<T>) => Promise<T>;
10
- /**
11
- * Wait until error is thrown.
12
- */
13
- export declare const untilError: (cb: () => Promise<any>) => Promise<unknown>;
14
- //# sourceMappingURL=until.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"until.d.ts","sourceRoot":"","sources":["../../../src/until.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;AAElH;;GAEG;AAEH,eAAO,MAAM,KAAK,GAAI,CAAC,GAAG,IAAI,EAAE,IAAI,aAAa,CAAC,CAAC,CAAC,EAAE,UAAU,MAAM,KAAG,OAAO,CAAC,CAAC,CAyBjF,CAAC;AAEF;;GAEG;AAEH,eAAO,MAAM,YAAY,GAAI,CAAC,GAAG,IAAI,EAAE,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,eAAS,CAAC;AAErE;;GAEG;AACH,eAAO,MAAM,UAAU,GAAI,IAAI,MAAM,OAAO,CAAC,GAAG,CAAC,qBAWhD,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=until.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"until.test.d.ts","sourceRoot":"","sources":["../../../src/until.test.ts"],"names":[],"mappings":""}
package/src/latch.ts DELETED
@@ -1,60 +0,0 @@
1
- //
2
- // Copyright 2020 DXOS.org
3
- //
4
-
5
- import { invariant } from '@dxos/invariant';
6
-
7
- type LatchProps = {
8
- count?: number;
9
- timeout?: number;
10
- };
11
-
12
- type LatchResult = [() => Promise<number>, () => number, (err: Error) => void];
13
-
14
- /**
15
- * Returns a callback and a promise that's resolved when the callback is called n times.
16
- */
17
- // TODO(burdon): Reconcile with until/trigger.
18
- export const latch = ({ count = 1, timeout }: LatchProps = {}): LatchResult => {
19
- invariant(count >= 0);
20
-
21
- let t: ReturnType<typeof setTimeout>;
22
- let doResolve: (value: number) => void;
23
- let doReject: (err: Error) => void;
24
- const promise = new Promise<number>((resolve, reject) => {
25
- doResolve = (value) => {
26
- clearTimeout(t);
27
- resolve(value);
28
- };
29
-
30
- doReject = (err) => {
31
- clearTimeout(t);
32
- reject(err);
33
- };
34
- });
35
-
36
- if (count === 0) {
37
- setTimeout(() => {
38
- doResolve(0);
39
- });
40
- } else {
41
- if (timeout) {
42
- t = setTimeout(() => {
43
- doReject(new Error(`Timed out after ${timeout.toLocaleString()}ms`));
44
- }, timeout);
45
- }
46
- }
47
-
48
- let i = 0;
49
- return [
50
- async () => await promise,
51
- () => {
52
- if (++i === count) {
53
- doResolve(i);
54
- }
55
-
56
- return i;
57
- },
58
- (err: Error) => doReject(err),
59
- ];
60
- };
package/src/sink.ts DELETED
@@ -1,26 +0,0 @@
1
- //
2
- // Copyright 2020 DXOS.org
3
- //
4
-
5
- import { type EventEmitter } from 'node:events';
6
-
7
- import { trigger } from './trigger';
8
-
9
- /**
10
- * Waits for the specified number of events from the given emitter.
11
- */
12
- export const sink = (emitter: EventEmitter, event: string, count = 1): Promise<void> => {
13
- const [getPromise, resolve] = trigger();
14
-
15
- let counter = 0;
16
- const listener = () => {
17
- if (++counter === count) {
18
- emitter.off(event, listener);
19
- resolve();
20
- }
21
- };
22
-
23
- emitter.on(event, listener);
24
-
25
- return getPromise();
26
- };
@@ -1,65 +0,0 @@
1
- //
2
- // Copyright 2020 DXOS.org
3
- //
4
-
5
- import { describe, expect, test } from 'vitest';
6
-
7
- import { throttle } from './throttle';
8
- import { sleep } from './timeout';
9
-
10
- describe('throttle', () => {
11
- test('throttles function calls', async () => {
12
- let count = 0;
13
- const fn = throttle(() => count++, 100);
14
-
15
- // First call should execute immediately
16
- fn();
17
- expect(count).toBe(1);
18
-
19
- // Second call within throttle window should not execute
20
- fn();
21
- expect(count).toBe(1);
22
-
23
- // Wait for throttle window to pass
24
- await sleep(150);
25
-
26
- // Next call should execute
27
- fn();
28
- expect(count).toBe(2);
29
- });
30
-
31
- test('passes arguments to throttled function', async () => {
32
- let lastArgs: any[] = [];
33
- const fn = throttle((...args: any[]) => {
34
- lastArgs = args;
35
- }, 100);
36
-
37
- fn('test', 123);
38
- expect(lastArgs).toEqual(['test', 123]);
39
-
40
- // Call with different args within throttle window
41
- fn('different', 456);
42
- expect(lastArgs).toEqual(['test', 123]); // Should not update
43
-
44
- await sleep(150);
45
- fn('new', 789);
46
- expect(lastArgs).toEqual(['new', 789]);
47
- });
48
-
49
- test('handles multiple rapid calls', async () => {
50
- let count = 0;
51
- const fn = throttle(() => count++, 100);
52
-
53
- // Make multiple rapid calls
54
- for (let i = 0; i < 5; i++) {
55
- fn();
56
- }
57
- expect(count).toBe(1); // Only first call should execute
58
-
59
- await sleep(150);
60
- expect(count).toBe(1); // Still only one execution
61
-
62
- fn();
63
- expect(count).toBe(2); // Next call after wait should execute
64
- });
65
- });
package/src/throttle.ts DELETED
@@ -1,14 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export const throttle = (cb: (...args: any[]) => void, wait = 100): ((...args: any[]) => void) => {
6
- let lastCall = 0;
7
- return (...args: any[]) => {
8
- const now = Date.now();
9
- if (now - lastCall >= wait) {
10
- cb(...args);
11
- lastCall = now;
12
- }
13
- };
14
- };
package/src/types.ts DELETED
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2021 DXOS.org
3
- //
4
-
5
- export type Awaited<T> = T extends Promise<infer U> ? U : T;
package/src/until.test.ts DELETED
@@ -1,47 +0,0 @@
1
- //
2
- // Copyright 2020 DXOS.org
3
- //
4
-
5
- import { describe, expect, test } from 'vitest';
6
-
7
- import { sleep } from './timeout';
8
- import { until } from './until';
9
-
10
- describe('until', () => {
11
- test('success', async () => {
12
- const value = await until<number>(async (resolve) => {
13
- await sleep(100);
14
- resolve(100);
15
- return 1;
16
- });
17
-
18
- expect(value).to.equal(100);
19
- });
20
-
21
- test('error', async () => {
22
- await expect(async () => {
23
- await until(async (resolve, reject) => {
24
- await sleep(100);
25
- reject(new Error());
26
- });
27
- }).rejects.toThrowError();
28
- });
29
-
30
- test('catch', async () => {
31
- await expect(async () => {
32
- await until(async () => {
33
- await sleep(100);
34
- throw new Error();
35
- });
36
- }).rejects.toThrowError();
37
- });
38
-
39
- test('timeout', async () => {
40
- await expect(async () => {
41
- await until(async (resolve) => {
42
- await sleep(500);
43
- resolve();
44
- }, 100); // Timeout before complete.
45
- }).rejects.toThrowError();
46
- });
47
- });
package/src/until.ts DELETED
@@ -1,58 +0,0 @@
1
- //
2
- // Copyright 2022 DXOS.org
3
- //
4
-
5
- export type UntilCallback<T> = (resolve: (value: T) => void, reject: (error: Error) => void) => Promise<T> | void;
6
-
7
- /**
8
- * Awaits promise.
9
- */
10
- // TODO(burdon): Reconcile with latch/trigger.
11
- export const until = <T = void>(cb: UntilCallback<T>, timeout?: number): Promise<T> => {
12
- return new Promise((resolve, reject) => {
13
- const t =
14
- timeout &&
15
- setTimeout(() => {
16
- reject(new Error(`Timeout after ${t}ms`));
17
- }, timeout);
18
-
19
- setTimeout(async () => {
20
- try {
21
- await cb(
22
- (value: T) => {
23
- t && clearTimeout(t);
24
- resolve(value);
25
- },
26
- (error: Error) => {
27
- t && clearTimeout(t);
28
- reject(error);
29
- },
30
- );
31
- } catch (err) {
32
- reject(err);
33
- }
34
- });
35
- });
36
- };
37
-
38
- /**
39
- * Wait until promise resolves.
40
- */
41
- // TODO(burdon): Reconcile promises (with timeouts).
42
- export const untilPromise = <T = void>(cb: () => Promise<T>) => cb();
43
-
44
- /**
45
- * Wait until error is thrown.
46
- */
47
- export const untilError = (cb: () => Promise<any>) => {
48
- return new Promise((resolve, reject) => {
49
- setTimeout(async () => {
50
- try {
51
- await cb();
52
- reject(new Error('No error was thrown.'));
53
- } catch (err) {
54
- resolve(err);
55
- }
56
- });
57
- });
58
- };