@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.
- package/LICENSE +102 -5
- package/dist/lib/browser/index.mjs +23 -86
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +23 -86
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/callback.d.ts.map +1 -1
- package/dist/types/src/chain.d.ts.map +1 -1
- package/dist/types/src/cleanup.d.ts +1 -2
- package/dist/types/src/cleanup.d.ts.map +1 -1
- package/dist/types/src/debounce.d.ts +15 -10
- package/dist/types/src/debounce.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/event-emitter.d.ts.map +1 -1
- package/dist/types/src/events.d.ts.map +1 -1
- package/dist/types/src/mutex.d.ts.map +1 -1
- package/dist/types/src/observable-value.d.ts.map +1 -1
- package/dist/types/src/observable.d.ts.map +1 -1
- package/dist/types/src/persistent-lifecycle.d.ts.map +1 -1
- package/dist/types/src/stream-to-array.d.ts.map +1 -1
- package/dist/types/src/task-scheduling.d.ts.map +1 -1
- package/dist/types/src/test-stream.d.ts.map +1 -1
- package/dist/types/src/testing.d.ts.map +1 -1
- package/dist/types/src/timeout.d.ts +1 -1
- package/dist/types/src/timeout.d.ts.map +1 -1
- package/dist/types/src/timer.d.ts.map +1 -1
- package/dist/types/src/track-leaks.d.ts.map +1 -1
- package/dist/types/src/trigger.d.ts.map +1 -1
- package/dist/types/src/update-scheduler.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -11
- package/src/cleanup.ts +5 -3
- package/src/debounce.ts +19 -14
- package/src/event-emitter.test.ts +0 -1
- package/src/observable-value.ts +4 -2
- package/src/persistent-lifecycle.ts +8 -0
- 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.
|
|
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": "
|
|
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/
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/
|
|
39
|
-
"@dxos/
|
|
40
|
-
"@dxos/
|
|
41
|
-
"@dxos/
|
|
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
|
|
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 = <
|
|
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
|
|
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
|
|
38
|
-
* @returns
|
|
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 = <
|
|
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
|
|
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
|
|
53
|
-
* @returns
|
|
57
|
+
* @param delay Minimum interval (ms) between successive invocations.
|
|
58
|
+
* @returns Wrapped function that rate-limits invocations.
|
|
54
59
|
*/
|
|
55
|
-
export const throttle = <
|
|
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
|
|
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 = <
|
|
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
|
|
102
|
+
}) as F;
|
|
98
103
|
};
|
package/src/observable-value.ts
CHANGED
|
@@ -66,8 +66,10 @@ export interface CancellableObservableEvents {
|
|
|
66
66
|
/**
|
|
67
67
|
* @deprecated
|
|
68
68
|
*/
|
|
69
|
-
export interface CancellableObservable<
|
|
70
|
-
extends
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
77
|
-
return await Promise.race([conditionTimeout, timeoutPromise]).finally(() => {
|
|
74
|
+
return await Promise.race([promise, timeoutPromise]).finally(() => {
|
|
78
75
|
clearTimeout(timeoutId);
|
|
79
76
|
});
|
|
80
77
|
};
|