-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathindex.ts
More file actions
296 lines (234 loc) · 8.1 KB
/
Copy pathindex.ts
File metadata and controls
296 lines (234 loc) · 8.1 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { DEBUG } from '@glimmer/env';
import type { Destroyable, Destructor } from '@glimmer/interfaces';
import { debugToString } from '@glimmer/debug-util';
export let scheduleDestroy: <T extends Destroyable>(
destroyable: T,
destructor: Destructor<T>
) => void = () => {};
export let scheduleDestroyed: (finalizer: () => void) => void = () => {};
export function setDestructionSchedulers(
destroy: typeof scheduleDestroy,
destroyed: typeof scheduleDestroyed
): void {
scheduleDestroy = destroy;
scheduleDestroyed = destroyed;
}
const LIVE_STATE = 0;
const DESTROYING_STATE = 1;
const DESTROYED_STATE = 2;
type DestroyableState = 0 | 1 | 2;
type OneOrMany<T> = null | T | BrandedArray<T>;
interface DestroyableMeta<T extends Destroyable> {
source?: T;
parents: OneOrMany<Destroyable>;
children: OneOrMany<Destroyable>;
eagerDestructors: OneOrMany<Destructor<T>>;
destructors: OneOrMany<Destructor<T>>;
state: DestroyableState;
}
interface UndestroyedDestroyablesError extends Error {
destroyables: object[];
}
let DESTROYABLE_META:
| Map<Destroyable, DestroyableMeta<Destroyable>>
| WeakMap<Destroyable, DestroyableMeta<Destroyable>> = new WeakMap();
const branded = Symbol('BrandedArray');
type BrandedArray<T> = T[] & { [branded]: true };
function isBrandedArray<T>(collection: OneOrMany<T>): collection is BrandedArray<T> {
return Array.isArray(collection) && branded in collection;
}
function push<T extends object>(collection: OneOrMany<T>, newItem: T): OneOrMany<T> {
if (collection === null) {
return newItem;
} else if (isBrandedArray(collection)) {
collection.push(newItem);
return collection;
} else {
const b = [collection, newItem] as BrandedArray<T>;
b[branded] = true;
return b;
}
}
function iterate<T extends object>(collection: OneOrMany<T>, fn: (item: T) => void) {
if (isBrandedArray(collection)) {
collection.forEach(fn);
} else if (collection !== null) {
fn(collection);
}
}
function remove<T extends object>(collection: OneOrMany<T>, item: T, message: string | false) {
if (DEBUG) {
let collectionIsItem = collection === item;
let collectionContainsItem = isBrandedArray(collection) && collection.indexOf(item) !== -1;
if (!collectionIsItem && !collectionContainsItem) {
throw new Error(String(message));
}
}
if (isBrandedArray(collection) && collection.length > 1) {
let index = collection.indexOf(item);
let lastIndex = collection.length - 1;
if (index !== lastIndex) {
collection[index] = collection[lastIndex] as T;
}
collection.length = lastIndex;
return collection;
} else {
return null;
}
}
function getDestroyableMeta<T extends Destroyable>(destroyable: T): DestroyableMeta<T> {
let meta = DESTROYABLE_META.get(destroyable);
if (meta === undefined) {
meta = {
parents: null,
children: null,
eagerDestructors: null,
destructors: null,
state: LIVE_STATE,
};
if (DEBUG) {
meta.source = destroyable as object;
}
DESTROYABLE_META.set(destroyable, meta);
}
return meta as unknown as DestroyableMeta<T>;
}
export function associateDestroyableChild<T extends Destroyable>(parent: Destroyable, child: T): T {
if (DEBUG && isDestroying(parent)) {
throw new Error(
'Attempted to associate a destroyable child with an object that is already destroying or destroyed'
);
}
let parentMeta = getDestroyableMeta(parent);
let childMeta = getDestroyableMeta(child);
parentMeta.children = push(parentMeta.children, child);
childMeta.parents = push(childMeta.parents, parent);
return child;
}
export function registerDestructor<T extends Destroyable>(
destroyable: T,
destructor: Destructor<T>,
eager = false
): Destructor<T> {
if (DEBUG && isDestroying(destroyable)) {
throw new Error(
'Attempted to register a destructor with an object that is already destroying or destroyed'
);
}
let meta = getDestroyableMeta(destroyable);
let destructorsKey: 'eagerDestructors' | 'destructors' = eager
? 'eagerDestructors'
: 'destructors';
meta[destructorsKey] = push(meta[destructorsKey], destructor);
return destructor;
}
export function unregisterDestructor<T extends Destroyable>(
destroyable: T,
destructor: Destructor<T>,
eager = false
): void {
if (DEBUG && isDestroying(destroyable)) {
throw new Error(
'Attempted to unregister a destructor with an object that is already destroying or destroyed'
);
}
let meta = getDestroyableMeta(destroyable);
let destructorsKey: 'eagerDestructors' | 'destructors' = eager
? 'eagerDestructors'
: 'destructors';
meta[destructorsKey] = remove(
meta[destructorsKey],
destructor,
DEBUG && 'attempted to remove a destructor that was not registered with the destroyable'
);
}
////////////
export function destroy(destroyable: Destroyable) {
let meta = getDestroyableMeta(destroyable);
if (meta.state >= DESTROYING_STATE) return;
let { parents, children, eagerDestructors, destructors } = meta;
meta.state = DESTROYING_STATE;
iterate(children, destroy);
iterate(eagerDestructors, (destructor) => {
destructor(destroyable);
});
iterate(destructors, (destructor) => {
scheduleDestroy(destroyable, destructor);
});
scheduleDestroyed(() => {
iterate(parents, (parent) => {
removeChildFromParent(destroyable, parent);
});
meta.state = DESTROYED_STATE;
});
}
function removeChildFromParent(child: Destroyable, parent: Destroyable) {
let parentMeta = getDestroyableMeta(parent);
if (parentMeta.state !== DESTROYED_STATE) {
parentMeta.children = remove(
parentMeta.children,
child,
DEBUG &&
"attempted to remove child from parent, but the parent's children did not contain the child. This is likely a bug with destructors."
);
}
}
export function destroyChildren(destroyable: Destroyable) {
let { children } = getDestroyableMeta(destroyable);
iterate(children, destroy);
}
export function _hasDestroyableChildren(destroyable: Destroyable) {
let meta = DESTROYABLE_META.get(destroyable);
return meta === undefined ? false : meta.children !== null;
}
export function isDestroying(destroyable: Destroyable) {
let meta = DESTROYABLE_META.get(destroyable);
return meta === undefined ? false : meta.state >= DESTROYING_STATE;
}
export function isDestroyed(destroyable: Destroyable) {
let meta = DESTROYABLE_META.get(destroyable);
return meta === undefined ? false : meta.state >= DESTROYED_STATE;
}
////////////
export let enableDestroyableTracking: undefined | (() => void);
export let assertDestroyablesDestroyed: undefined | (() => void);
if (DEBUG) {
let isTesting = false;
enableDestroyableTracking = () => {
if (isTesting) {
// Reset destroyable meta just in case, before throwing the error
DESTROYABLE_META = new WeakMap();
throw new Error(
'Attempted to start destroyable testing, but you did not end the previous destroyable test. Did you forget to call `assertDestroyablesDestroyed()`'
);
}
isTesting = true;
DESTROYABLE_META = new Map();
};
assertDestroyablesDestroyed = () => {
if (!isTesting) {
throw new Error(
'Attempted to assert destroyables destroyed, but you did not start a destroyable test. Did you forget to call `enableDestroyableTracking()`'
);
}
isTesting = false;
let map = DESTROYABLE_META as Map<Destroyable, DestroyableMeta<Destroyable>>;
DESTROYABLE_META = new WeakMap();
let undestroyed: object[] = [];
map.forEach((meta) => {
if (meta.state !== DESTROYED_STATE) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- @fixme
undestroyed.push(meta.source!);
}
});
if (undestroyed.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- @fixme
let objectsToString = undestroyed.map(debugToString!).join('\n ');
let error = new Error(
`Some destroyables were not destroyed during this test:\n ${objectsToString}`
) as UndestroyedDestroyablesError;
error.destroyables = undestroyed;
throw error;
}
};
}