@libp2p/utils 6.1.3-a5cd8cfbe → 6.1.3-c2ff2e454
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/src/debounce.d.ts +11 -0
- package/dist/src/debounce.d.ts.map +1 -0
- package/dist/src/debounce.js +21 -0
- package/dist/src/debounce.js.map +1 -0
- package/dist/src/repeating-task.d.ts +18 -0
- package/dist/src/repeating-task.d.ts.map +1 -0
- package/dist/src/repeating-task.js +55 -0
- package/dist/src/repeating-task.js.map +1 -0
- package/package.json +13 -5
- package/src/debounce.ts +28 -0
- package/src/repeating-task.ts +82 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface CancelableFunction {
|
|
2
|
+
(): void;
|
|
3
|
+
stop(): void;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Returns a function wrapper that will only call the passed function once
|
|
7
|
+
*
|
|
8
|
+
* Important - the passed function should not throw or reject
|
|
9
|
+
*/
|
|
10
|
+
export declare function debounce(func: () => void | Promise<void>, wait: number): CancelableFunction;
|
|
11
|
+
//# sourceMappingURL=debounce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,IAAI,IAAI,CAAA;IACR,IAAI,IAAI,IAAI,CAAA;CACb;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAE,IAAI,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,kBAAkB,CAiB5F"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a function wrapper that will only call the passed function once
|
|
3
|
+
*
|
|
4
|
+
* Important - the passed function should not throw or reject
|
|
5
|
+
*/
|
|
6
|
+
export function debounce(func, wait) {
|
|
7
|
+
let timeout;
|
|
8
|
+
const output = function () {
|
|
9
|
+
const later = function () {
|
|
10
|
+
timeout = undefined;
|
|
11
|
+
void func();
|
|
12
|
+
};
|
|
13
|
+
clearTimeout(timeout);
|
|
14
|
+
timeout = setTimeout(later, wait);
|
|
15
|
+
};
|
|
16
|
+
output.stop = () => {
|
|
17
|
+
clearTimeout(timeout);
|
|
18
|
+
};
|
|
19
|
+
return output;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=debounce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.js","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAE,IAAgC,EAAE,IAAY;IACtE,IAAI,OAAkD,CAAA;IAEtD,MAAM,MAAM,GAAG;QACb,MAAM,KAAK,GAAG;YACZ,OAAO,GAAG,SAAS,CAAA;YACnB,KAAK,IAAI,EAAE,CAAA;QACb,CAAC,CAAA;QAED,YAAY,CAAC,OAAO,CAAC,CAAA;QACrB,OAAO,GAAG,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IACnC,CAAC,CAAA;IACD,MAAM,CAAC,IAAI,GAAG,GAAG,EAAE;QACjB,YAAY,CAAC,OAAO,CAAC,CAAA;IACvB,CAAC,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AbortOptions } from '@libp2p/interface';
|
|
2
|
+
export interface RepeatingTask {
|
|
3
|
+
start(): void;
|
|
4
|
+
stop(): void;
|
|
5
|
+
}
|
|
6
|
+
export interface RepeatingTaskOptions {
|
|
7
|
+
/**
|
|
8
|
+
* How long the task is allowed to run before the passed AbortSignal fires an
|
|
9
|
+
* abort event
|
|
10
|
+
*/
|
|
11
|
+
timeout?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to schedule the task to run immediately
|
|
14
|
+
*/
|
|
15
|
+
runImmediately?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function repeatingTask(fn: (options?: AbortOptions) => void | Promise<void>, interval: number, options?: RepeatingTaskOptions): RepeatingTask;
|
|
18
|
+
//# sourceMappingURL=repeating-task.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repeating-task.d.ts","sourceRoot":"","sources":["../../src/repeating-task.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAErD,MAAM,WAAW,aAAa;IAC5B,KAAK,IAAI,IAAI,CAAA;IACb,IAAI,IAAI,IAAI,CAAA;CACb;AAED,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB;AAED,wBAAgB,aAAa,CAAE,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,YAAY,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,aAAa,CA2DpJ"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { setMaxListeners } from '@libp2p/interface';
|
|
2
|
+
import { anySignal } from 'any-signal';
|
|
3
|
+
export function repeatingTask(fn, interval, options) {
|
|
4
|
+
let timeout;
|
|
5
|
+
let shutdownController;
|
|
6
|
+
function runTask() {
|
|
7
|
+
const opts = {
|
|
8
|
+
signal: shutdownController.signal
|
|
9
|
+
};
|
|
10
|
+
if (options?.timeout != null) {
|
|
11
|
+
const signal = anySignal([shutdownController.signal, AbortSignal.timeout(options.timeout)]);
|
|
12
|
+
setMaxListeners(Infinity, signal);
|
|
13
|
+
opts.signal = signal;
|
|
14
|
+
}
|
|
15
|
+
Promise.resolve().then(async () => {
|
|
16
|
+
await fn(opts);
|
|
17
|
+
})
|
|
18
|
+
.catch(() => { })
|
|
19
|
+
.finally(() => {
|
|
20
|
+
if (shutdownController.signal.aborted) {
|
|
21
|
+
// task has been cancelled, bail
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
// reschedule
|
|
25
|
+
timeout = setTimeout(runTask, interval);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
let started = false;
|
|
29
|
+
return {
|
|
30
|
+
start: () => {
|
|
31
|
+
if (started) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
started = true;
|
|
35
|
+
shutdownController = new AbortController();
|
|
36
|
+
setMaxListeners(Infinity, shutdownController.signal);
|
|
37
|
+
// run now
|
|
38
|
+
if (options?.runImmediately === true) {
|
|
39
|
+
queueMicrotask(() => {
|
|
40
|
+
runTask();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// run later
|
|
45
|
+
timeout = setTimeout(runTask, interval);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
stop: () => {
|
|
49
|
+
clearTimeout(timeout);
|
|
50
|
+
shutdownController?.abort();
|
|
51
|
+
started = false;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=repeating-task.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repeating-task.js","sourceRoot":"","sources":["../../src/repeating-task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAqBtC,MAAM,UAAU,aAAa,CAAE,EAAoD,EAAE,QAAgB,EAAE,OAA8B;IACnI,IAAI,OAAsC,CAAA;IAC1C,IAAI,kBAAmC,CAAA;IAEvC,SAAS,OAAO;QACd,MAAM,IAAI,GAAiB;YACzB,MAAM,EAAE,kBAAkB,CAAC,MAAM;SAClC,CAAA;QAED,IAAI,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,CAAC;YAC7B,MA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AA;YACrB,kBAAkB,EAAE,KAAK,EAAE,CAAA;YAC3B,OAAO,GAAG,KAAK,CAAA;QACjB,CAAC;KACF,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/utils",
|
|
3
|
-
"version": "6.1.3-
|
|
3
|
+
"version": "6.1.3-c2ff2e454",
|
|
4
4
|
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/utils#readme",
|
|
@@ -68,6 +68,10 @@
|
|
|
68
68
|
"types": "./dist/src/close-source.d.ts",
|
|
69
69
|
"import": "./dist/src/close-source.js"
|
|
70
70
|
},
|
|
71
|
+
"./debounce": {
|
|
72
|
+
"types": "./dist/src/debounce.d.ts",
|
|
73
|
+
"import": "./dist/src/debounce.js"
|
|
74
|
+
},
|
|
71
75
|
"./filters": {
|
|
72
76
|
"types": "./dist/src/filters/index.d.ts",
|
|
73
77
|
"import": "./dist/src/filters/index.js"
|
|
@@ -112,6 +116,10 @@
|
|
|
112
116
|
"types": "./dist/src/rate-limiter.d.ts",
|
|
113
117
|
"import": "./dist/src/rate-limiter.js"
|
|
114
118
|
},
|
|
119
|
+
"./repeating-task": {
|
|
120
|
+
"types": "./dist/src/repeating-task.d.ts",
|
|
121
|
+
"import": "./dist/src/repeating-task.js"
|
|
122
|
+
},
|
|
115
123
|
"./stream-to-ma-conn": {
|
|
116
124
|
"types": "./dist/src/stream-to-ma-conn.d.ts",
|
|
117
125
|
"import": "./dist/src/stream-to-ma-conn.js"
|
|
@@ -148,9 +156,9 @@
|
|
|
148
156
|
},
|
|
149
157
|
"dependencies": {
|
|
150
158
|
"@chainsafe/is-ip": "^2.0.2",
|
|
151
|
-
"@libp2p/crypto": "5.0.6-
|
|
152
|
-
"@libp2p/interface": "2.2.0-
|
|
153
|
-
"@libp2p/logger": "5.1.3-
|
|
159
|
+
"@libp2p/crypto": "5.0.6-c2ff2e454",
|
|
160
|
+
"@libp2p/interface": "2.2.0-c2ff2e454",
|
|
161
|
+
"@libp2p/logger": "5.1.3-c2ff2e454",
|
|
154
162
|
"@multiformats/multiaddr": "^12.2.3",
|
|
155
163
|
"@sindresorhus/fnv1a": "^3.1.0",
|
|
156
164
|
"@types/murmurhash3js-revisited": "^3.0.3",
|
|
@@ -171,7 +179,7 @@
|
|
|
171
179
|
"uint8arrays": "^5.1.0"
|
|
172
180
|
},
|
|
173
181
|
"devDependencies": {
|
|
174
|
-
"@libp2p/peer-id": "5.0.7-
|
|
182
|
+
"@libp2p/peer-id": "5.0.7-c2ff2e454",
|
|
175
183
|
"@types/netmask": "^2.0.5",
|
|
176
184
|
"aegir": "^44.0.1",
|
|
177
185
|
"benchmark": "^2.1.4",
|
package/src/debounce.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface CancelableFunction {
|
|
2
|
+
(): void
|
|
3
|
+
stop(): void
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns a function wrapper that will only call the passed function once
|
|
8
|
+
*
|
|
9
|
+
* Important - the passed function should not throw or reject
|
|
10
|
+
*/
|
|
11
|
+
export function debounce (func: () => void | Promise<void>, wait: number): CancelableFunction {
|
|
12
|
+
let timeout: ReturnType<typeof setTimeout> | undefined
|
|
13
|
+
|
|
14
|
+
const output = function (): void {
|
|
15
|
+
const later = function (): void {
|
|
16
|
+
timeout = undefined
|
|
17
|
+
void func()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
clearTimeout(timeout)
|
|
21
|
+
timeout = setTimeout(later, wait)
|
|
22
|
+
}
|
|
23
|
+
output.stop = () => {
|
|
24
|
+
clearTimeout(timeout)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return output
|
|
28
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { setMaxListeners } from '@libp2p/interface'
|
|
2
|
+
import { anySignal } from 'any-signal'
|
|
3
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
4
|
+
|
|
5
|
+
export interface RepeatingTask {
|
|
6
|
+
start(): void
|
|
7
|
+
stop(): void
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface RepeatingTaskOptions {
|
|
11
|
+
/**
|
|
12
|
+
* How long the task is allowed to run before the passed AbortSignal fires an
|
|
13
|
+
* abort event
|
|
14
|
+
*/
|
|
15
|
+
timeout?: number
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Whether to schedule the task to run immediately
|
|
19
|
+
*/
|
|
20
|
+
runImmediately?: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function repeatingTask (fn: (options?: AbortOptions) => void | Promise<void>, interval: number, options?: RepeatingTaskOptions): RepeatingTask {
|
|
24
|
+
let timeout: ReturnType<typeof setTimeout>
|
|
25
|
+
let shutdownController: AbortController
|
|
26
|
+
|
|
27
|
+
function runTask (): void {
|
|
28
|
+
const opts: AbortOptions = {
|
|
29
|
+
signal: shutdownController.signal
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (options?.timeout != null) {
|
|
33
|
+
const signal = anySignal([shutdownController.signal, AbortSignal.timeout(options.timeout)])
|
|
34
|
+
setMaxListeners(Infinity, signal)
|
|
35
|
+
|
|
36
|
+
opts.signal = signal
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
Promise.resolve().then(async () => {
|
|
40
|
+
await fn(opts)
|
|
41
|
+
})
|
|
42
|
+
.catch(() => {})
|
|
43
|
+
.finally(() => {
|
|
44
|
+
if (shutdownController.signal.aborted) {
|
|
45
|
+
// task has been cancelled, bail
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// reschedule
|
|
50
|
+
timeout = setTimeout(runTask, interval)
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let started = false
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
start: () => {
|
|
58
|
+
if (started) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
started = true
|
|
63
|
+
shutdownController = new AbortController()
|
|
64
|
+
setMaxListeners(Infinity, shutdownController.signal)
|
|
65
|
+
|
|
66
|
+
// run now
|
|
67
|
+
if (options?.runImmediately === true) {
|
|
68
|
+
queueMicrotask(() => {
|
|
69
|
+
runTask()
|
|
70
|
+
})
|
|
71
|
+
} else {
|
|
72
|
+
// run later
|
|
73
|
+
timeout = setTimeout(runTask, interval)
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
stop: () => {
|
|
77
|
+
clearTimeout(timeout)
|
|
78
|
+
shutdownController?.abort()
|
|
79
|
+
started = false
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|