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

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 (65) hide show
  1. package/dist/lib/browser/index.mjs +297 -196
  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 +297 -196
  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 +2 -1
  12. package/dist/types/src/cleanup.d.ts.map +1 -1
  13. package/dist/types/src/debounce.d.ts +31 -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/persistent-lifecycle.d.ts +2 -2
  18. package/dist/types/src/persistent-lifecycle.d.ts.map +1 -1
  19. package/dist/types/src/task-scheduling.d.ts +28 -0
  20. package/dist/types/src/task-scheduling.d.ts.map +1 -1
  21. package/dist/types/src/testing.d.ts +13 -0
  22. package/dist/types/src/testing.d.ts.map +1 -1
  23. package/dist/types/src/timeout.d.ts +1 -1
  24. package/dist/types/src/timeout.d.ts.map +1 -1
  25. package/dist/types/src/trigger.d.ts +11 -0
  26. package/dist/types/src/trigger.d.ts.map +1 -1
  27. package/dist/types/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +13 -9
  29. package/src/callback.ts +3 -3
  30. package/src/chain.ts +1 -1
  31. package/src/cleanup.ts +4 -3
  32. package/src/debounce.test.ts +69 -12
  33. package/src/debounce.ts +82 -13
  34. package/src/event-emitter.test.ts +2 -1
  35. package/src/index.ts +0 -5
  36. package/src/observable-value.test.ts +1 -1
  37. package/src/persistent-lifecycle.test.ts +1 -1
  38. package/src/persistent-lifecycle.ts +2 -2
  39. package/src/task-scheduling.ts +94 -1
  40. package/src/testing.test.ts +41 -1
  41. package/src/testing.ts +53 -0
  42. package/src/timeout.ts +23 -22
  43. package/src/trigger.ts +58 -1
  44. package/src/update-scheduler.ts +1 -1
  45. package/dist/types/src/latch.d.ts +0 -11
  46. package/dist/types/src/latch.d.ts.map +0 -1
  47. package/dist/types/src/sink.d.ts +0 -6
  48. package/dist/types/src/sink.d.ts.map +0 -1
  49. package/dist/types/src/throttle.d.ts +0 -2
  50. package/dist/types/src/throttle.d.ts.map +0 -1
  51. package/dist/types/src/throttle.test.d.ts +0 -2
  52. package/dist/types/src/throttle.test.d.ts.map +0 -1
  53. package/dist/types/src/types.d.ts +0 -2
  54. package/dist/types/src/types.d.ts.map +0 -1
  55. package/dist/types/src/until.d.ts +0 -14
  56. package/dist/types/src/until.d.ts.map +0 -1
  57. package/dist/types/src/until.test.d.ts +0 -2
  58. package/dist/types/src/until.test.d.ts.map +0 -1
  59. package/src/latch.ts +0 -60
  60. package/src/sink.ts +0 -26
  61. package/src/throttle.test.ts +0 -65
  62. package/src/throttle.ts +0 -14
  63. package/src/types.ts +0 -5
  64. package/src/until.test.ts +0 -47
  65. package/src/until.ts +0 -58
package/package.json CHANGED
@@ -1,21 +1,25 @@
1
1
  {
2
2
  "name": "@dxos/async",
3
- "version": "0.8.4-main.f9ba587",
3
+ "version": "0.8.4-main.fbb7a13",
4
4
  "description": "Async utilities.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
13
  "sideEffects": true,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
17
+ "types": "./dist/types/src/index.d.ts",
13
18
  "browser": "./dist/lib/browser/index.mjs",
14
19
  "node": {
15
20
  "require": "./dist/lib/node/index.cjs",
16
21
  "default": "./dist/lib/node-esm/index.mjs"
17
- },
18
- "types": "./dist/types/src/index.d.ts"
22
+ }
19
23
  }
20
24
  },
21
25
  "types": "dist/types/src/index.d.ts",
@@ -29,12 +33,12 @@
29
33
  "dependencies": {
30
34
  "zen-observable": "^0.10.0",
31
35
  "zen-push": "^0.3.1",
32
- "@dxos/context": "0.8.4-main.f9ba587",
33
- "@dxos/debug": "0.8.4-main.f9ba587",
34
- "@dxos/invariant": "0.8.4-main.f9ba587",
35
- "@dxos/log": "0.8.4-main.f9ba587",
36
- "@dxos/node-std": "0.8.4-main.f9ba587",
37
- "@dxos/util": "0.8.4-main.f9ba587"
36
+ "@dxos/context": "0.8.4-main.fbb7a13",
37
+ "@dxos/debug": "0.8.4-main.fbb7a13",
38
+ "@dxos/invariant": "0.8.4-main.fbb7a13",
39
+ "@dxos/log": "0.8.4-main.fbb7a13",
40
+ "@dxos/util": "0.8.4-main.fbb7a13",
41
+ "@dxos/node-std": "0.8.4-main.fbb7a13"
38
42
  },
39
43
  "devDependencies": {
40
44
  "@types/zen-observable": "^0.8.3"
package/src/callback.ts CHANGED
@@ -2,12 +2,12 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
+ export type Awaited<T> = T extends Promise<infer U> ? U : T;
6
+
5
7
  /**
6
8
  * Helper to convert a callback based API into a promise based API.
7
9
  */
8
- export const createPromiseFromCallback = <T = void>(
9
- run: (cb: (error?: Error, value?: T) => void) => void,
10
- ): Promise<T> =>
10
+ export const promiseFromCallback = <T = void>(run: (cb: (error?: Error, value?: T) => void) => void): Promise<T> =>
11
11
  new Promise((resolve, reject) => {
12
12
  run((error, value) => {
13
13
  if (error) {
package/src/chain.ts CHANGED
@@ -7,7 +7,7 @@ type Transform = (...args: any) => Promise<any>;
7
7
  /**
8
8
  * Async reducer iteratively applies functions to the given array of elements.
9
9
  */
10
- export const asyncChain =
10
+ export const chain =
11
11
  <T>(chain: Transform[]) =>
12
12
  async (elements: Promise<T[]>) => {
13
13
  let result = await elements;
package/src/cleanup.ts CHANGED
@@ -37,16 +37,17 @@ type EventMap<T> = T extends Window
37
37
  /**
38
38
  * Add the event listener and return a cleanup function.
39
39
  * Can be used in effect hooks in conjunction with `combine`.
40
+ * @deprecated use bind-event-listener
40
41
  */
41
- export function addEventListener<T extends EventTarget, K extends keyof EventMap<T>>(
42
+ export const addEventListener = <T extends EventTarget, K extends keyof EventMap<T>>(
42
43
  target: T,
43
44
  type: K,
44
45
  listener: (this: T, ev: EventMap<T>[K]) => any,
45
46
  options?: boolean | AddEventListenerOptions,
46
- ): CleanupFn {
47
+ ): CleanupFn => {
47
48
  target.addEventListener(type as string, listener as EventListener, options);
48
49
  return () => target.removeEventListener(type as string, listener as EventListener, options);
49
- }
50
+ };
50
51
 
51
52
  export class SubscriptionList {
52
53
  private readonly _cleanups: CleanupFn[] = [];
@@ -4,25 +4,82 @@
4
4
 
5
5
  import { describe, expect, test } from 'vitest';
6
6
 
7
- import { debounce } from './debounce';
7
+ import { debounce, throttle } from './debounce';
8
8
  import { sleep } from './timeout';
9
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
+ });
66
+
10
67
  describe('debounce', () => {
11
68
  test('debounces function calls', async () => {
12
69
  let count = 0;
13
70
  const fn = debounce(() => count++, 100);
14
71
 
15
- // First call should not execute immediately
72
+ // First call should not execute immediately.
16
73
  fn();
17
74
  expect(count).toBe(0);
18
75
 
19
- // Second call should reset the timer
76
+ // Second call should reset the timer.
20
77
  fn();
21
78
  expect(count).toBe(0);
22
79
 
23
- // Wait for debounce window to pass
80
+ // Wait for debounce window to pass.
24
81
  await sleep(150);
25
- expect(count).toBe(1); // Only the last call should execute
82
+ expect(count).toBe(1); // Only the last call should execute.
26
83
  });
27
84
 
28
85
  test('passes arguments to debounced function', async () => {
@@ -32,27 +89,27 @@ describe('debounce', () => {
32
89
  }, 100);
33
90
 
34
91
  fn('test', 123);
35
- expect(lastArgs).toEqual([]); // Should not execute immediately
92
+ expect(lastArgs).toEqual([]); // Should not execute immediately.
36
93
 
37
- // Call with different args
94
+ // Call with different args.
38
95
  fn('different', 456);
39
- expect(lastArgs).toEqual([]); // Should not execute immediately
96
+ expect(lastArgs).toEqual([]); // Should not execute immediately.
40
97
 
41
98
  await sleep(150);
42
- expect(lastArgs).toEqual(['different', 456]); // Should execute with last args
99
+ expect(lastArgs).toEqual(['different', 456]); // Should execute with last args.
43
100
  });
44
101
 
45
102
  test('handles multiple rapid calls', async () => {
46
103
  let count = 0;
47
104
  const fn = debounce(() => count++, 100);
48
105
 
49
- // Make multiple rapid calls
106
+ // Make multiple rapid calls.
50
107
  for (let i = 0; i < 5; i++) {
51
108
  fn();
52
109
  }
53
- expect(count).toBe(0); // Should not execute immediately
110
+ expect(count).toBe(0); // Should not execute immediately.
54
111
 
55
112
  await sleep(150);
56
- expect(count).toBe(1); // Should execute only once after wait
113
+ expect(count).toBe(1); // Should execute only once after wait.
57
114
  });
58
115
  });
package/src/debounce.ts CHANGED
@@ -2,28 +2,97 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { throttle } from './throttle';
5
+ type Callback = (...args: any[]) => void;
6
+
7
+ /**
8
+ * Delay callback execution by a specified time.
9
+ * Unlike debounce, subsequent calls during the delay period are ignored.
10
+ *
11
+ * @param cb Callback to invoke.
12
+ * @param delay Time to wait before invoking the callback.
13
+ * @returns A new function that schedules the callback once and ignores subsequent calls until executed.
14
+ */
15
+ export const delay = <CB extends Callback>(cb: CB, delay = 100): CB => {
16
+ let pending = false;
17
+ return ((...args: any[]) => {
18
+ if (pending) {
19
+ return;
20
+ }
21
+
22
+ pending = true;
23
+ setTimeout(() => {
24
+ try {
25
+ cb(...args);
26
+ } finally {
27
+ pending = false;
28
+ }
29
+ }, delay);
30
+ }) as CB;
31
+ };
6
32
 
7
33
  /**
8
34
  * Debounce callback.
35
+ *
36
+ * @param cb Callback to invoke.
37
+ * @param delay Time window to wait before allowing calls.
38
+ * @returns A new function that wraps the callback and ensures that the callback is only invoked after the time window has passed and no new calls have been made.
9
39
  */
10
- export const debounce = (cb: (...args: any[]) => void, wait = 100): ((...args: any[]) => void) => {
40
+ export const debounce = <CB extends Callback>(cb: CB, delay = 100): CB => {
11
41
  let t: ReturnType<typeof setTimeout>;
12
- return (...args: any[]) => {
42
+ return ((...args: any[]) => {
13
43
  clearTimeout(t);
14
- t = setTimeout(() => cb(...args), wait);
15
- };
44
+ t = setTimeout(() => cb(...args), delay);
45
+ }) as CB;
46
+ };
47
+
48
+ /**
49
+ * Throttle callback.
50
+ *
51
+ * @param cb Callback to invoke.
52
+ * @param delay Time window to allow calls in.
53
+ * @returns A new function that wraps the callback and prevents multiple invocations within the time window.
54
+ */
55
+ export const throttle = <CB extends Callback>(cb: CB, delay = 100): CB => {
56
+ let lastCall = 0;
57
+ return ((...args: any[]) => {
58
+ const now = Date.now();
59
+ if (now - lastCall >= delay) {
60
+ cb(...args);
61
+ lastCall = now;
62
+ }
63
+ }) as CB;
16
64
  };
17
65
 
18
66
  /**
19
67
  * Debounce and throttle callback.
68
+ * Executes immediately on the first call (throttle), prevents calls during the throttle window,
69
+ * and ensures a final call happens after activity stops (debounce).
70
+ *
71
+ * @param cb Callback to invoke.
72
+ * @param delay Time window for both throttle and debounce.
73
+ * @returns A new function that combines throttle and debounce behavior.
20
74
  */
21
- export const debounceAndThrottle = (cb: (...args: any[]) => void, wait = 100): ((...args: any[]) => void) => {
22
- const debounced = debounce(cb, wait);
23
- const throttled = throttle(cb, wait);
24
-
25
- return (...args: any[]) => {
26
- debounced(...args);
27
- throttled(...args);
28
- };
75
+ export const debounceAndThrottle = <CB extends Callback>(cb: CB, delay = 100): CB => {
76
+ let timeout: ReturnType<typeof setTimeout>;
77
+ let lastCall = 0;
78
+
79
+ return ((...args: any[]) => {
80
+ const now = Date.now();
81
+ const delta = now - lastCall;
82
+
83
+ // Clear any pending debounced call.
84
+ clearTimeout(timeout);
85
+
86
+ // Throttle: execute immediately if enough time has passed.
87
+ if (delta >= delay) {
88
+ cb(...args);
89
+ lastCall = now;
90
+ } else {
91
+ // Debounce: schedule to execute after the remaining time.
92
+ timeout = setTimeout(() => {
93
+ cb(...args);
94
+ lastCall = Date.now();
95
+ }, delay - delta);
96
+ }
97
+ }) as CB;
29
98
  };
@@ -3,11 +3,12 @@
3
3
  //
4
4
 
5
5
  import { EventEmitter } from 'node:events';
6
+
6
7
  import { describe, expect, test } from 'vitest';
7
8
 
8
9
  import { onEvent, waitForEvent } from './event-emitter';
9
- import { latch } from './latch';
10
10
  import { asyncTimeout } from './timeout';
11
+ import { latch } from './trigger';
11
12
 
12
13
  describe('event-emitter', () => {
13
14
  test('onEvent', async () => {
package/src/index.ts CHANGED
@@ -9,22 +9,17 @@ export * from './debounce';
9
9
  export * from './errors';
10
10
  export * from './event-emitter';
11
11
  export * from './events';
12
- export * from './latch';
13
12
  export * from './mutex';
14
13
  export * from './observable';
15
14
  export * from './observable-value';
16
15
  export * from './persistent-lifecycle';
17
16
  export * from './push-iterable';
18
- export * from './sink';
19
17
  export * from './stream-to-array';
20
18
  export * from './task-scheduling';
21
19
  export * from './test-stream';
22
20
  export * from './testing';
23
- export * from './throttle';
24
21
  export * from './timeout';
25
22
  export * from './timer';
26
23
  export * from './track-leaks';
27
24
  export * from './trigger';
28
- export * from './types';
29
- export * from './until';
30
25
  export * from './update-scheduler';
@@ -5,12 +5,12 @@
5
5
  import { describe, expect, test } from 'vitest';
6
6
 
7
7
  import { type AsyncEvents, TimeoutError } from './errors';
8
- import { latch } from './latch';
9
8
  import {
10
9
  type CancellableObservable,
11
10
  type CancellableObservableEvents,
12
11
  CancellableObservableProvider,
13
12
  } from './observable-value';
13
+ import { latch } from './trigger';
14
14
 
15
15
  interface ConnectionEvents extends AsyncEvents, CancellableObservableEvents {
16
16
  onConnected(connectionId: string): void;
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { describe, expect, test, onTestFinished } from 'vitest';
5
+ import { describe, expect, onTestFinished, test } from 'vitest';
6
6
 
7
7
  import { log } from '@dxos/log';
8
8
 
@@ -13,7 +13,7 @@ import { sleep } from './timeout';
13
13
  const INIT_RESTART_DELAY = 100;
14
14
  const DEFAULT_MAX_RESTART_DELAY = 5000;
15
15
 
16
- export type PersistentLifecycleParams<T> = {
16
+ export type PersistentLifecycleProps<T> = {
17
17
  /**
18
18
  * Create connection.
19
19
  * If promise resolves successfully, connection is considered established.
@@ -51,7 +51,7 @@ export class PersistentLifecycle<T> extends Resource {
51
51
  private _restartTask?: DeferredTask = undefined;
52
52
  private _restartAfter = 0;
53
53
 
54
- constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }: PersistentLifecycleParams<T>) {
54
+ constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }: PersistentLifecycleProps<T>) {
55
55
  super();
56
56
  this._start = start;
57
57
  this._stop = stop;
@@ -2,7 +2,7 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { ContextDisposedError, type Context } from '@dxos/context';
5
+ import { type Context, ContextDisposedError } from '@dxos/context';
6
6
  import { StackTrace } from '@dxos/debug';
7
7
  import { type MaybePromise } from '@dxos/util';
8
8
 
@@ -77,6 +77,99 @@ export class DeferredTask {
77
77
  }
78
78
  }
79
79
 
80
+ // TODO(dmaretskyi): Protyping this an alternative API to DeferredTask.
81
+ export class AsyncTask {
82
+ #callback: () => Promise<void>;
83
+ #ctx?: Context = undefined;
84
+
85
+ #scheduled = false;
86
+ #currentTask: Promise<void> | null = null; // Can't be rejected.
87
+ #nextTask = new Trigger();
88
+
89
+ constructor(callback: () => Promise<void>) {
90
+ this.#callback = callback;
91
+ }
92
+
93
+ get scheduled() {
94
+ return this.#scheduled;
95
+ }
96
+
97
+ /**
98
+ * Context of the resource that owns the task.
99
+ * When the context is disposed, the task is cancelled and cannot be scheduled again.
100
+ */
101
+ // TODO(dmaretskyi): We don't really need to pass ctx in here, since close will also signal dispose.
102
+ open(ctx: Context): void {
103
+ this.#ctx = ctx;
104
+ }
105
+
106
+ /**
107
+ * Closes the task and waits for it to finish if it is running.
108
+ */
109
+ async close(): Promise<void> {
110
+ this.#ctx = undefined;
111
+ await this.join();
112
+ }
113
+
114
+ [Symbol.asyncDispose](): Promise<void> {
115
+ return this.close();
116
+ }
117
+
118
+ /**
119
+ * Schedule the task to run asynchronously.
120
+ */
121
+ schedule(): void {
122
+ if (!this.#ctx || this.#ctx.disposed) {
123
+ throw new Error('AsyncTask not open');
124
+ }
125
+
126
+ if (this.#scheduled) {
127
+ return; // Already scheduled.
128
+ }
129
+
130
+ scheduleTask(this.#ctx, async () => {
131
+ // The previous task might still be running, so we need to wait for it to finish.
132
+ await this.#currentTask; // Can't be rejected.
133
+
134
+ if (!this.#ctx || this.#ctx.disposed) {
135
+ return;
136
+ }
137
+
138
+ // Reset the flag. New tasks can now be scheduled. They would wait for the callback to finish.
139
+ this.#scheduled = false;
140
+ const completionTrigger = this.#nextTask;
141
+ this.#nextTask = new Trigger(); // Re-create the trigger as opposed to resetting it since there might be listeners waiting for it.
142
+
143
+ // Store the promise so that new tasks could wait for this one to finish.
144
+ this.#currentTask = runInContextAsync(this.#ctx, () => this.#callback()).then(() => {
145
+ completionTrigger.wake();
146
+ });
147
+ });
148
+
149
+ this.#scheduled = true;
150
+ }
151
+
152
+ /**
153
+ * Schedule the task to run and wait for it to finish.
154
+ */
155
+ async runBlocking(): Promise<void> {
156
+ if (this.#ctx?.disposed) {
157
+ throw new ContextDisposedError();
158
+ }
159
+
160
+ this.schedule();
161
+ await this.#nextTask.wait();
162
+ }
163
+
164
+ /**
165
+ * Waits for the current task to finish if it is running.
166
+ * Does not schedule a new task.
167
+ */
168
+ async join(): Promise<void> {
169
+ await this.#currentTask;
170
+ }
171
+ }
172
+
80
173
  export const runInContext = (ctx: Context, fn: () => void) => {
81
174
  try {
82
175
  fn();
@@ -6,7 +6,8 @@ import { describe, expect, test } from 'vitest';
6
6
 
7
7
  import { expectToThrow, raise } from '@dxos/debug';
8
8
 
9
- import { waitForCondition } from './testing';
9
+ import { until, waitForCondition } from './testing';
10
+ import { sleep } from './timeout';
10
11
 
11
12
  describe('waitForCondition', () => {
12
13
  test('succeeds', async () => {
@@ -27,3 +28,42 @@ describe('waitForCondition', () => {
27
28
  ).rejects.toThrow('test');
28
29
  });
29
30
  });
31
+
32
+ describe('until', () => {
33
+ test('success', async () => {
34
+ const value = await until<number>(async (resolve) => {
35
+ await sleep(100);
36
+ resolve(100);
37
+ return 1;
38
+ });
39
+
40
+ expect(value).to.equal(100);
41
+ });
42
+
43
+ test('error', async () => {
44
+ await expect(async () => {
45
+ await until(async (resolve, reject) => {
46
+ await sleep(100);
47
+ reject(new Error());
48
+ });
49
+ }).rejects.toThrowError();
50
+ });
51
+
52
+ test('catch', async () => {
53
+ await expect(async () => {
54
+ await until(async () => {
55
+ await sleep(100);
56
+ throw new Error();
57
+ });
58
+ }).rejects.toThrowError();
59
+ });
60
+
61
+ test('timeout', async () => {
62
+ await expect(async () => {
63
+ await until(async (resolve) => {
64
+ await sleep(500);
65
+ resolve();
66
+ }, 100); // Timeout before complete.
67
+ }).rejects.toThrowError();
68
+ });
69
+ });
package/src/testing.ts CHANGED
@@ -54,3 +54,56 @@ export const waitForCondition = <FunctionType extends (...args: any) => any>({
54
54
 
55
55
  return trigger.wait({ timeout });
56
56
  };
57
+
58
+ export type UntilCallback<T> = (resolve: (value: T) => void, reject: (error: Error) => void) => Promise<T> | void;
59
+
60
+ /**
61
+ * Awaits promise.
62
+ */
63
+ export const until = <T = void>(cb: UntilCallback<T>, timeout?: number): Promise<T> => {
64
+ return new Promise((resolve, reject) => {
65
+ const t =
66
+ timeout &&
67
+ setTimeout(() => {
68
+ reject(new Error(`Timeout after ${t}ms`));
69
+ }, timeout);
70
+
71
+ setTimeout(async () => {
72
+ try {
73
+ await cb(
74
+ (value: T) => {
75
+ t && clearTimeout(t);
76
+ resolve(value);
77
+ },
78
+ (error: Error) => {
79
+ t && clearTimeout(t);
80
+ reject(error);
81
+ },
82
+ );
83
+ } catch (err) {
84
+ reject(err);
85
+ }
86
+ });
87
+ });
88
+ };
89
+
90
+ /**
91
+ * Wait until promise resolves.
92
+ */
93
+ export const untilPromise = <T = void>(cb: () => Promise<T>) => cb();
94
+
95
+ /**
96
+ * Wait until error is thrown.
97
+ */
98
+ export const untilError = (cb: () => Promise<any>) => {
99
+ return new Promise((resolve, reject) => {
100
+ setTimeout(async () => {
101
+ try {
102
+ await cb();
103
+ reject(new Error('No error was thrown.'));
104
+ } catch (err) {
105
+ resolve(err);
106
+ }
107
+ });
108
+ });
109
+ };