-
Notifications
You must be signed in to change notification settings - Fork 59
/
index.js
70 lines (61 loc) · 1.89 KB
/
index.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
import { SharedService, createSharedServicePort } from "./SharedService.js";
// This is a sample service. Only methods with structured cloneable
// arguments and results can be called by proxy.
const target = {
async add(x, y) {
log(`evaluating ${x} + ${y}`);
return x + y;
},
multiply(x, y) {
log(`evaluating ${x} * ${y}`);
return x * y;
},
async slow_subtract(x, y) {
log(`evaluating ${x} - ${y} with 5s delay`);
await new Promise(resolve => setTimeout(resolve, 5000));
return x - y;
},
throw_error(x, y) {
log('throwing Error');
throw new Error('test error');
}
};
// This function is called when this instance is designated as the
// service provider. The port is created locally here but it could
// come from a different context, e.g. a Worker.
function portProvider() {
log('appointed provider');
return createSharedServicePort(target);
}
// Create the shared service.
log('start');
const sharedService = new SharedService('test', portProvider);
sharedService.activate();
for (const button of Array.from(document.getElementsByTagName('button'))) {
button.addEventListener('click', async () => {
// Call the service.
const op = button.getAttribute('data-op');
const x = Math.trunc(Math.random() * 100);
const y = Math.trunc(Math.random() * 100);
log(`requesting ${op}(${x}, ${y})`);
try {
const result = await sharedService.proxy[op](x, y);
log(`result ${result}`);
} catch (e) {
const text = e.stack.includes(e.message) ? e.stack : `${e.message}\n${e.stack}`;
log(text);
}
});
}
function log(s) {
const TIME_FORMAT = {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 3
};
// @ts-ignore
const timestamp = new Date().toLocaleTimeString(undefined, TIME_FORMAT);
document.getElementById('output').textContent += `${timestamp} ${s}\n`;
}