-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtest-distributed-programming.js
53 lines (46 loc) · 1.4 KB
/
test-distributed-programming.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
// @ts-check
/* eslint-disable import/order -- https://github.com/endojs/endo/issues/1235 */
import { test } from './prepare-test-env-ava.js';
import { E } from '@endo/eventual-send';
// #region importFar
import { Far } from '@endo/far';
// #endregion importFar
// #region import-pass-style
import { passStyleOf } from '@endo/pass-style';
// #endregion import-pass-style
test('remote counter', async t => {
const assert = cond => t.true(cond);
// #region makeFarCounter
const makeCounter = () => {
let count = 0;
return Far('counter', {
incr: () => (count += 1),
decr: () => (count -= 1),
});
};
const publicFacet = Far('makeCounter', { makeCounter });
assert(passStyleOf(publicFacet) === 'remotable');
// #endregion makeFarCounter
// #region useFarCounter
const counter = E(publicFacet).makeCounter();
const n = await E(counter).incr();
assert(n === 1);
// #endregion useFarCounter
});
test('async fetch', async t => {
const assert = cond => t.true(cond);
const fetch = _ =>
Promise.resolve({
json: () => Promise.resolve(['p1', 'p2']),
});
const initialize = p => assert(p.length === 2);
// #region asyncFetch
const init = fetch('products.json')
.then(response => response.json())
.then(products => initialize(products))
.catch(err => {
console.log(`Fetch problem: ${err.message}`);
});
// #endregion asyncFetch
await init;
});