@agoric/time 0.3.3-dev-7cc5def.0 → 0.3.3-u11.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.
- package/CHANGELOG.md +45 -0
- package/package.json +10 -11
- package/src/timeMath.js +0 -272
- package/src/typeGuards.js +0 -23
- package/src/types.d.ts +0 -308
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
### [0.3.3-u11.0](https://github.com/Agoric/agoric-sdk/compare/@agoric/time@0.3.2...@agoric/time@0.3.3-u11.0) (2023-08-24)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @agoric/time
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### [0.3.2](https://github.com/Agoric/agoric-sdk/compare/@agoric/time@0.3.1...@agoric/time@0.3.2) (2023-06-02)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @agoric/time
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### [0.3.1](https://github.com/Agoric/agoric-sdk/compare/@agoric/time@0.3.0...@agoric/time@0.3.1) (2023-05-24)
|
|
23
|
+
|
|
24
|
+
**Note:** Version bump only for package @agoric/time
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
## 0.3.0 (2023-05-19)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### Features
|
|
34
|
+
|
|
35
|
+
* **auction:** add an auctioneer to manage vault liquidation ([#7000](https://github.com/Agoric/agoric-sdk/issues/7000)) ([398b70f](https://github.com/Agoric/agoric-sdk/commit/398b70f7e028f957afc1582f0ee31eb2574c94d0)), closes [#6992](https://github.com/Agoric/agoric-sdk/issues/6992) [#7047](https://github.com/Agoric/agoric-sdk/issues/7047) [#7074](https://github.com/Agoric/agoric-sdk/issues/7074)
|
|
36
|
+
* create new @agoric/time package ([a61a3fb](https://github.com/Agoric/agoric-sdk/commit/a61a3fbb7a5ccfe07c715a310baa88ada8e572b2)), closes [#6003](https://github.com/Agoric/agoric-sdk/issues/6003)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Bug Fixes
|
|
40
|
+
|
|
41
|
+
* **time:** TimerService now returns branded TimestampRecord ([9137e9c](https://github.com/Agoric/agoric-sdk/commit/9137e9cab6f459c876b1a2ad8e681be7224749ce)), closes [#6003](https://github.com/Agoric/agoric-sdk/issues/6003)
|
|
42
|
+
* clean up types ([6f53f19](https://github.com/Agoric/agoric-sdk/commit/6f53f1915ce21e65fefc2fff900b7d4b947be6b1))
|
|
43
|
+
* move timer files to new package ([c105bde](https://github.com/Agoric/agoric-sdk/commit/c105bdefff2527a90b3c6b9d80d0462944dd51c3))
|
|
44
|
+
* TimerBrand has isMyTimerService(), not isMyTimer() ([9f4e867](https://github.com/Agoric/agoric-sdk/commit/9f4e8670694504ebbd451c8840f900a1a24b902f))
|
|
45
|
+
* **time:** fix the code/test to work in its new home ([504d333](https://github.com/Agoric/agoric-sdk/commit/504d3335cf632cc50e079fb27a82db604318bd4a))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agoric/time",
|
|
3
|
-
"version": "0.3.3-
|
|
3
|
+
"version": "0.3.3-u11.0",
|
|
4
4
|
"description": "Timestamps, time math, timer service API definition",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"test": "ava",
|
|
13
13
|
"test:xs": "exit 0",
|
|
14
14
|
"lint": "run-s --continue-on-error lint:*",
|
|
15
|
-
"lint:types": "tsc",
|
|
15
|
+
"lint:types": "tsc -p jsconfig.json",
|
|
16
16
|
"lint:eslint": "eslint .",
|
|
17
17
|
"lint-fix": "yarn lint:eslint --fix"
|
|
18
18
|
},
|
|
@@ -30,22 +30,21 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/Agoric/agoric-sdk#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@agoric/assert": "0.6.
|
|
34
|
-
"@
|
|
35
|
-
"@endo/
|
|
33
|
+
"@agoric/assert": "^0.6.0",
|
|
34
|
+
"@agoric/store": "^0.9.3-u11.0",
|
|
35
|
+
"@endo/nat": "^4.1.27"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@endo/far": "^0.2.
|
|
39
|
-
"@endo/init": "^0.5.
|
|
40
|
-
"ava": "^5.
|
|
38
|
+
"@endo/far": "^0.2.18",
|
|
39
|
+
"@endo/init": "^0.5.56",
|
|
40
|
+
"ava": "^5.2.0"
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"*.js",
|
|
44
|
-
"NEWS.md"
|
|
45
|
-
"src"
|
|
44
|
+
"NEWS.md"
|
|
46
45
|
],
|
|
47
46
|
"publishConfig": {
|
|
48
47
|
"access": "public"
|
|
49
48
|
},
|
|
50
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "92b6cd72484079b0349d8ccfa4510aeb820e8d67"
|
|
51
50
|
}
|
package/src/timeMath.js
DELETED
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import { Nat } from '@endo/nat';
|
|
2
|
-
import { mustMatch } from '@endo/patterns';
|
|
3
|
-
import { RelativeTimeRecordShape, TimestampRecordShape } from './typeGuards.js';
|
|
4
|
-
|
|
5
|
-
const { Fail, quote: q } = assert;
|
|
6
|
-
/**
|
|
7
|
-
* @typedef {import('./types').TimerBrand} TimerBrand
|
|
8
|
-
* @typedef {import('./types').Timestamp} Timestamp
|
|
9
|
-
* @typedef {import('./types').RelativeTime} RelativeTime
|
|
10
|
-
* @typedef {import('./types').RelativeTimeValue} RelativeTimeValue
|
|
11
|
-
* @typedef {import('./types').TimestampValue} TimestampValue
|
|
12
|
-
* @typedef {import('./types').TimeMathType} TimeMathType
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* `agreedTimerBrand` is internal to this module.
|
|
18
|
-
*
|
|
19
|
-
* @param {TimerBrand | undefined} leftBrand
|
|
20
|
-
* @param {TimerBrand | undefined} rightBrand
|
|
21
|
-
* @returns {TimerBrand | undefined}
|
|
22
|
-
*/
|
|
23
|
-
const agreedTimerBrand = (leftBrand, rightBrand) => {
|
|
24
|
-
if (leftBrand === undefined) {
|
|
25
|
-
if (rightBrand === undefined) {
|
|
26
|
-
return undefined;
|
|
27
|
-
} else {
|
|
28
|
-
return rightBrand;
|
|
29
|
-
}
|
|
30
|
-
} else if (rightBrand === undefined) {
|
|
31
|
-
return leftBrand;
|
|
32
|
-
} else {
|
|
33
|
-
leftBrand === rightBrand ||
|
|
34
|
-
Fail`TimerBrands must match: ${q(leftBrand)} vs ${q(rightBrand)}`;
|
|
35
|
-
return leftBrand;
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* `sharedTimerBrand` is internal to this module, and implements the
|
|
41
|
-
* transitional brand checking and contaigion logic explained in the `TimeMath`
|
|
42
|
-
* comment. It is used to define the binary operators that should follow
|
|
43
|
-
* this logic. It does the error checking between the operands, and returns
|
|
44
|
-
* the brand, if any, that should label the resulting time value.
|
|
45
|
-
*
|
|
46
|
-
* @param {Timestamp | RelativeTime} left
|
|
47
|
-
* @param {Timestamp | RelativeTime} right
|
|
48
|
-
* @returns {TimerBrand | undefined}
|
|
49
|
-
*/
|
|
50
|
-
const sharedTimerBrand = (left, right) => {
|
|
51
|
-
const leftBrand = typeof left === 'bigint' ? undefined : left.timerBrand;
|
|
52
|
-
const rightBrand = typeof right === 'bigint' ? undefined : right.timerBrand;
|
|
53
|
-
return agreedTimerBrand(leftBrand, rightBrand);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* `absLike` is internal to this module, and used to implement the binary
|
|
58
|
-
* operators in the case where the returned time should be a `Timestamp`
|
|
59
|
-
* rather than a `RelativeTime`.
|
|
60
|
-
*
|
|
61
|
-
* @param {Timestamp | RelativeTime} left
|
|
62
|
-
* @param {Timestamp | RelativeTime} right
|
|
63
|
-
* @param {TimestampValue} absValue
|
|
64
|
-
* @returns {Timestamp}
|
|
65
|
-
*/
|
|
66
|
-
const absLike = (left, right, absValue) => {
|
|
67
|
-
Nat(absValue);
|
|
68
|
-
const timerBrand = sharedTimerBrand(left, right);
|
|
69
|
-
if (timerBrand) {
|
|
70
|
-
return harden({
|
|
71
|
-
timerBrand,
|
|
72
|
-
absValue,
|
|
73
|
-
});
|
|
74
|
-
} else {
|
|
75
|
-
return absValue;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* `relLike` is internal to this module, and used to implement the binary
|
|
81
|
-
* operators in the case where the returned time should be a `RelativeTime`
|
|
82
|
-
* rather than a `Timestamp`.
|
|
83
|
-
*
|
|
84
|
-
* @param {Timestamp | RelativeTime} left
|
|
85
|
-
* @param {Timestamp | RelativeTime} right
|
|
86
|
-
* @param {RelativeTimeValue} relValue
|
|
87
|
-
* @returns {RelativeTime}
|
|
88
|
-
*/
|
|
89
|
-
const relLike = (left, right, relValue) => {
|
|
90
|
-
Nat(relValue);
|
|
91
|
-
const timerBrand = sharedTimerBrand(left, right);
|
|
92
|
-
if (timerBrand) {
|
|
93
|
-
return harden({
|
|
94
|
-
timerBrand,
|
|
95
|
-
relValue,
|
|
96
|
-
});
|
|
97
|
-
} else {
|
|
98
|
-
return relValue;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
// For all the following time operators, their documentation is in
|
|
103
|
-
// the `TimeMathType`, since that is the documentation that shows up
|
|
104
|
-
// in the IDE. Well, at least the vscode IDE.
|
|
105
|
-
|
|
106
|
-
const absValue = abs => {
|
|
107
|
-
if (typeof abs === 'bigint') {
|
|
108
|
-
return Nat(abs);
|
|
109
|
-
}
|
|
110
|
-
mustMatch(abs, TimestampRecordShape, 'timestamp');
|
|
111
|
-
return Nat(abs.absValue);
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const relValue = rel => {
|
|
115
|
-
if (typeof rel === 'bigint') {
|
|
116
|
-
return Nat(rel);
|
|
117
|
-
}
|
|
118
|
-
mustMatch(rel, RelativeTimeRecordShape, 'relative');
|
|
119
|
-
return Nat(rel.relValue);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const makeTimestampRecord = (abs, timerBrand) =>
|
|
123
|
-
harden({ absValue: abs, timerBrand });
|
|
124
|
-
const makeRelativeTimeRecord = (rel, timerBrand) =>
|
|
125
|
-
harden({ relValue: rel, timerBrand });
|
|
126
|
-
|
|
127
|
-
const coerceTimestampRecord = (ts, brand) => {
|
|
128
|
-
brand || Fail`must have a brand`;
|
|
129
|
-
if (typeof ts === 'number') {
|
|
130
|
-
ts = Nat(ts);
|
|
131
|
-
}
|
|
132
|
-
if (typeof ts === 'bigint') {
|
|
133
|
-
return makeTimestampRecord(ts, brand);
|
|
134
|
-
} else {
|
|
135
|
-
const { timerBrand } = ts;
|
|
136
|
-
mustMatch(ts, TimestampRecordShape, 'timestamp');
|
|
137
|
-
agreedTimerBrand(timerBrand, brand);
|
|
138
|
-
return ts;
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
const coerceRelativeTimeRecord = (rt, brand) => {
|
|
143
|
-
brand || Fail`must have a brand`;
|
|
144
|
-
if (typeof rt === 'number') {
|
|
145
|
-
rt = Nat(rt);
|
|
146
|
-
}
|
|
147
|
-
if (typeof rt === 'bigint') {
|
|
148
|
-
return makeRelativeTimeRecord(rt, brand);
|
|
149
|
-
} else {
|
|
150
|
-
const { timerBrand } = rt;
|
|
151
|
-
mustMatch(rt, RelativeTimeRecordShape, 'relativeTime');
|
|
152
|
-
agreedTimerBrand(timerBrand, brand);
|
|
153
|
-
return rt;
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const addAbsRel = (abs, rel) =>
|
|
158
|
-
absLike(abs, rel, absValue(abs) + relValue(rel));
|
|
159
|
-
|
|
160
|
-
const addRelRel = (rel1, rel2) =>
|
|
161
|
-
relLike(rel1, rel2, relValue(rel1) + relValue(rel2));
|
|
162
|
-
|
|
163
|
-
const subtractAbsAbs = (abs1, abs2) =>
|
|
164
|
-
relLike(abs1, abs2, absValue(abs1) - absValue(abs2));
|
|
165
|
-
|
|
166
|
-
const clampedSubtractAbsAbs = (abs1, abs2) => {
|
|
167
|
-
const val1 = absValue(abs1);
|
|
168
|
-
const val2 = absValue(abs2);
|
|
169
|
-
return relLike(abs1, abs2, val1 > val2 ? val1 - val2 : 0n);
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
const subtractAbsRel = (abs, rel) =>
|
|
173
|
-
absLike(abs, rel, absValue(abs) - relValue(rel));
|
|
174
|
-
|
|
175
|
-
const subtractRelRel = (rel1, rel2) =>
|
|
176
|
-
relLike(rel1, rel2, relValue(rel1) - relValue(rel2));
|
|
177
|
-
|
|
178
|
-
const isRelZero = rel => relValue(rel) === 0n;
|
|
179
|
-
|
|
180
|
-
const multiplyRelNat = (rel, nat) => relLike(rel, nat, relValue(rel) * nat);
|
|
181
|
-
|
|
182
|
-
const divideRelNat = (rel, nat) => relLike(rel, nat, relValue(rel) / nat);
|
|
183
|
-
|
|
184
|
-
const divideRelRel = (rel1, rel2) => {
|
|
185
|
-
sharedTimerBrand(rel1, rel2); // just error check
|
|
186
|
-
return relValue(rel1) / relValue(rel2);
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
const modAbsRel = (abs, step) =>
|
|
190
|
-
relLike(abs, step, absValue(abs) % relValue(step));
|
|
191
|
-
|
|
192
|
-
const modRelRel = (rel, step) =>
|
|
193
|
-
relLike(rel, step, relValue(rel) % relValue(step));
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* `compareValues` is internal to this module, and used to implement
|
|
197
|
-
* the time comparison operators.
|
|
198
|
-
*
|
|
199
|
-
* @param {Timestamp | RelativeTime} left
|
|
200
|
-
* @param {Timestamp | RelativeTime} right
|
|
201
|
-
* @param {bigint} v1
|
|
202
|
-
* @param {bigint} v2
|
|
203
|
-
* @returns {RankComparison}
|
|
204
|
-
*/
|
|
205
|
-
const compareValues = (left, right, v1, v2) => {
|
|
206
|
-
sharedTimerBrand(left, right);
|
|
207
|
-
if (v1 < v2) {
|
|
208
|
-
return -1;
|
|
209
|
-
} else if (v1 === v2) {
|
|
210
|
-
return 0;
|
|
211
|
-
} else {
|
|
212
|
-
assert(v1 > v2);
|
|
213
|
-
return 1;
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* The `TimeMath` object provides helper methods to do arithmetic on labeled
|
|
219
|
-
* time values, much like `AmountMath` provides helper methods to do arithmetic
|
|
220
|
-
* on labeled asset/money values. Both check for consistency of labels: a
|
|
221
|
-
* binary operation on two labeled objects ensures that the both carry
|
|
222
|
-
* the same label. If they produce another object from the same domain, it
|
|
223
|
-
* will carry the same label. If the operands have incompatible labels,
|
|
224
|
-
* an error is thrown.
|
|
225
|
-
*
|
|
226
|
-
* Unlike amount arithmetic, time arithmetic deals in two kinds of time objects:
|
|
227
|
-
* Timestamps, which represent absolute time, and RelativeTime, which represents
|
|
228
|
-
* the duration between two absolute times. Both kinds of time object
|
|
229
|
-
* are labeled by a `TimerBrand`. For a Timestamp object, the value is
|
|
230
|
-
* a bigint in an `absValue` property. For a RelativeTime object, the value
|
|
231
|
-
* is a bigint in a `relValue` property. Thus we have a runtime safety check
|
|
232
|
-
* to ensure that we don't confused the two, even if we have managed to fool
|
|
233
|
-
* the (unsound) static type system.
|
|
234
|
-
*
|
|
235
|
-
* As a transitional measure, currently many Timestamps and RelativeTimes are
|
|
236
|
-
* still represented by unlabeled bigints. During this transitional period,
|
|
237
|
-
* we allow this, both statically and dynamically. For a normal binary
|
|
238
|
-
* operation, if both inputs are labeled, then we do the full checking as
|
|
239
|
-
* explained above and return a labeled result. If both inputs are unlabeled
|
|
240
|
-
* bigints, we *assume* that they indicate a time of the right kind
|
|
241
|
-
* (Timestamp vs RelativeTime) and timer brand. Since we don't know what
|
|
242
|
-
* brand was intended, we can only return yet another unlabeled bigint.
|
|
243
|
-
*
|
|
244
|
-
* If one operand is labeled and the other is not, we check the labeled operand,
|
|
245
|
-
* *assume* the unlabeled bigint represents the value needed for the other
|
|
246
|
-
* operand, and return a labeled time object with the brand of the labeled
|
|
247
|
-
* operand.
|
|
248
|
-
*
|
|
249
|
-
* @type {TimeMathType}
|
|
250
|
-
*/
|
|
251
|
-
export const TimeMath = harden({
|
|
252
|
-
absValue,
|
|
253
|
-
relValue,
|
|
254
|
-
coerceTimestampRecord,
|
|
255
|
-
coerceRelativeTimeRecord,
|
|
256
|
-
addAbsRel,
|
|
257
|
-
addRelRel,
|
|
258
|
-
subtractAbsAbs,
|
|
259
|
-
clampedSubtractAbsAbs,
|
|
260
|
-
subtractAbsRel,
|
|
261
|
-
subtractRelRel,
|
|
262
|
-
isRelZero,
|
|
263
|
-
multiplyRelNat,
|
|
264
|
-
divideRelNat,
|
|
265
|
-
divideRelRel,
|
|
266
|
-
modAbsRel,
|
|
267
|
-
modRelRel,
|
|
268
|
-
compareAbs: (abs1, abs2) =>
|
|
269
|
-
compareValues(abs1, abs2, absValue(abs1), absValue(abs2)),
|
|
270
|
-
compareRel: (rel1, rel2) =>
|
|
271
|
-
compareValues(rel1, rel2, relValue(rel1), relValue(rel2)),
|
|
272
|
-
});
|
package/src/typeGuards.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { M } from '@endo/patterns';
|
|
2
|
-
|
|
3
|
-
export const TimerBrandShape = M.remotable('TimerBrand');
|
|
4
|
-
export const TimestampValueShape = M.nat();
|
|
5
|
-
export const RelativeTimeValueShape = M.nat(); // Should we allow negatives?
|
|
6
|
-
|
|
7
|
-
export const TimestampRecordShape = harden({
|
|
8
|
-
timerBrand: TimerBrandShape,
|
|
9
|
-
absValue: TimestampValueShape,
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
export const RelativeTimeRecordShape = harden({
|
|
13
|
-
timerBrand: TimerBrandShape,
|
|
14
|
-
relValue: RelativeTimeValueShape,
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
export const TimestampShape = M.or(TimestampRecordShape, TimestampValueShape);
|
|
18
|
-
export const RelativeTimeShape = M.or(
|
|
19
|
-
RelativeTimeRecordShape,
|
|
20
|
-
RelativeTimeValueShape,
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
export const TimerServiceShape = M.remotable('TimerService');
|
package/src/types.d.ts
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-use-before-define, no-undef */
|
|
2
|
-
import type { ERef } from '@endo/eventual-send';
|
|
3
|
-
|
|
4
|
-
import type { RankComparison } from '@agoric/store';
|
|
5
|
-
|
|
6
|
-
/// <reference types="@agoric/notifier/src/types.js"/>
|
|
7
|
-
|
|
8
|
-
// These aren't in the global runtime environment. They are just types that are
|
|
9
|
-
// meant to be globally accessible as a side-effect of importing this module.
|
|
10
|
-
/**
|
|
11
|
-
* The TimerBrand is a unique object that represents the kind of Time
|
|
12
|
-
* used in Timestamp/RelativeTime records. Time from different sources
|
|
13
|
-
* is not comparable.
|
|
14
|
-
*
|
|
15
|
-
* Do not call `isMyTimerService(myTimerService)` on an untrusted
|
|
16
|
-
* brand, because that will leak your closely-held timer authority. If
|
|
17
|
-
* the goal is to check the suitability of a client-provided
|
|
18
|
-
* Timestamp, use coerceTimestampRecord() or add/subtract it to a
|
|
19
|
-
* known-good Timestamp, or extact its brand and === against
|
|
20
|
-
* `timerService.getTimerBrand()`.
|
|
21
|
-
*
|
|
22
|
-
* TODO Not all Timestamps are labeled with the TimerBrand (in much
|
|
23
|
-
* the same way that `Amounts` are asset/money values labeled by
|
|
24
|
-
* `Brands`), but the SwingSet vat-timer TimerService will use branded
|
|
25
|
-
* TimestampRecord/RelativeTimeRecord in all messages it emits. Also,
|
|
26
|
-
* a `TimerService` is still used everywhere a `TimerBrand` is called
|
|
27
|
-
* for.
|
|
28
|
-
*
|
|
29
|
-
* See https://github.com/Agoric/agoric-sdk/issues/5798
|
|
30
|
-
* and https://github.com/Agoric/agoric-sdk/pull/5821
|
|
31
|
-
*/
|
|
32
|
-
export type TimerBrand = {
|
|
33
|
-
isMyTimerService: (timer: TimerService) => ERef<boolean>;
|
|
34
|
-
isMyClock: (clock: Clock) => ERef<boolean>;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @deprecated use TimestampRecord
|
|
39
|
-
*
|
|
40
|
-
* An absolute time returned by a TimerService. Note that different timer
|
|
41
|
-
* services may have different interpretations of actual TimestampValue values.
|
|
42
|
-
* Will generally be a count of some number of units starting at some starting
|
|
43
|
-
* point. But what the starting point is and what units are counted is purely up
|
|
44
|
-
* to the meaning of that particular TimerService
|
|
45
|
-
*/
|
|
46
|
-
export type TimestampValue = bigint;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @deprecated use RelativeTimeRecord
|
|
50
|
-
*
|
|
51
|
-
* Difference between two TimestampValues. Note that different timer services
|
|
52
|
-
* may have different interpretations of TimestampValues values.
|
|
53
|
-
*/
|
|
54
|
-
export type RelativeTimeValue = bigint;
|
|
55
|
-
|
|
56
|
-
export type TimestampRecord = {
|
|
57
|
-
timerBrand: TimerBrand;
|
|
58
|
-
absValue: bigint;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type RelativeTimeRecord = {
|
|
62
|
-
timerBrand: TimerBrand;
|
|
63
|
-
relValue: bigint;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* @deprecated use TimestampRecord
|
|
68
|
-
*
|
|
69
|
-
* Transitional measure until all are converted to TimestampRecord.
|
|
70
|
-
* See `TimeMath` comment for an explanation of the representation
|
|
71
|
-
* during this transition. After the transition, `Timestamp` will simplify
|
|
72
|
-
* to the current definition of `TimestampRecord`, which will itself
|
|
73
|
-
* be deleted. All Timestamps will then be labeled by TimerBrands.
|
|
74
|
-
*/
|
|
75
|
-
export type Timestamp = TimestampRecord | TimestampValue;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @deprecated use RelativeTimeRecord
|
|
79
|
-
*
|
|
80
|
-
* Transitional measure until all are converted to RelativeTimeRecord
|
|
81
|
-
* See `TimeMath` comment for an explanation of the representation
|
|
82
|
-
* during this transition. After the transition, `RelativeTime` will simplify
|
|
83
|
-
* to the current definition of `RelativeTimeRecord`, which will itself
|
|
84
|
-
* be deleted. All RelativeTimes will then be labeled by TimerBrands.
|
|
85
|
-
*/
|
|
86
|
-
export type RelativeTime = RelativeTimeRecord | RelativeTimeValue;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* A CancelToken is an arbitrary marker object, passed in with
|
|
90
|
-
* each API call that creates a wakeup or repeater, and passed to
|
|
91
|
-
* cancel() to cancel them all.
|
|
92
|
-
*/
|
|
93
|
-
export type CancelToken = object;
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Gives the ability to get the current time,
|
|
97
|
-
* schedule a single wake() call, create a repeater that will allow scheduling
|
|
98
|
-
* of events at regular intervals, or remove scheduled calls.
|
|
99
|
-
*/
|
|
100
|
-
export interface TimerService {
|
|
101
|
-
/**
|
|
102
|
-
* Retrieve the latest timestamp
|
|
103
|
-
*/
|
|
104
|
-
getCurrentTimestamp: () => TimestampRecord;
|
|
105
|
-
/**
|
|
106
|
-
* Return value is the time at which the call is scheduled to take place
|
|
107
|
-
*/
|
|
108
|
-
setWakeup: (
|
|
109
|
-
baseTime: Timestamp,
|
|
110
|
-
waker: ERef<TimerWaker>,
|
|
111
|
-
cancelToken?: CancelToken,
|
|
112
|
-
) => TimestampRecord;
|
|
113
|
-
/**
|
|
114
|
-
* Create and return a promise that will resolve after the absolte
|
|
115
|
-
* time has passed.
|
|
116
|
-
*/
|
|
117
|
-
wakeAt: (
|
|
118
|
-
baseTime: Timestamp,
|
|
119
|
-
cancelToken?: CancelToken,
|
|
120
|
-
) => Promise<TimestampRecord>;
|
|
121
|
-
/**
|
|
122
|
-
* Create and return a promise that will resolve after the relative time has
|
|
123
|
-
* passed.
|
|
124
|
-
*/
|
|
125
|
-
delay: (
|
|
126
|
-
delay: RelativeTime,
|
|
127
|
-
cancelToken?: CancelToken,
|
|
128
|
-
) => Promise<TimestampRecord>;
|
|
129
|
-
/**
|
|
130
|
-
* Create and return a repeater that will schedule `wake()` calls
|
|
131
|
-
* repeatedly at times that are a multiple of interval following delay.
|
|
132
|
-
* Interval is the difference between successive times at which wake will be
|
|
133
|
-
* called. When `schedule(w)` is called, `w.wake()` will be scheduled to be
|
|
134
|
-
* called after the next multiple of interval from the base. Since times can be
|
|
135
|
-
* coarse-grained, the actual call may occur later, but this won't change when
|
|
136
|
-
* the next event will be called.
|
|
137
|
-
*/
|
|
138
|
-
makeRepeater: (
|
|
139
|
-
delay: RelativeTime,
|
|
140
|
-
interval: RelativeTime,
|
|
141
|
-
cancelToken?: CancelToken,
|
|
142
|
-
) => TimerRepeater;
|
|
143
|
-
/**
|
|
144
|
-
* Create a repeater with a handler directly.
|
|
145
|
-
*/
|
|
146
|
-
repeatAfter: (
|
|
147
|
-
delay: RelativeTime,
|
|
148
|
-
interval: RelativeTime,
|
|
149
|
-
handler: TimerWaker,
|
|
150
|
-
cancelToken?: CancelToken,
|
|
151
|
-
) => void;
|
|
152
|
-
/**
|
|
153
|
-
* Create and return a Notifier that will deliver updates repeatedly at times
|
|
154
|
-
* that are a multiple of interval following delay.
|
|
155
|
-
*/
|
|
156
|
-
makeNotifier: (
|
|
157
|
-
delay: RelativeTime,
|
|
158
|
-
interval: RelativeTime,
|
|
159
|
-
cancelToken?: CancelToken,
|
|
160
|
-
) => Notifier<TimestampRecord>;
|
|
161
|
-
/**
|
|
162
|
-
* Cancel a previously-established wakeup or repeater.
|
|
163
|
-
*/
|
|
164
|
-
cancel: (cancelToken: CancelToken) => void;
|
|
165
|
-
/**
|
|
166
|
-
* Retrieve the read-only Clock facet.
|
|
167
|
-
*/
|
|
168
|
-
getClock: () => Clock;
|
|
169
|
-
/**
|
|
170
|
-
* Retrieve the Brand for this timer service.
|
|
171
|
-
*/
|
|
172
|
-
getTimerBrand: () => TimerBrand;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export interface Clock {
|
|
176
|
-
/**
|
|
177
|
-
* Retrieve the latest timestamp
|
|
178
|
-
*/
|
|
179
|
-
getCurrentTimestamp: () => TimestampRecord;
|
|
180
|
-
/**
|
|
181
|
-
* Retrieve the Brand for this timer service.
|
|
182
|
-
*/
|
|
183
|
-
getTimerBrand: () => TimerBrand;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
export interface TimerWaker {
|
|
187
|
-
/**
|
|
188
|
-
* The timestamp passed to `wake()` is the time that the call was scheduled
|
|
189
|
-
* to occur.
|
|
190
|
-
*/
|
|
191
|
-
wake: (timestamp: TimestampRecord) => void;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export interface TimerRepeater {
|
|
195
|
-
/**
|
|
196
|
-
* Returns the time scheduled for
|
|
197
|
-
* the first call to `E(waker).wake()`. The waker will continue to be scheduled
|
|
198
|
-
* every interval until the repeater is disabled.
|
|
199
|
-
*/
|
|
200
|
-
schedule: (waker: ERef<TimerWaker>) => TimestampRecord;
|
|
201
|
-
/**
|
|
202
|
-
* Disable this repeater, so `schedule(w)` can't
|
|
203
|
-
* be called, and wakers already scheduled with this repeater won't be
|
|
204
|
-
* rescheduled again after `E(waker).wake()` is next called on them.
|
|
205
|
-
*/
|
|
206
|
-
disable: () => void;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export type TimeMathType = {
|
|
210
|
-
/**
|
|
211
|
-
* Validates that the operand represents a `Timestamp` and returns the bigint
|
|
212
|
-
* representing its absolute time value.
|
|
213
|
-
* During the transition explained in the`TimeMath` comment,
|
|
214
|
-
* `absValue` will also accept a bigint which it then just returns.
|
|
215
|
-
*/
|
|
216
|
-
absValue: (abs: Timestamp) => TimestampValue;
|
|
217
|
-
/**
|
|
218
|
-
* Validates that the operand represents a `RelativeTime` and returns the
|
|
219
|
-
* bigint representing its relative time value.
|
|
220
|
-
* During the transition explained in the`TimeMath` comment,
|
|
221
|
-
* `relValue` will also accept a bigint which it then just returns.
|
|
222
|
-
*/
|
|
223
|
-
relValue: (rel: RelativeTime) => RelativeTimeValue;
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Coerces to a TimestampRecord if possible, else throws. If the value has a brand, ensure it matches.
|
|
227
|
-
* Return a Timestamp labeled with that brand.
|
|
228
|
-
*/
|
|
229
|
-
coerceTimestampRecord: (
|
|
230
|
-
abs: TimestampRecord | TimestampValue | number,
|
|
231
|
-
brand: TimerBrand,
|
|
232
|
-
) => TimestampRecord;
|
|
233
|
-
/**
|
|
234
|
-
* Coerces to a RelativeTime if possible. If a brand is provided, ensure it
|
|
235
|
-
* matches and return a RelativeTime labeled with that brand.
|
|
236
|
-
*/
|
|
237
|
-
coerceRelativeTimeRecord: (
|
|
238
|
-
rel: RelativeTimeRecord | RelativeTimeValue | number,
|
|
239
|
-
brand: TimerBrand,
|
|
240
|
-
) => RelativeTimeRecord;
|
|
241
|
-
/**
|
|
242
|
-
* An absolute time + a relative time gives a new absolute time.
|
|
243
|
-
*
|
|
244
|
-
* @template {Timestamp} T
|
|
245
|
-
*/
|
|
246
|
-
addAbsRel: (abs: T, rel: RelativeTime) => T;
|
|
247
|
-
/**
|
|
248
|
-
* A relative time (i.e., a duration) + another relative time
|
|
249
|
-
* gives a new relative time.
|
|
250
|
-
*
|
|
251
|
-
* @template {RelativeTime} T
|
|
252
|
-
*/
|
|
253
|
-
addRelRel: (rel1: T, rel2: T) => T;
|
|
254
|
-
/**
|
|
255
|
-
* The difference between two absolute times is a relative time. If abs1 > abs2
|
|
256
|
-
* the difference would be negative, so this method throws instead.
|
|
257
|
-
*/
|
|
258
|
-
subtractAbsAbs: (abs1: Timestamp, abs2: Timestamp) => RelativeTime;
|
|
259
|
-
/**
|
|
260
|
-
* The difference between two absolute times is a relative time. If abs1 > abs2
|
|
261
|
-
* the difference would be negative, so this method returns a zero
|
|
262
|
-
* relative time instead.
|
|
263
|
-
*/
|
|
264
|
-
clampedSubtractAbsAbs: (abs1: Timestamp, abs2: Timestamp) => RelativeTime;
|
|
265
|
-
/**
|
|
266
|
-
* An absolute time - a relative time gives a new absolute time
|
|
267
|
-
*/
|
|
268
|
-
subtractAbsRel: (abs: Timestamp, rel: RelativeTime) => Timestamp;
|
|
269
|
-
/**
|
|
270
|
-
* The difference between two relative times.
|
|
271
|
-
*/
|
|
272
|
-
subtractRelRel: (rel1: RelativeTime, rel2: RelativeTime) => RelativeTime;
|
|
273
|
-
/**
|
|
274
|
-
* Does it represent a zero relative time, i.e., the difference
|
|
275
|
-
* of an absolute time with itself? (We choose not to define a similar
|
|
276
|
-
* isAbsZero, even though we could, because it is much less likely to be
|
|
277
|
-
* meaningful.)
|
|
278
|
-
*/
|
|
279
|
-
isRelZero: (rel: RelativeTime) => boolean;
|
|
280
|
-
multiplyRelNat: (rel: RelativeTime, nat: bigint) => RelativeTime;
|
|
281
|
-
divideRelNat: (rel: RelativeTime, nat: bigint) => RelativeTime;
|
|
282
|
-
divideRelRel: (rel1: RelativeTime, rel2: RelativeTime) => bigint;
|
|
283
|
-
/**
|
|
284
|
-
* An absolute time modulo a relative time is a relative time. For example,
|
|
285
|
-
* 20:17 on July 20, 1969 modulo 1 day is just 20:17, a relative time that
|
|
286
|
-
* can be added to the beginning of any day.
|
|
287
|
-
*/
|
|
288
|
-
modAbsRel: (abs: Timestamp, step: RelativeTime) => RelativeTime;
|
|
289
|
-
/**
|
|
290
|
-
* A relative time modulo a relative time is a relative time. For example,
|
|
291
|
-
* 3.5 hours modulo an hour is 30 minutes.
|
|
292
|
-
*/
|
|
293
|
-
modRelRel: (rel: RelativeTime, step: RelativeTime) => RelativeTime;
|
|
294
|
-
/**
|
|
295
|
-
* Compares two absolute times. This comparison function is compatible
|
|
296
|
-
* with JavaScript's `Array.prototype.sort` and so can be used to sort an
|
|
297
|
-
* array of absolute times. The result is -1, 0, or 1 indicating whether
|
|
298
|
-
* the first argument is less than, equal, or greater than the second.
|
|
299
|
-
*/
|
|
300
|
-
compareAbs: (abs1: Timestamp, abs2: Timestamp) => RankComparison;
|
|
301
|
-
/**
|
|
302
|
-
* Compares two relative times. This comparison function is compatible
|
|
303
|
-
* with JavaScript's `Array.prototype.sort` and so can be used to sort an
|
|
304
|
-
* array of relative times. The result is -1, 0, or 1 indicating whether
|
|
305
|
-
* the first argument is less than, equal, or greater than the second.
|
|
306
|
-
*/
|
|
307
|
-
compareRel: (rel1: RelativeTime, rel2: RelativeTime) => RankComparison;
|
|
308
|
-
};
|