@dxos/async 0.8.4-main.ef1bc66f44 → 0.8.4-main.f466a3d56e

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 (38) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +23 -86
  3. package/dist/lib/browser/index.mjs.map +3 -3
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +23 -86
  6. package/dist/lib/node-esm/index.mjs.map +3 -3
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/callback.d.ts.map +1 -1
  9. package/dist/types/src/chain.d.ts.map +1 -1
  10. package/dist/types/src/cleanup.d.ts +1 -2
  11. package/dist/types/src/cleanup.d.ts.map +1 -1
  12. package/dist/types/src/debounce.d.ts +15 -10
  13. package/dist/types/src/debounce.d.ts.map +1 -1
  14. package/dist/types/src/errors.d.ts.map +1 -1
  15. package/dist/types/src/event-emitter.d.ts.map +1 -1
  16. package/dist/types/src/events.d.ts.map +1 -1
  17. package/dist/types/src/mutex.d.ts.map +1 -1
  18. package/dist/types/src/observable-value.d.ts.map +1 -1
  19. package/dist/types/src/observable.d.ts.map +1 -1
  20. package/dist/types/src/persistent-lifecycle.d.ts.map +1 -1
  21. package/dist/types/src/stream-to-array.d.ts.map +1 -1
  22. package/dist/types/src/task-scheduling.d.ts.map +1 -1
  23. package/dist/types/src/test-stream.d.ts.map +1 -1
  24. package/dist/types/src/testing.d.ts.map +1 -1
  25. package/dist/types/src/timeout.d.ts +1 -1
  26. package/dist/types/src/timeout.d.ts.map +1 -1
  27. package/dist/types/src/timer.d.ts.map +1 -1
  28. package/dist/types/src/track-leaks.d.ts.map +1 -1
  29. package/dist/types/src/trigger.d.ts.map +1 -1
  30. package/dist/types/src/update-scheduler.d.ts.map +1 -1
  31. package/dist/types/tsconfig.tsbuildinfo +1 -1
  32. package/package.json +8 -11
  33. package/src/cleanup.ts +5 -3
  34. package/src/debounce.ts +19 -14
  35. package/src/event-emitter.test.ts +0 -1
  36. package/src/observable-value.ts +4 -2
  37. package/src/persistent-lifecycle.ts +8 -0
  38. package/src/timeout.ts +6 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/async",
3
- "version": "0.8.4-main.ef1bc66f44",
3
+ "version": "0.8.4-main.f466a3d56e",
4
4
  "description": "Async utilities.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -8,7 +8,7 @@
8
8
  "type": "git",
9
9
  "url": "https://github.com/dxos/dxos"
10
10
  },
11
- "license": "MIT",
11
+ "license": "FSL-1.1-Apache-2.0",
12
12
  "author": "DXOS.org",
13
13
  "sideEffects": true,
14
14
  "type": "module",
@@ -23,9 +23,6 @@
23
23
  }
24
24
  },
25
25
  "types": "dist/types/src/index.d.ts",
26
- "typesVersions": {
27
- "*": {}
28
- },
29
26
  "files": [
30
27
  "dist",
31
28
  "src"
@@ -33,12 +30,12 @@
33
30
  "dependencies": {
34
31
  "zen-observable": "^0.10.0",
35
32
  "zen-push": "^0.3.1",
36
- "@dxos/context": "0.8.4-main.ef1bc66f44",
37
- "@dxos/debug": "0.8.4-main.ef1bc66f44",
38
- "@dxos/invariant": "0.8.4-main.ef1bc66f44",
39
- "@dxos/log": "0.8.4-main.ef1bc66f44",
40
- "@dxos/util": "0.8.4-main.ef1bc66f44",
41
- "@dxos/node-std": "0.8.4-main.ef1bc66f44"
33
+ "@dxos/debug": "0.8.4-main.f466a3d56e",
34
+ "@dxos/log": "0.8.4-main.f466a3d56e",
35
+ "@dxos/node-std": "0.8.4-main.f466a3d56e",
36
+ "@dxos/util": "0.8.4-main.f466a3d56e",
37
+ "@dxos/context": "0.8.4-main.f466a3d56e",
38
+ "@dxos/invariant": "0.8.4-main.f466a3d56e"
42
39
  },
43
40
  "devDependencies": {
44
41
  "@types/zen-observable": "^0.8.3"
package/src/cleanup.ts CHANGED
@@ -10,9 +10,12 @@ export type CleanupFn = () => void;
10
10
  * Combine multiple cleanup functions into a single cleanup function.
11
11
  * Can be used in effect hooks in conjunction with `addEventListener`.
12
12
  */
13
- export const combine = (...cleanupFns: (CleanupFn | CleanupFn[])[]): CleanupFn => {
13
+ export const combine = (...cleanupFns: (boolean | undefined | CleanupFn | CleanupFn[])[]): CleanupFn => {
14
14
  return () => {
15
- cleanupFns.flat().forEach((cleanupFn) => cleanupFn());
15
+ cleanupFns
16
+ .flat()
17
+ .filter((f): f is CleanupFn => typeof f === 'function')
18
+ .forEach((cleanupFn) => cleanupFn());
16
19
  };
17
20
  };
18
21
 
@@ -37,7 +40,6 @@ type EventMap<T> = T extends Window
37
40
  /**
38
41
  * Add the event listener and return a cleanup function.
39
42
  * Can be used in effect hooks in conjunction with `combine`.
40
- * @deprecated use bind-event-listener
41
43
  */
42
44
  export const addEventListener = <T extends EventTarget, K extends keyof EventMap<T>>(
43
45
  target: T,
package/src/debounce.ts CHANGED
@@ -12,7 +12,7 @@ type Callback = (...args: any[]) => void;
12
12
  * @param delay Time to wait before invoking the callback.
13
13
  * @returns A new function that schedules the callback once and ignores subsequent calls until executed.
14
14
  */
15
- export const delay = <CB extends Callback>(cb: CB, delay = 100): CB => {
15
+ export const delay = <F extends Callback>(cb: F, delay = 100): F => {
16
16
  let pending = false;
17
17
  return ((...args: any[]) => {
18
18
  if (pending) {
@@ -27,32 +27,37 @@ export const delay = <CB extends Callback>(cb: CB, delay = 100): CB => {
27
27
  pending = false;
28
28
  }
29
29
  }, delay);
30
- }) as CB;
30
+ }) as F;
31
31
  };
32
32
 
33
33
  /**
34
- * Debounce callback.
34
+ * Debounce callback: delays execution until calls stop.
35
+ * Each new call resets the timer, so the callback fires only after the delay elapses with no further calls (trailing-edge).
36
+ * Use when you want to react to the end of a burst of events (e.g. user stops typing).
35
37
  *
36
38
  * @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.
39
+ * @param delay Idle time (ms) to wait after the last call before invoking.
40
+ * @returns Wrapped function that postpones invocation until activity ceases.
39
41
  */
40
- export const debounce = <CB extends Callback>(cb: CB, delay = 100): CB => {
42
+ export const debounce = <F extends Callback>(cb: F, delay = 100): F => {
41
43
  let t: ReturnType<typeof setTimeout>;
42
44
  return ((...args: any[]) => {
43
45
  clearTimeout(t);
44
46
  t = setTimeout(() => cb(...args), delay);
45
- }) as CB;
47
+ }) as F;
46
48
  };
47
49
 
48
50
  /**
49
- * Throttle callback.
51
+ * Throttle callback: limits execution to at most once per interval.
52
+ * The callback fires immediately on the first call;
53
+ * subsequent calls within the same interval are dropped (leading-edge).
54
+ * Use when you need regular updates at a bounded rate (e.g. scroll or resize handlers).
50
55
  *
51
56
  * @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.
57
+ * @param delay Minimum interval (ms) between successive invocations.
58
+ * @returns Wrapped function that rate-limits invocations.
54
59
  */
55
- export const throttle = <CB extends Callback>(cb: CB, delay = 100): CB => {
60
+ export const throttle = <F extends Callback>(cb: F, delay = 100): F => {
56
61
  let lastCall = 0;
57
62
  return ((...args: any[]) => {
58
63
  const now = Date.now();
@@ -60,7 +65,7 @@ export const throttle = <CB extends Callback>(cb: CB, delay = 100): CB => {
60
65
  cb(...args);
61
66
  lastCall = now;
62
67
  }
63
- }) as CB;
68
+ }) as F;
64
69
  };
65
70
 
66
71
  /**
@@ -72,7 +77,7 @@ export const throttle = <CB extends Callback>(cb: CB, delay = 100): CB => {
72
77
  * @param delay Time window for both throttle and debounce.
73
78
  * @returns A new function that combines throttle and debounce behavior.
74
79
  */
75
- export const debounceAndThrottle = <CB extends Callback>(cb: CB, delay = 100): CB => {
80
+ export const debounceAndThrottle = <F extends Callback>(cb: F, delay = 100): F => {
76
81
  let timeout: ReturnType<typeof setTimeout>;
77
82
  let lastCall = 0;
78
83
 
@@ -94,5 +99,5 @@ export const debounceAndThrottle = <CB extends Callback>(cb: CB, delay = 100): C
94
99
  lastCall = Date.now();
95
100
  }, delay - delta);
96
101
  }
97
- }) as CB;
102
+ }) as F;
98
103
  };
@@ -3,7 +3,6 @@
3
3
  //
4
4
 
5
5
  import { EventEmitter } from 'node:events';
6
-
7
6
  import { describe, expect, test } from 'vitest';
8
7
 
9
8
  import { onEvent, waitForEvent } from './event-emitter';
@@ -66,8 +66,10 @@ export interface CancellableObservableEvents {
66
66
  /**
67
67
  * @deprecated
68
68
  */
69
- export interface CancellableObservable<Events extends CancellableObservableEvents, Value = unknown>
70
- extends ObservableValue<Events, Value> {
69
+ export interface CancellableObservable<
70
+ Events extends CancellableObservableEvents,
71
+ Value = unknown,
72
+ > extends ObservableValue<Events, Value> {
71
73
  cancel(): Promise<void>;
72
74
  }
73
75
 
@@ -69,12 +69,20 @@ export class PersistentLifecycle<T> extends Resource {
69
69
  try {
70
70
  await this._restart();
71
71
  } catch (err) {
72
+ // Suppress noise from restarts that race with shutdown.
73
+ if (this._ctx?.disposed) {
74
+ return;
75
+ }
72
76
  log.warn('Restart failed', { err });
73
77
  this._restartTask?.schedule();
74
78
  }
75
79
  });
76
80
 
77
81
  this._currentState = await this._start().catch((err) => {
82
+ // Suppress noise when shutdown was requested while the initial start was in flight.
83
+ if (this._ctx?.disposed) {
84
+ return undefined;
85
+ }
78
86
  log.warn('Start failed', { err });
79
87
  this._restartTask?.schedule();
80
88
  return undefined;
package/src/timeout.ts CHANGED
@@ -4,7 +4,6 @@
4
4
 
5
5
  import { type Context, ContextDisposedError } from '@dxos/context';
6
6
 
7
- import { promiseFromCallback } from './callback';
8
7
  import { TimeoutError } from './errors';
9
8
 
10
9
  /**
@@ -57,12 +56,11 @@ export const asyncReturn = () => sleep(0);
57
56
  /**
58
57
  * Wait for promise or throw error.
59
58
  */
60
- export const asyncTimeout = async <T>(
61
- // TODO(dmaretskyi): This callback API is unintuitive and leads to bugs.
62
- promise: Promise<T> | (() => Promise<T>),
63
- timeout: number,
64
- err?: Error | string,
65
- ): Promise<T> => {
59
+ export const asyncTimeout = async <T>(promise: Promise<T>, timeout: number, err?: Error | string): Promise<T> => {
60
+ if (typeof promise === 'function') {
61
+ throw new Error('First argument must be a promise.');
62
+ }
63
+
66
64
  let timeoutId: NodeJS.Timeout;
67
65
  const throwable = err === undefined || typeof err === 'string' ? new TimeoutError(timeout, err) : err;
68
66
  const timeoutPromise = new Promise<T>((resolve, reject) => {
@@ -73,8 +71,7 @@ export const asyncTimeout = async <T>(
73
71
  unrefTimeout(timeoutId);
74
72
  });
75
73
 
76
- const conditionTimeout = typeof promise === 'function' ? promiseFromCallback<T>(promise) : promise;
77
- return await Promise.race([conditionTimeout, timeoutPromise]).finally(() => {
74
+ return await Promise.race([promise, timeoutPromise]).finally(() => {
78
75
  clearTimeout(timeoutId);
79
76
  });
80
77
  };