@dxos/async 0.8.3 → 0.8.4-main.1068cf700f
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/dist/lib/browser/index.mjs +346 -218
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +346 -218
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/callback.d.ts +2 -1
- package/dist/types/src/callback.d.ts.map +1 -1
- package/dist/types/src/chain.d.ts +1 -1
- package/dist/types/src/chain.d.ts.map +1 -1
- package/dist/types/src/cleanup.d.ts +3 -2
- package/dist/types/src/cleanup.d.ts.map +1 -1
- package/dist/types/src/debounce.d.ts +34 -1
- package/dist/types/src/debounce.d.ts.map +1 -1
- package/dist/types/src/debounce.test.d.ts +2 -0
- package/dist/types/src/debounce.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +0 -4
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/persistent-lifecycle.d.ts +2 -2
- package/dist/types/src/persistent-lifecycle.d.ts.map +1 -1
- package/dist/types/src/task-scheduling.d.ts +29 -1
- package/dist/types/src/task-scheduling.d.ts.map +1 -1
- package/dist/types/src/testing.d.ts +13 -0
- 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/trigger.d.ts +11 -0
- package/dist/types/src/trigger.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -9
- package/src/callback.ts +3 -3
- package/src/chain.ts +1 -1
- package/src/cleanup.ts +6 -5
- package/src/debounce.test.ts +115 -0
- package/src/debounce.ts +88 -4
- package/src/event-emitter.test.ts +2 -1
- package/src/events.test.ts +1 -1
- package/src/index.ts +0 -4
- package/src/observable-value.test.ts +1 -1
- package/src/persistent-lifecycle.test.ts +1 -1
- package/src/persistent-lifecycle.ts +2 -2
- package/src/task-scheduling.ts +95 -1
- package/src/testing.test.ts +41 -1
- package/src/testing.ts +53 -0
- package/src/timeout.ts +23 -22
- package/src/trigger.ts +58 -1
- package/src/update-scheduler.ts +1 -1
- package/dist/lib/node/index.cjs +0 -1638
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/types/src/latch.d.ts +0 -11
- package/dist/types/src/latch.d.ts.map +0 -1
- package/dist/types/src/sink.d.ts +0 -6
- package/dist/types/src/sink.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -2
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/until.d.ts +0 -14
- package/dist/types/src/until.d.ts.map +0 -1
- package/dist/types/src/until.test.d.ts +0 -2
- package/dist/types/src/until.test.d.ts.map +0 -1
- package/src/latch.ts +0 -60
- package/src/sink.ts +0 -26
- package/src/types.ts +0 -5
- package/src/until.test.ts +0 -47
- package/src/until.ts +0 -58
package/src/timeout.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { type Context, ContextDisposedError } from '@dxos/context';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { promiseFromCallback } from './callback';
|
|
8
8
|
import { TimeoutError } from './errors';
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -28,6 +28,27 @@ export const sleep = (ms: number) => {
|
|
|
28
28
|
});
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
// TODO(burdon): Reconcile with sleep.
|
|
32
|
+
export const sleepWithContext = (ctx: Context, ms: number) => {
|
|
33
|
+
const error = new ContextDisposedError();
|
|
34
|
+
return new Promise<void>((resolve, reject) => {
|
|
35
|
+
if (ctx.disposed) {
|
|
36
|
+
reject(error);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const timeout = setTimeout(() => {
|
|
41
|
+
clearDispose();
|
|
42
|
+
resolve();
|
|
43
|
+
}, ms);
|
|
44
|
+
|
|
45
|
+
const clearDispose = ctx.onDispose(() => {
|
|
46
|
+
clearTimeout(timeout);
|
|
47
|
+
reject(error);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
31
52
|
/**
|
|
32
53
|
* Can be used in long-running tasks to let other callbacks be invoked.
|
|
33
54
|
*/
|
|
@@ -52,7 +73,7 @@ export const asyncTimeout = async <T>(
|
|
|
52
73
|
unrefTimeout(timeoutId);
|
|
53
74
|
});
|
|
54
75
|
|
|
55
|
-
const conditionTimeout = typeof promise === 'function' ?
|
|
76
|
+
const conditionTimeout = typeof promise === 'function' ? promiseFromCallback<T>(promise) : promise;
|
|
56
77
|
return await Promise.race([conditionTimeout, timeoutPromise]).finally(() => {
|
|
57
78
|
clearTimeout(timeoutId);
|
|
58
79
|
});
|
|
@@ -67,23 +88,3 @@ export const unrefTimeout = (timeoutId: NodeJS.Timeout) => {
|
|
|
67
88
|
timeoutId.unref();
|
|
68
89
|
}
|
|
69
90
|
};
|
|
70
|
-
|
|
71
|
-
export const sleepWithContext = (ctx: Context, ms: number) => {
|
|
72
|
-
const error = new ContextDisposedError();
|
|
73
|
-
return new Promise<void>((resolve, reject) => {
|
|
74
|
-
if (ctx.disposed) {
|
|
75
|
-
reject(error);
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const timeout = setTimeout(() => {
|
|
80
|
-
clearDispose();
|
|
81
|
-
resolve();
|
|
82
|
-
}, ms);
|
|
83
|
-
|
|
84
|
-
const clearDispose = ctx.onDispose(() => {
|
|
85
|
-
clearTimeout(timeout);
|
|
86
|
-
reject(error);
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
};
|
package/src/trigger.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { invariant } from '@dxos/invariant';
|
|
6
|
+
|
|
5
7
|
import { TimeoutError } from './errors';
|
|
6
8
|
import { asyncTimeout } from './timeout';
|
|
7
9
|
|
|
@@ -10,7 +12,7 @@ import { asyncTimeout } from './timeout';
|
|
|
10
12
|
* @deprecated Use `Trigger` instead.
|
|
11
13
|
*/
|
|
12
14
|
export const trigger = <T = void>(timeout?: number): [() => Promise<T>, (arg: T) => void] => {
|
|
13
|
-
// eslint-disable-line
|
|
15
|
+
// eslint-disable-line prefer-arrow-functions/prefer-arrow-functions
|
|
14
16
|
let callback: (arg: T) => void;
|
|
15
17
|
|
|
16
18
|
const promise = new Promise<T>((resolve, reject) => {
|
|
@@ -121,3 +123,58 @@ export class Trigger<T = void> {
|
|
|
121
123
|
return this;
|
|
122
124
|
}
|
|
123
125
|
}
|
|
126
|
+
|
|
127
|
+
type LatchProps = {
|
|
128
|
+
count?: number;
|
|
129
|
+
timeout?: number;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
type LatchResult = [() => Promise<number>, () => number, (err: Error) => void];
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Returns a callback and a promise that's resolved when the callback is called n times.
|
|
136
|
+
* @deprecated Use `Trigger` instead.
|
|
137
|
+
*/
|
|
138
|
+
export const latch = ({ count = 1, timeout }: LatchProps = {}): LatchResult => {
|
|
139
|
+
invariant(count >= 0);
|
|
140
|
+
|
|
141
|
+
let t: ReturnType<typeof setTimeout>;
|
|
142
|
+
let doResolve: (value: number) => void;
|
|
143
|
+
let doReject: (err: Error) => void;
|
|
144
|
+
const promise = new Promise<number>((resolve, reject) => {
|
|
145
|
+
doResolve = (value) => {
|
|
146
|
+
clearTimeout(t);
|
|
147
|
+
resolve(value);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
doReject = (err) => {
|
|
151
|
+
clearTimeout(t);
|
|
152
|
+
reject(err);
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
if (count === 0) {
|
|
157
|
+
setTimeout(() => {
|
|
158
|
+
doResolve(0);
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
if (timeout) {
|
|
162
|
+
t = setTimeout(() => {
|
|
163
|
+
doReject(new Error(`Timed out after ${timeout.toLocaleString()}ms`));
|
|
164
|
+
}, timeout);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let i = 0;
|
|
169
|
+
return [
|
|
170
|
+
async () => await promise,
|
|
171
|
+
() => {
|
|
172
|
+
if (++i === count) {
|
|
173
|
+
doResolve(i);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return i;
|
|
177
|
+
},
|
|
178
|
+
(err: Error) => doReject(err),
|
|
179
|
+
];
|
|
180
|
+
};
|