-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathdebug.ts
More file actions
244 lines (188 loc) · 7.32 KB
/
debug.ts
File metadata and controls
244 lines (188 loc) · 7.32 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { DEBUG } from '@glimmer/env';
/* eslint-disable @typescript-eslint/no-non-null-assertion -- @fixme */
import type { Tag } from '@glimmer/interfaces';
import { asPresentArray, getLast } from '@glimmer/debug-util';
import { assert } from '@glimmer/global-context';
interface DebugTransaction {
beginTrackingTransaction?:
| undefined
| ((debuggingContext?: string | false, deprecate?: boolean) => void);
endTrackingTransaction?: undefined | (() => void);
runInTrackingTransaction?: undefined | (<T>(fn: () => T, debuggingContext?: string | false) => T);
resetTrackingTransaction?: undefined | (() => string);
setTrackingTransactionEnv?:
| undefined
| ((env: { debugMessage?(obj?: unknown, keyName?: string): string }) => void);
assertTagNotConsumed?:
| undefined
| (<T>(tag: Tag, obj?: T, keyName?: keyof T | string | symbol) => void);
markTagAsConsumed?: undefined | ((_tag: Tag) => void);
logTrackingStack?: undefined | ((transaction?: Transaction) => string);
}
export const debug: DebugTransaction = {};
interface Transaction {
parent: Transaction | null;
debugLabel?: string | undefined;
}
if (DEBUG) {
let CONSUMED_TAGS: WeakMap<Tag, Transaction> | null = null;
const TRANSACTION_STACK: Transaction[] = [];
/////////
const TRANSACTION_ENV = {
debugMessage(obj?: unknown, keyName?: string) {
let objName;
if (typeof obj === 'function') {
objName = obj.name;
} else if (typeof obj === 'object' && obj !== null) {
let className = obj.constructor.name || '(unknown class)';
objName = `(an instance of ${className})`;
} else if (obj === undefined) {
objName = '(an unknown tag)';
} else {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
objName = String(obj);
}
let dirtyString = keyName ? `\`${keyName}\` on \`${objName}\`` : `\`${objName}\``;
return `You attempted to update ${dirtyString}, but it had already been used previously in the same computation. Attempting to update a value after using it in a computation can cause logical errors, infinite revalidation bugs, and performance issues, and is not supported.`;
},
};
debug.setTrackingTransactionEnv = (env) => Object.assign(TRANSACTION_ENV, env);
debug.beginTrackingTransaction = (_debugLabel?: string | false) => {
CONSUMED_TAGS = CONSUMED_TAGS || new WeakMap();
let debugLabel = _debugLabel || undefined;
let parent = TRANSACTION_STACK[TRANSACTION_STACK.length - 1] ?? null;
TRANSACTION_STACK.push({
parent,
debugLabel,
});
};
debug.endTrackingTransaction = () => {
if (TRANSACTION_STACK.length === 0) {
throw new Error('attempted to close a tracking transaction, but one was not open');
}
TRANSACTION_STACK.pop();
if (TRANSACTION_STACK.length === 0) {
CONSUMED_TAGS = null;
}
};
debug.resetTrackingTransaction = () => {
let stack = '';
if (TRANSACTION_STACK.length > 0) {
stack = debug.logTrackingStack!(TRANSACTION_STACK[TRANSACTION_STACK.length - 1]);
}
TRANSACTION_STACK.splice(0, TRANSACTION_STACK.length);
CONSUMED_TAGS = null;
return stack;
};
/**
* Creates a global autotracking transaction. This will prevent any backflow
* in any `track` calls within the transaction, even if they are not
* externally consumed.
*
* `runInAutotrackingTransaction` can be called within itself, and it will add
* onto the existing transaction if one exists.
*
* TODO: Only throw an error if the `track` is consumed.
*/
debug.runInTrackingTransaction = <T>(fn: () => T, debugLabel?: string | false) => {
debug.beginTrackingTransaction!(debugLabel);
let didError = true;
try {
let value = fn();
didError = false;
return value;
} finally {
if (!didError) {
debug.endTrackingTransaction!();
}
// if (id !== TRANSACTION_STACK.length) {
// throw new Error(
// `attempted to close a tracking transaction (${id}), but it was not the last transaction (${TRANSACTION_STACK.length})`
// );
// }
}
};
let nthIndex = (str: string, pattern: string, n: number, startingPos = -1) => {
let i = startingPos;
while (n-- > 0 && i++ < str.length) {
i = str.indexOf(pattern, i);
if (i < 0) break;
}
return i;
};
let makeTrackingErrorMessage = <T>(
transaction: Transaction,
obj?: T,
keyName?: keyof T | string | symbol
) => {
let message = [TRANSACTION_ENV.debugMessage(obj, keyName && String(keyName))];
message.push(`\`${String(keyName)}\` was first used:`);
message.push(debug.logTrackingStack!(transaction));
message.push(`Stack trace for the update:`);
return message.join('\n\n');
};
debug.logTrackingStack = (transaction?: Transaction) => {
let trackingStack = [];
let current: Transaction | null | undefined =
transaction || TRANSACTION_STACK[TRANSACTION_STACK.length - 1];
if (current === undefined) return '';
while (current) {
if (current.debugLabel) {
trackingStack.unshift(current.debugLabel);
}
current = current.parent;
}
return trackingStack.map((label, index) => ' '.repeat(2 * index) + label).join('\n');
};
debug.markTagAsConsumed = (_tag: Tag) => {
if (!CONSUMED_TAGS || CONSUMED_TAGS.has(_tag)) return;
CONSUMED_TAGS.set(_tag, getLast(asPresentArray(TRANSACTION_STACK)));
// We need to mark the tag and all of its subtags as consumed, so we need to
// cast it and access its internals. In the future this shouldn't be necessary,
// this is only for computed properties.
let subtag = (_tag as unknown as { subtag: Tag | Tag[] | null }).subtag;
if (!subtag || !debug.markTagAsConsumed) return;
if (Array.isArray(subtag)) {
subtag.forEach(debug.markTagAsConsumed);
} else {
debug.markTagAsConsumed(subtag);
}
};
debug.assertTagNotConsumed = <T>(tag: Tag, obj?: T, keyName?: keyof T | string | symbol) => {
if (CONSUMED_TAGS === null) return;
let transaction = CONSUMED_TAGS.get(tag);
if (!transaction) return;
// Allow get/set/get (lazy initialization) within the same tracking frame.
// If the tag was consumed in the current transaction, un-consume it so that
// a subsequent read can re-consume it with the updated value.
let currentTransaction = TRANSACTION_STACK[TRANSACTION_STACK.length - 1];
if (transaction === currentTransaction) {
CONSUMED_TAGS.delete(tag);
return;
}
// This hack makes the assertion message nicer, we can cut off the first
// few lines of the stack trace and let users know where the actual error
// occurred.
try {
assert(false, makeTrackingErrorMessage(transaction, obj, keyName));
} catch (e) {
if (hasStack(e)) {
let updateStackBegin = e.stack.indexOf('Stack trace for the update:');
if (updateStackBegin !== -1) {
let start = nthIndex(e.stack, '\n', 1, updateStackBegin);
let end = nthIndex(e.stack, '\n', 4, updateStackBegin);
e.stack = e.stack.substr(0, start) + e.stack.substr(end);
}
}
throw e;
}
};
}
function hasStack(error: unknown): error is { stack: string } {
return (
typeof error === 'object' &&
error !== null &&
'stack' in error &&
typeof error.stack === 'string'
);
}