@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.
@@ -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
- const nowNs = monotonicClock.now();
62
- const diffNs = targetNs - nowNs;
62
+ while (true) {
63
+ const nowNs = monotonicClock.now();
64
+ const diffNs = targetNs - nowNs;
63
65
 
64
- if (diffNs <= 0n) {
65
- return;
66
- }
66
+ if (diffNs <= 0n) {
67
+ return;
68
+ }
67
69
 
68
- const ms = diffNs / 1_000_000n;
69
- if (ms > BigInt(Number.MAX_SAFE_INTEGER)) {
70
- throw new TypeError(`Cannot wait for ${targetNs} ns, exceeds maximum safe integer`);
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
- await sleep(Number(ms));
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
- const ms = durationNs / 1_000_000n;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview3-shim",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "WASI Preview3 shim for JS environments",
5
5
  "homepage": "https://github.com/bytecodealliance/jco/blob/main/packages/preview3-shim/#readme",
6
6
  "bugs": {