@dxos/async 0.8.4-main.3f58842 → 0.8.4-main.548089c
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 +113 -64
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +113 -64
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/debounce.d.ts +15 -0
- package/dist/types/src/debounce.d.ts.map +1 -1
- package/dist/types/src/task-scheduling.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -9
- package/src/debounce.ts +52 -4
- package/src/event-emitter.test.ts +1 -0
- package/src/persistent-lifecycle.test.ts +1 -1
- package/src/task-scheduling.ts +1 -1
- package/src/testing.test.ts +1 -1
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.548089c",
|
|
4
4
|
"description": "Async utilities.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
|
+
"types": "./dist/types/src/index.d.ts",
|
|
13
14
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
15
|
"node": {
|
|
15
16
|
"require": "./dist/lib/node/index.cjs",
|
|
16
17
|
"default": "./dist/lib/node-esm/index.mjs"
|
|
17
|
-
}
|
|
18
|
-
"types": "./dist/types/src/index.d.ts"
|
|
18
|
+
}
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"zen-observable": "^0.10.0",
|
|
31
31
|
"zen-push": "^0.3.1",
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/invariant": "0.8.4-main.
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/
|
|
32
|
+
"@dxos/context": "0.8.4-main.548089c",
|
|
33
|
+
"@dxos/debug": "0.8.4-main.548089c",
|
|
34
|
+
"@dxos/invariant": "0.8.4-main.548089c",
|
|
35
|
+
"@dxos/node-std": "0.8.4-main.548089c",
|
|
36
|
+
"@dxos/log": "0.8.4-main.548089c",
|
|
37
|
+
"@dxos/util": "0.8.4-main.548089c"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/zen-observable": "^0.8.3"
|
package/src/debounce.ts
CHANGED
|
@@ -4,6 +4,32 @@
|
|
|
4
4
|
|
|
5
5
|
type Callback = (...args: any[]) => void;
|
|
6
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
|
+
};
|
|
32
|
+
|
|
7
33
|
/**
|
|
8
34
|
* Debounce callback.
|
|
9
35
|
*
|
|
@@ -39,12 +65,34 @@ export const throttle = <CB extends Callback>(cb: CB, delay = 100): CB => {
|
|
|
39
65
|
|
|
40
66
|
/**
|
|
41
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.
|
|
42
74
|
*/
|
|
43
75
|
export const debounceAndThrottle = <CB extends Callback>(cb: CB, delay = 100): CB => {
|
|
44
|
-
|
|
45
|
-
|
|
76
|
+
let timeout: ReturnType<typeof setTimeout>;
|
|
77
|
+
let lastCall = 0;
|
|
78
|
+
|
|
46
79
|
return ((...args: any[]) => {
|
|
47
|
-
|
|
48
|
-
|
|
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
|
+
}
|
|
49
97
|
}) as CB;
|
|
50
98
|
};
|
package/src/task-scheduling.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
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
|
|
package/src/testing.test.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { describe, expect, test } from 'vitest';
|
|
|
6
6
|
|
|
7
7
|
import { expectToThrow, raise } from '@dxos/debug';
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { until, waitForCondition } from './testing';
|
|
10
10
|
import { sleep } from './timeout';
|
|
11
11
|
|
|
12
12
|
describe('waitForCondition', () => {
|