@bytecodealliance/preview3-shim 0.1.1 → 0.1.2
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/nodejs/clocks.js +14 -16
- package/package.json +1 -1
package/lib/nodejs/clocks.js
CHANGED
|
@@ -27,6 +27,7 @@ export const systemClock = {
|
|
|
27
27
|
const { subscribeInstant, subscribeDuration, ...baseMonotonicClock } = monotonicClockV2;
|
|
28
28
|
|
|
29
29
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
30
|
+
const nsToTimeoutMs = (ns) => (ns + 999_999n) / 1_000_000n;
|
|
30
31
|
|
|
31
32
|
export const monotonicClock = {
|
|
32
33
|
...baseMonotonicClock,
|
|
@@ -58,19 +59,21 @@ export const monotonicClock = {
|
|
|
58
59
|
* @throws {TypeError} If targetNs is not a bigint
|
|
59
60
|
*/
|
|
60
61
|
async waitUntil(targetNs) {
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
while (true) {
|
|
63
|
+
const nowNs = monotonicClock.now();
|
|
64
|
+
const diffNs = targetNs - nowNs;
|
|
63
65
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
if (diffNs <= 0n) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
67
69
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
const ms = nsToTimeoutMs(diffNs);
|
|
71
|
+
if (ms > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
72
|
+
throw new TypeError(`Cannot wait for ${targetNs} ns, exceeds maximum safe integer`);
|
|
73
|
+
}
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
await sleep(Number(ms));
|
|
76
|
+
}
|
|
74
77
|
},
|
|
75
78
|
|
|
76
79
|
/**
|
|
@@ -90,11 +93,6 @@ export const monotonicClock = {
|
|
|
90
93
|
return;
|
|
91
94
|
}
|
|
92
95
|
|
|
93
|
-
|
|
94
|
-
if (ms > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
95
|
-
throw new TypeError(`Cannot wait for ${durationNs} ns, exceeds maximum safe integer`);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
await sleep(Number(ms));
|
|
96
|
+
await monotonicClock.waitUntil(monotonicClock.now() + durationNs);
|
|
99
97
|
},
|
|
100
98
|
};
|
package/package.json
CHANGED