@agoric/client-utils 0.1.1-dev-8fb6eaf.0 → 0.1.1-dev-cb12196.0

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.
Files changed (2) hide show
  1. package/package.json +7 -7
  2. package/src/clock-timer.js +50 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/client-utils",
3
- "version": "0.1.1-dev-8fb6eaf.0+8fb6eaf",
3
+ "version": "0.1.1-dev-cb12196.0+cb12196",
4
4
  "description": "Utilities for building Agoric clients",
5
5
  "license": "Apache-2.0",
6
6
  "publishConfig": {
@@ -27,11 +27,11 @@
27
27
  "ts-blank-space": "^0.4.4"
28
28
  },
29
29
  "dependencies": {
30
- "@agoric/casting": "0.4.3-dev-8fb6eaf.0+8fb6eaf",
31
- "@agoric/ertp": "0.16.3-dev-8fb6eaf.0+8fb6eaf",
32
- "@agoric/internal": "0.3.3-dev-8fb6eaf.0+8fb6eaf",
33
- "@agoric/smart-wallet": "0.5.4-dev-8fb6eaf.0+8fb6eaf",
34
- "@agoric/vats": "0.15.2-dev-8fb6eaf.0+8fb6eaf",
30
+ "@agoric/casting": "0.4.3-dev-cb12196.0+cb12196",
31
+ "@agoric/ertp": "0.16.3-dev-cb12196.0+cb12196",
32
+ "@agoric/internal": "0.3.3-dev-cb12196.0+cb12196",
33
+ "@agoric/smart-wallet": "0.5.4-dev-cb12196.0+cb12196",
34
+ "@agoric/vats": "0.15.2-dev-cb12196.0+cb12196",
35
35
  "@cosmjs/stargate": "^0.32.3",
36
36
  "@cosmjs/tendermint-rpc": "^0.32.3",
37
37
  "@endo/common": "^1.2.8",
@@ -58,5 +58,5 @@
58
58
  ],
59
59
  "timeout": "20m"
60
60
  },
61
- "gitHead": "8fb6eaf1945eaccbd5d0d445566a4fbce0cacd47"
61
+ "gitHead": "cb12196e58c3809ce7d997fa29399c4bf0e4393b"
62
62
  }
@@ -0,0 +1,50 @@
1
+ const { freeze } = Object;
2
+
3
+ /**
4
+ * @typedef {object} IntervalIO
5
+ * @property {typeof setTimeout} setTimeout
6
+ * @property {typeof clearTimeout} clearTimeout
7
+ * @property {typeof Date.now} now
8
+ */
9
+
10
+ /**
11
+ * Creates an async iterable that emits values at specified intervals.
12
+ * @param {number} intervalMs - The interval duration in milliseconds.
13
+ * @param {IntervalIO} io
14
+ * @returns {{
15
+ * [Symbol.asyncIterator]: () => AsyncGenerator<number, void, void>
16
+ * }}
17
+ */
18
+ export const makeIntervalIterable = (
19
+ intervalMs,
20
+ { setTimeout, clearTimeout, now },
21
+ ) => {
22
+ const self = freeze({
23
+ /**
24
+ * The async generator implementation.
25
+ * @returns {AsyncGenerator<number, void, void>}
26
+ */
27
+ async *[Symbol.asyncIterator]() {
28
+ let timeoutId;
29
+ /** @type {undefined | ((x: number) => void) } */
30
+ let resolveNext;
31
+
32
+ await null;
33
+ try {
34
+ for (;;) {
35
+ timeoutId = setTimeout(() => {
36
+ // Promise.withResovers() would obviate this check
37
+ if (resolveNext) {
38
+ resolveNext(now());
39
+ }
40
+ }, intervalMs);
41
+ yield await new Promise(resolve => (resolveNext = resolve));
42
+ }
43
+ } finally {
44
+ // Ensure cleanup on completion
45
+ clearTimeout(timeoutId);
46
+ }
47
+ },
48
+ });
49
+ return self;
50
+ };