-
Notifications
You must be signed in to change notification settings - Fork 208
/
tools.js
78 lines (71 loc) · 2.08 KB
/
tools.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// @ts-check
import { makeAsVow } from './vow-utils.js';
import { prepareVowKit } from './vow.js';
import { prepareWatchUtils } from './watch-utils.js';
import { prepareWatch } from './watch.js';
import { makeWhen } from './when.js';
/**
* @import {Zone} from '@agoric/base-zone';
* @import {IsRetryableReason, AsPromiseFunction, EVow, Vow, ERef} from './types.js';
*/
/**
* NB: Not to be used in a Vat. It doesn't know what an upgrade is. For that you
* need `prepareVowTools` from `vat.js`.
*
* @param {Zone} zone
* @param {object} [powers]
* @param {IsRetryableReason} [powers.isRetryableReason]
*/
export const prepareVowTools = (zone, powers = {}) => {
const { isRetryableReason = /** @type {IsRetryableReason} */ (() => false) } =
powers;
const makeVowKit = prepareVowKit(zone);
const when = makeWhen(isRetryableReason);
const watch = prepareWatch(zone, makeVowKit, isRetryableReason);
const makeWatchUtils = prepareWatchUtils(zone, {
watch,
when,
makeVowKit,
isRetryableReason,
});
const watchUtils = makeWatchUtils();
const asVow = makeAsVow(makeVowKit);
/**
* TODO FIXME make this real
* Create a function that retries the given function if the underlying
* functions rejects due to upgrade disconnection.
*
* The internal functions
*
* @template T
* @param {Zone} fnZone - the zone for the named function
* @param {string} name
* @param {(...args: unknown[]) => ERef<T>} fn
* @returns {(...args: unknown[]) => Vow<T>}
*/
const retriable =
(fnZone, name, fn) =>
(...args) => {
return watch(fn(...args));
};
/**
* Vow-tolerant implementation of Promise.all.
*
* @param {EVow<unknown>[]} maybeVows
*/
const allVows = maybeVows => watchUtils.all(maybeVows);
/** @type {AsPromiseFunction} */
const asPromise = (specimenP, ...watcherArgs) =>
watchUtils.asPromise(specimenP, ...watcherArgs);
return harden({
when,
watch,
makeVowKit,
allVows,
asVow,
asPromise,
retriable,
});
};
harden(prepareVowTools);
/** @typedef {ReturnType<typeof prepareVowTools>} VowTools */