@peerbit/time 2.0.2 → 2.0.4
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/lib/esm/hrtime.d.ts +5 -0
- package/lib/esm/hrtime.js +71 -0
- package/lib/esm/hrtime.js.map +1 -0
- package/lib/esm/index.d.ts +4 -21
- package/lib/esm/index.js +4 -75
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/metrics.d.ts +7 -0
- package/lib/esm/metrics.js +19 -0
- package/lib/esm/metrics.js.map +1 -0
- package/lib/esm/wait.d.ts +21 -0
- package/lib/esm/wait.js +76 -0
- package/lib/esm/wait.js.map +1 -0
- package/package.json +2 -2
- package/src/hrtime.ts +78 -0
- package/src/index.ts +4 -103
- package/src/metrics.ts +19 -0
- package/src/wait.ts +103 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2020 Vlad Tansky
|
|
6
|
+
Copyright (c) 2022 dao.xyz
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
|
25
|
+
|
|
26
|
+
*/
|
|
27
|
+
const _perfomancePolyfill = () => {
|
|
28
|
+
// based on https://gist.github.com/paulirish/5438650 copyright Paul Irish 2015.
|
|
29
|
+
if ("performance" in window === false) {
|
|
30
|
+
window.performance = {};
|
|
31
|
+
}
|
|
32
|
+
Date.now =
|
|
33
|
+
Date.now ||
|
|
34
|
+
(() => {
|
|
35
|
+
// thanks IE8
|
|
36
|
+
return new Date().getTime();
|
|
37
|
+
});
|
|
38
|
+
if ("now" in window.performance === false) {
|
|
39
|
+
let nowOffset = Date.now();
|
|
40
|
+
if (performance.timing && performance.timing.navigationStart) {
|
|
41
|
+
nowOffset = performance.timing.navigationStart;
|
|
42
|
+
}
|
|
43
|
+
window.performance["now"] = () => Date.now() - nowOffset;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const _hrtime = (previousTimestamp) => {
|
|
47
|
+
_perfomancePolyfill();
|
|
48
|
+
const baseNow = Math.floor((Date.now() - performance.now()) * 1e-3);
|
|
49
|
+
const clocktime = performance.now() * 1e-3;
|
|
50
|
+
let seconds = Math.floor(clocktime) + baseNow;
|
|
51
|
+
let nanoseconds = Math.floor((clocktime % 1) * 1e9);
|
|
52
|
+
if (previousTimestamp) {
|
|
53
|
+
seconds = seconds - previousTimestamp[0];
|
|
54
|
+
nanoseconds = nanoseconds - previousTimestamp[1];
|
|
55
|
+
if (nanoseconds < 0) {
|
|
56
|
+
seconds--;
|
|
57
|
+
nanoseconds += 1e9;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return [seconds, nanoseconds];
|
|
61
|
+
};
|
|
62
|
+
const NS_PER_SEC = 1e9;
|
|
63
|
+
_hrtime.bigint = (time) => {
|
|
64
|
+
const diff = _hrtime(time);
|
|
65
|
+
return BigInt(diff[0] * NS_PER_SEC + diff[1]);
|
|
66
|
+
};
|
|
67
|
+
export default typeof process === "undefined" ||
|
|
68
|
+
typeof process.hrtime === "undefined"
|
|
69
|
+
? _hrtime
|
|
70
|
+
: process.hrtime;
|
|
71
|
+
//# sourceMappingURL=hrtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hrtime.js","sourceRoot":"","sources":["../../src/hrtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;EAyBE;AAEF,MAAM,mBAAmB,GAAG,GAAG,EAAE;IAChC,gFAAgF;IAChF,IAAI,aAAa,IAAI,MAAM,KAAK,KAAK,EAAE,CAAC;QACrC,MAAc,CAAC,WAAmB,GAAG,EAAE,CAAC;IAC3C,CAAC;IAED,IAAI,CAAC,GAAG;QACP,IAAI,CAAC,GAAG;YACR,CAAC,GAAG,EAAE;gBACL,aAAa;gBACb,OAAO,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;YAC7B,CAAC,CAAC,CAAC;IAEJ,IAAI,KAAK,IAAI,MAAM,CAAC,WAAW,KAAK,KAAK,EAAE,CAAC;QAC3C,IAAI,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAE3B,IAAI,WAAW,CAAC,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;YAC9D,SAAS,GAAG,WAAW,CAAC,MAAM,CAAC,eAAe,CAAC;QAChD,CAAC;QAEA,MAAM,CAAC,WAAmB,CAAC,KAAK,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;IACnE,CAAC;AACF,CAAC,CAAC;AAEF,MAAM,OAAO,GAAG,CAAC,iBAAoC,EAAoB,EAAE;IAC1E,mBAAmB,EAAE,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
3
|
-
|
|
4
|
-
export
|
|
5
|
-
constructor(message?: string);
|
|
6
|
-
}
|
|
7
|
-
export declare const delay: (ms: number, options?: {
|
|
8
|
-
signal?: AbortSignal;
|
|
9
|
-
}) => Promise<void>;
|
|
10
|
-
export declare const waitFor: <T>(fn: () => T | Promise<T>, options?: {
|
|
11
|
-
timeout?: number;
|
|
12
|
-
signal?: AbortSignal;
|
|
13
|
-
delayInterval?: number;
|
|
14
|
-
timeoutMessage?: string;
|
|
15
|
-
}) => Promise<T | undefined>;
|
|
16
|
-
export declare const waitForResolved: <T>(fn: () => T | Promise<T>, options?: {
|
|
17
|
-
timeout?: number;
|
|
18
|
-
signal?: AbortSignal;
|
|
19
|
-
delayInterval?: number;
|
|
20
|
-
timeoutMessage?: string;
|
|
21
|
-
}) => Promise<T>;
|
|
1
|
+
export * from "./wait.js";
|
|
2
|
+
export * from "./metrics.js";
|
|
3
|
+
import hrtime from "./hrtime.js";
|
|
4
|
+
export { hrtime };
|
package/lib/esm/index.js
CHANGED
|
@@ -1,76 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
export class AbortError extends Error {
|
|
7
|
-
constructor(message) {
|
|
8
|
-
super(message);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export const delay = (ms, options) => {
|
|
12
|
-
return new Promise((res, rej) => {
|
|
13
|
-
function handleAbort() {
|
|
14
|
-
clearTimeout(timer);
|
|
15
|
-
rej(new AbortError());
|
|
16
|
-
}
|
|
17
|
-
options?.signal?.addEventListener("abort", handleAbort);
|
|
18
|
-
const timer = setTimeout(() => {
|
|
19
|
-
options?.signal?.removeEventListener("abort", handleAbort);
|
|
20
|
-
res();
|
|
21
|
-
}, ms);
|
|
22
|
-
});
|
|
23
|
-
};
|
|
24
|
-
const createTimeoutError = (options) => new TimeoutError(options?.timeoutMessage
|
|
25
|
-
? "Timed out: " + options?.timeoutMessage
|
|
26
|
-
: "Timed out");
|
|
27
|
-
export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval: 100 }) => {
|
|
28
|
-
const delayInterval = options.delayInterval || 100;
|
|
29
|
-
const timeout = options.timeout || 10 * 1000;
|
|
30
|
-
const startTime = +new Date();
|
|
31
|
-
let stop = false;
|
|
32
|
-
const handleAbort = () => {
|
|
33
|
-
stop = true;
|
|
34
|
-
options.signal?.removeEventListener("abort", handleAbort);
|
|
35
|
-
};
|
|
36
|
-
options.signal?.addEventListener("abort", handleAbort);
|
|
37
|
-
while (!stop && +new Date() - startTime < timeout) {
|
|
38
|
-
const result = await fn();
|
|
39
|
-
if (result) {
|
|
40
|
-
options.signal?.removeEventListener("abort", handleAbort);
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
await delay(delayInterval, options);
|
|
44
|
-
}
|
|
45
|
-
throw createTimeoutError(options);
|
|
46
|
-
};
|
|
47
|
-
export const waitForResolved = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
|
|
48
|
-
const delayInterval = options.delayInterval || 50;
|
|
49
|
-
const timeout = options.timeout || 10 * 1000;
|
|
50
|
-
const startTime = +new Date();
|
|
51
|
-
let stop = false;
|
|
52
|
-
let lastError;
|
|
53
|
-
const handleAbort = () => {
|
|
54
|
-
stop = true;
|
|
55
|
-
options.signal?.removeEventListener("abort", handleAbort);
|
|
56
|
-
};
|
|
57
|
-
options.signal?.addEventListener("abort", handleAbort);
|
|
58
|
-
while (!stop && +new Date() - startTime < timeout) {
|
|
59
|
-
try {
|
|
60
|
-
const result = await fn();
|
|
61
|
-
options.signal?.removeEventListener("abort", handleAbort);
|
|
62
|
-
return result;
|
|
63
|
-
}
|
|
64
|
-
catch (error) {
|
|
65
|
-
if (error instanceof AbortError === false) {
|
|
66
|
-
lastError = error;
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
throw error;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
await delay(delayInterval, options);
|
|
73
|
-
}
|
|
74
|
-
throw lastError || createTimeoutError(options);
|
|
75
|
-
};
|
|
1
|
+
export * from "./wait.js";
|
|
2
|
+
export * from "./metrics.js";
|
|
3
|
+
import hrtime from "./hrtime.js";
|
|
4
|
+
export { hrtime };
|
|
76
5
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import hrtime from "./hrtime.js";
|
|
2
|
+
export class MovingAverageTracker {
|
|
3
|
+
tau;
|
|
4
|
+
lastTS;
|
|
5
|
+
value = 0;
|
|
6
|
+
constructor(tau = 10) {
|
|
7
|
+
this.tau = tau;
|
|
8
|
+
this.lastTS = hrtime.bigint();
|
|
9
|
+
}
|
|
10
|
+
add(number) {
|
|
11
|
+
const now = hrtime.bigint();
|
|
12
|
+
const diff = Number(now - this.lastTS);
|
|
13
|
+
const dt = diff / 1e9;
|
|
14
|
+
this.lastTS = now;
|
|
15
|
+
const alpha_t = 1 - Math.exp(-dt / this.tau);
|
|
16
|
+
this.value = (1 - alpha_t) * this.value + (alpha_t * number) / dt;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/metrics.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,MAAM,OAAO,oBAAoB;IAKX;IAJb,MAAM,CAAS;IAEvB,KAAK,GAAG,CAAC,CAAC;IAEV,YAAqB,MAAM,EAAE;QAAR,QAAG,GAAH,GAAG,CAAK;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;IAC/B,CAAC;IACD,GAAG,CAAC,MAAc;QACjB,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;QACvC,MAAM,EAAE,GAAG,IAAI,GAAG,GAAG,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QAClB,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QAC7C,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,GAAG,EAAE,CAAC;IACnE,CAAC;CACD"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class TimeoutError extends Error {
|
|
2
|
+
constructor(message?: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class AbortError extends Error {
|
|
5
|
+
constructor(message?: string);
|
|
6
|
+
}
|
|
7
|
+
export declare const delay: (ms: number, options?: {
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}) => Promise<void>;
|
|
10
|
+
export declare const waitFor: <T>(fn: () => T | Promise<T>, options?: {
|
|
11
|
+
timeout?: number;
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
delayInterval?: number;
|
|
14
|
+
timeoutMessage?: string;
|
|
15
|
+
}) => Promise<T | undefined>;
|
|
16
|
+
export declare const waitForResolved: <T>(fn: () => T | Promise<T>, options?: {
|
|
17
|
+
timeout?: number;
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
delayInterval?: number;
|
|
20
|
+
timeoutMessage?: string;
|
|
21
|
+
}) => Promise<T>;
|
package/lib/esm/wait.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export class TimeoutError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export class AbortError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export const delay = (ms, options) => {
|
|
12
|
+
return new Promise((res, rej) => {
|
|
13
|
+
function handleAbort() {
|
|
14
|
+
clearTimeout(timer);
|
|
15
|
+
rej(new AbortError());
|
|
16
|
+
}
|
|
17
|
+
options?.signal?.addEventListener("abort", handleAbort);
|
|
18
|
+
const timer = setTimeout(() => {
|
|
19
|
+
options?.signal?.removeEventListener("abort", handleAbort);
|
|
20
|
+
res();
|
|
21
|
+
}, ms);
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
const createTimeoutError = (options) => new TimeoutError(options?.timeoutMessage
|
|
25
|
+
? "Timed out: " + options?.timeoutMessage
|
|
26
|
+
: "Timed out");
|
|
27
|
+
export const waitFor = async (fn, options = { timeout: 10 * 1000, delayInterval: 100 }) => {
|
|
28
|
+
const delayInterval = options.delayInterval || 100;
|
|
29
|
+
const timeout = options.timeout || 10 * 1000;
|
|
30
|
+
const startTime = +new Date();
|
|
31
|
+
let stop = false;
|
|
32
|
+
const handleAbort = () => {
|
|
33
|
+
stop = true;
|
|
34
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
35
|
+
};
|
|
36
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
37
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
38
|
+
const result = await fn();
|
|
39
|
+
if (result) {
|
|
40
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
await delay(delayInterval, options);
|
|
44
|
+
}
|
|
45
|
+
throw createTimeoutError(options);
|
|
46
|
+
};
|
|
47
|
+
export const waitForResolved = async (fn, options = { timeout: 10 * 1000, delayInterval: 50 }) => {
|
|
48
|
+
const delayInterval = options.delayInterval || 50;
|
|
49
|
+
const timeout = options.timeout || 10 * 1000;
|
|
50
|
+
const startTime = +new Date();
|
|
51
|
+
let stop = false;
|
|
52
|
+
let lastError;
|
|
53
|
+
const handleAbort = () => {
|
|
54
|
+
stop = true;
|
|
55
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
56
|
+
};
|
|
57
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
58
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
59
|
+
try {
|
|
60
|
+
const result = await fn();
|
|
61
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if (error instanceof AbortError === false) {
|
|
66
|
+
lastError = error;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
await delay(delayInterval, options);
|
|
73
|
+
}
|
|
74
|
+
throw lastError || createTimeoutError(options);
|
|
75
|
+
};
|
|
76
|
+
//# sourceMappingURL=wait.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wait.js","sourceRoot":"","sources":["../../src/wait.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;IACtC,YAAY,OAAgB;QAC3B,KAAK,CAAC,OAAO,CAAC,CAAC;IAChB,CAAC;CACD;AAED,MAAM,OAAO,UAAW,SAAQ,KAAK;IACpC,YAAY,OAAgB;QAC3B,KAAK,CAAC,OAAO,CAAC,CAAC;IAChB,CAAC;CACD;AACD,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,OAAkC,EAAE,EAAE;IACvE,OAAO,IAAI,OAAO,CAAO,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC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YAC1D,OAAO,MAAM,CAAC;QACf,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YACrB,IAAI,KAAK,YAAY,UAAU,KAAK,KAAK,EAAE,CAAC;gBAC3C,SAAS,GAAG,KAAK,CAAC;YACnB,CAAC;iBAAM,CAAC;gBACP,MAAM,KAAK,CAAC;YACb,CAAC;QACF,CAAC;QACD,MAAM,KAAK,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;IACrC,CAAC;IAED,MAAM,SAAS,IAAI,kBAAkB,CAAC,OAAO,CAAC,CAAC;AAChD,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/time",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "Utility functions for time",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
},
|
|
30
30
|
"author": "dao.xyz",
|
|
31
31
|
"license": "MIT",
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "2539f936bbe572f34a297bc8275fcd059105fc2a"
|
|
33
33
|
}
|
package/src/hrtime.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2020 Vlad Tansky
|
|
6
|
+
Copyright (c) 2022 dao.xyz
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
|
25
|
+
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const _perfomancePolyfill = () => {
|
|
29
|
+
// based on https://gist.github.com/paulirish/5438650 copyright Paul Irish 2015.
|
|
30
|
+
if ("performance" in window === false) {
|
|
31
|
+
((window as any).performance as any) = {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
Date.now =
|
|
35
|
+
Date.now ||
|
|
36
|
+
(() => {
|
|
37
|
+
// thanks IE8
|
|
38
|
+
return new Date().getTime();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if ("now" in window.performance === false) {
|
|
42
|
+
let nowOffset = Date.now();
|
|
43
|
+
|
|
44
|
+
if (performance.timing && performance.timing.navigationStart) {
|
|
45
|
+
nowOffset = performance.timing.navigationStart;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
(window.performance as any)["now"] = () => Date.now() - nowOffset;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const _hrtime = (previousTimestamp?: [number, number]): [number, number] => {
|
|
53
|
+
_perfomancePolyfill();
|
|
54
|
+
const baseNow = Math.floor((Date.now() - performance.now()) * 1e-3);
|
|
55
|
+
const clocktime = performance.now() * 1e-3;
|
|
56
|
+
let seconds = Math.floor(clocktime) + baseNow;
|
|
57
|
+
let nanoseconds = Math.floor((clocktime % 1) * 1e9);
|
|
58
|
+
|
|
59
|
+
if (previousTimestamp) {
|
|
60
|
+
seconds = seconds - previousTimestamp[0];
|
|
61
|
+
nanoseconds = nanoseconds - previousTimestamp[1];
|
|
62
|
+
if (nanoseconds < 0) {
|
|
63
|
+
seconds--;
|
|
64
|
+
nanoseconds += 1e9;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return [seconds, nanoseconds];
|
|
68
|
+
};
|
|
69
|
+
const NS_PER_SEC = 1e9;
|
|
70
|
+
_hrtime.bigint = (time?: [number, number]): bigint => {
|
|
71
|
+
const diff = _hrtime(time);
|
|
72
|
+
return BigInt(diff[0] * NS_PER_SEC + diff[1]);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export default typeof process === "undefined" ||
|
|
76
|
+
typeof process.hrtime === "undefined"
|
|
77
|
+
? _hrtime
|
|
78
|
+
: process.hrtime;
|
package/src/index.ts
CHANGED
|
@@ -1,103 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export class AbortError extends Error {
|
|
8
|
-
constructor(message?: string) {
|
|
9
|
-
super(message);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export const delay = (ms: number, options?: { signal?: AbortSignal }) => {
|
|
13
|
-
return new Promise<void>((res, rej) => {
|
|
14
|
-
function handleAbort() {
|
|
15
|
-
clearTimeout(timer);
|
|
16
|
-
rej(new AbortError());
|
|
17
|
-
}
|
|
18
|
-
options?.signal?.addEventListener("abort", handleAbort);
|
|
19
|
-
const timer = setTimeout(() => {
|
|
20
|
-
options?.signal?.removeEventListener("abort", handleAbort);
|
|
21
|
-
res();
|
|
22
|
-
}, ms);
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const createTimeoutError = (options: { timeoutMessage?: string }) =>
|
|
27
|
-
new TimeoutError(
|
|
28
|
-
options?.timeoutMessage
|
|
29
|
-
? "Timed out: " + options?.timeoutMessage
|
|
30
|
-
: "Timed out"
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
export const waitFor = async <T>(
|
|
34
|
-
fn: () => T | Promise<T>,
|
|
35
|
-
options: {
|
|
36
|
-
timeout?: number;
|
|
37
|
-
signal?: AbortSignal;
|
|
38
|
-
delayInterval?: number;
|
|
39
|
-
timeoutMessage?: string;
|
|
40
|
-
} = { timeout: 10 * 1000, delayInterval: 100 }
|
|
41
|
-
): Promise<T | undefined> => {
|
|
42
|
-
const delayInterval = options.delayInterval || 100;
|
|
43
|
-
const timeout = options.timeout || 10 * 1000;
|
|
44
|
-
const startTime = +new Date();
|
|
45
|
-
let stop = false;
|
|
46
|
-
|
|
47
|
-
const handleAbort = () => {
|
|
48
|
-
stop = true;
|
|
49
|
-
options.signal?.removeEventListener("abort", handleAbort);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
options.signal?.addEventListener("abort", handleAbort);
|
|
53
|
-
while (!stop && +new Date() - startTime < timeout) {
|
|
54
|
-
const result = await fn();
|
|
55
|
-
if (result) {
|
|
56
|
-
options.signal?.removeEventListener("abort", handleAbort);
|
|
57
|
-
return result;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
await delay(delayInterval, options);
|
|
61
|
-
}
|
|
62
|
-
throw createTimeoutError(options);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export const waitForResolved = async <T>(
|
|
66
|
-
fn: () => T | Promise<T>,
|
|
67
|
-
options: {
|
|
68
|
-
timeout?: number;
|
|
69
|
-
signal?: AbortSignal;
|
|
70
|
-
delayInterval?: number;
|
|
71
|
-
timeoutMessage?: string;
|
|
72
|
-
} = { timeout: 10 * 1000, delayInterval: 50 }
|
|
73
|
-
): Promise<T> => {
|
|
74
|
-
const delayInterval = options.delayInterval || 50;
|
|
75
|
-
const timeout = options.timeout || 10 * 1000;
|
|
76
|
-
|
|
77
|
-
const startTime = +new Date();
|
|
78
|
-
let stop = false;
|
|
79
|
-
let lastError: Error | undefined;
|
|
80
|
-
|
|
81
|
-
const handleAbort = () => {
|
|
82
|
-
stop = true;
|
|
83
|
-
options.signal?.removeEventListener("abort", handleAbort);
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
options.signal?.addEventListener("abort", handleAbort);
|
|
87
|
-
while (!stop && +new Date() - startTime < timeout) {
|
|
88
|
-
try {
|
|
89
|
-
const result = await fn();
|
|
90
|
-
options.signal?.removeEventListener("abort", handleAbort);
|
|
91
|
-
return result;
|
|
92
|
-
} catch (error: any) {
|
|
93
|
-
if (error instanceof AbortError === false) {
|
|
94
|
-
lastError = error;
|
|
95
|
-
} else {
|
|
96
|
-
throw error;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
await delay(delayInterval, options);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
throw lastError || createTimeoutError(options);
|
|
103
|
-
};
|
|
1
|
+
export * from "./wait.js";
|
|
2
|
+
export * from "./metrics.js";
|
|
3
|
+
import hrtime from "./hrtime.js";
|
|
4
|
+
export { hrtime };
|
package/src/metrics.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import hrtime from "./hrtime.js";
|
|
2
|
+
|
|
3
|
+
export class MovingAverageTracker {
|
|
4
|
+
private lastTS: bigint;
|
|
5
|
+
|
|
6
|
+
value = 0;
|
|
7
|
+
|
|
8
|
+
constructor(readonly tau = 10) {
|
|
9
|
+
this.lastTS = hrtime.bigint();
|
|
10
|
+
}
|
|
11
|
+
add(number: number) {
|
|
12
|
+
const now = hrtime.bigint();
|
|
13
|
+
const diff = Number(now - this.lastTS);
|
|
14
|
+
const dt = diff / 1e9;
|
|
15
|
+
this.lastTS = now;
|
|
16
|
+
const alpha_t = 1 - Math.exp(-dt / this.tau);
|
|
17
|
+
this.value = (1 - alpha_t) * this.value + (alpha_t * number) / dt;
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/wait.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export class TimeoutError extends Error {
|
|
2
|
+
constructor(message?: string) {
|
|
3
|
+
super(message);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class AbortError extends Error {
|
|
8
|
+
constructor(message?: string) {
|
|
9
|
+
super(message);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export const delay = (ms: number, options?: { signal?: AbortSignal }) => {
|
|
13
|
+
return new Promise<void>((res, rej) => {
|
|
14
|
+
function handleAbort() {
|
|
15
|
+
clearTimeout(timer);
|
|
16
|
+
rej(new AbortError());
|
|
17
|
+
}
|
|
18
|
+
options?.signal?.addEventListener("abort", handleAbort);
|
|
19
|
+
const timer = setTimeout(() => {
|
|
20
|
+
options?.signal?.removeEventListener("abort", handleAbort);
|
|
21
|
+
res();
|
|
22
|
+
}, ms);
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const createTimeoutError = (options: { timeoutMessage?: string }) =>
|
|
27
|
+
new TimeoutError(
|
|
28
|
+
options?.timeoutMessage
|
|
29
|
+
? "Timed out: " + options?.timeoutMessage
|
|
30
|
+
: "Timed out"
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
export const waitFor = async <T>(
|
|
34
|
+
fn: () => T | Promise<T>,
|
|
35
|
+
options: {
|
|
36
|
+
timeout?: number;
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
delayInterval?: number;
|
|
39
|
+
timeoutMessage?: string;
|
|
40
|
+
} = { timeout: 10 * 1000, delayInterval: 100 }
|
|
41
|
+
): Promise<T | undefined> => {
|
|
42
|
+
const delayInterval = options.delayInterval || 100;
|
|
43
|
+
const timeout = options.timeout || 10 * 1000;
|
|
44
|
+
const startTime = +new Date();
|
|
45
|
+
let stop = false;
|
|
46
|
+
|
|
47
|
+
const handleAbort = () => {
|
|
48
|
+
stop = true;
|
|
49
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
53
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
54
|
+
const result = await fn();
|
|
55
|
+
if (result) {
|
|
56
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
await delay(delayInterval, options);
|
|
61
|
+
}
|
|
62
|
+
throw createTimeoutError(options);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const waitForResolved = async <T>(
|
|
66
|
+
fn: () => T | Promise<T>,
|
|
67
|
+
options: {
|
|
68
|
+
timeout?: number;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
delayInterval?: number;
|
|
71
|
+
timeoutMessage?: string;
|
|
72
|
+
} = { timeout: 10 * 1000, delayInterval: 50 }
|
|
73
|
+
): Promise<T> => {
|
|
74
|
+
const delayInterval = options.delayInterval || 50;
|
|
75
|
+
const timeout = options.timeout || 10 * 1000;
|
|
76
|
+
|
|
77
|
+
const startTime = +new Date();
|
|
78
|
+
let stop = false;
|
|
79
|
+
let lastError: Error | undefined;
|
|
80
|
+
|
|
81
|
+
const handleAbort = () => {
|
|
82
|
+
stop = true;
|
|
83
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
options.signal?.addEventListener("abort", handleAbort);
|
|
87
|
+
while (!stop && +new Date() - startTime < timeout) {
|
|
88
|
+
try {
|
|
89
|
+
const result = await fn();
|
|
90
|
+
options.signal?.removeEventListener("abort", handleAbort);
|
|
91
|
+
return result;
|
|
92
|
+
} catch (error: any) {
|
|
93
|
+
if (error instanceof AbortError === false) {
|
|
94
|
+
lastError = error;
|
|
95
|
+
} else {
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
await delay(delayInterval, options);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
throw lastError || createTimeoutError(options);
|
|
103
|
+
};
|