-
Notifications
You must be signed in to change notification settings - Fork 72
/
track-turns.js
95 lines (87 loc) · 3.1 KB
/
track-turns.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* global globalThis */
// @ts-nocheck
// NOTE: We can't import these because they're not in scope before lockdown.
// import { assert, details as X } from '@agoric/assert';
// WARNING: Global Mutable State!
// This state is communicated to `assert` that makes it available to the
// causal console, which affects the console log output. Normally we
// regard the ability to see console log output as a meta-level privilege
// analogous to the ability to debug. Aside from that, this module should
// not have any observably mutable state.
let hiddenPriorError;
let hiddenCurrentTurn = 0;
let hiddenCurrentEvent = 0;
// Turn on if you seem to be losing error logging at the top of the event loop
const VERBOSE = false;
/**
* @typedef {((...args: any[]) => any) | undefined} TurnStarterFn
* An optional function that is not this-sensitive, expected to be called at
* bottom of stack to start a new turn.
*/
/**
* @template {TurnStarterFn[]} T
* Given a list of `TurnStarterFn`s, returns a list of `TurnStarterFn`s whose
* `this`-free call behaviors are not observably different to those that
* cannot see console output. The only purpose is to cause additional
* information to appear on the console.
*
* The call to `trackTurns` is itself a sending event, that occurs in some call
* stack in some turn number at some event number within that turn. Each call
* to any of the returned `TurnStartFn`s is a receiving event that begins a new
* turn. This sending event caused each of those receiving events.
*
* @param {T} funcs
* @returns {T}
*/
export const trackTurns = funcs => {
if (typeof globalThis === 'undefined' || !globalThis.assert) {
return funcs;
}
const { details: X } = assert;
hiddenCurrentEvent += 1;
const sendingError = new Error(
`Event: ${hiddenCurrentTurn}.${hiddenCurrentEvent}`,
);
if (hiddenPriorError !== undefined) {
assert.note(sendingError, X`Caused by: ${hiddenPriorError}`);
}
return funcs.map(
func =>
func &&
((...args) => {
hiddenPriorError = sendingError;
hiddenCurrentTurn += 1;
hiddenCurrentEvent = 0;
try {
let result;
try {
result = func(...args);
} catch (err) {
if (err instanceof Error) {
assert.note(
err,
X`Thrown from: ${hiddenPriorError}:${hiddenCurrentTurn}.${hiddenCurrentEvent}`,
);
}
if (VERBOSE) {
console.log('THROWN to top of event loop', err);
}
throw err;
}
// Must capture this now, not when the catch triggers.
const detailsNote = X`Rejection from: ${hiddenPriorError}:${hiddenCurrentTurn}.${hiddenCurrentEvent}`;
Promise.resolve(result).catch(reason => {
if (reason instanceof Error) {
assert.note(reason, detailsNote);
}
if (VERBOSE) {
console.log('REJECTED at top of event loop', reason);
}
});
return harden(result);
} finally {
hiddenPriorError = undefined;
}
}),
);
};