-
Notifications
You must be signed in to change notification settings - Fork 3
/
destructors.ts
394 lines (351 loc) · 12 KB
/
destructors.ts
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { assert } from '@ember/debug';
import { schedule } from '@ember/runloop';
import { DEBUG } from '@glimmer/env';
import Ember from 'ember';
import { gte } from 'ember-compatibility-helpers';
import { meta, deleteMeta } from './meta';
export type Destructor<T extends object = object> = (destroyable: T) => void;
let isTesting = false;
let DESTRUCTORS:
| Map<object, Set<Destructor>>
| WeakMap<object, Set<Destructor>> = new Map<object, Set<Destructor>>();
let DESTROYABLE_PARENTS:
| Map<object, object>
| WeakMap<object, object> = new WeakMap<object, object>();
const DESTROYABLE_CHILDREN = new WeakMap<object, Set<object>>();
let _internalRegisterDestructor: Function;
let _internalAssociateDestroyableChild: Function;
let _internalIsDestroying: Function;
let _internalIsDestroyed: Function;
let _internalUnregisterDestructor: Function;
let _internalDestroy: Function;
let _internalAssertDestroyablesDestroyed: Function;
let _internalEnableDestroyableTracking: Function;
if (gte('3.20.0-beta.4')) {
const glimmerRuntime = (Ember as any).__loader.require('@glimmer/runtime');
_internalRegisterDestructor = glimmerRuntime.registerDestructor;
_internalAssociateDestroyableChild = glimmerRuntime.associateDestroyableChild;
_internalIsDestroying = glimmerRuntime.isDestroying;
_internalIsDestroyed = glimmerRuntime.isDestroyed;
_internalUnregisterDestructor = glimmerRuntime.unregisterDestructor;
_internalDestroy = glimmerRuntime.destroy;
_internalAssertDestroyablesDestroyed =
glimmerRuntime.assertDestroyablesDestroyed;
_internalEnableDestroyableTracking = glimmerRuntime.enableDestroyableTracking;
}
function getDestructors<T extends object>(destroyable: T): Set<Destructor<T>> {
if (!DESTRUCTORS.has(destroyable)) DESTRUCTORS.set(destroyable, new Set());
return DESTRUCTORS.get(destroyable)!;
}
function getDestroyableChildren(destroyable: object): Set<object> {
if (!DESTROYABLE_CHILDREN.has(destroyable))
DESTROYABLE_CHILDREN.set(destroyable, new Set());
return DESTROYABLE_CHILDREN.get(destroyable)!;
}
/**
* Receives a destroyable, and returns `true` if the destroyable has begun
* destroying. Otherwise returns false.
*
* @example
* ```ts
* const obj = {};
* isDestroying(obj); // false
* destroy(obj);
* isDestroying(obj); // true
* ```
*/
export function isDestroying(destroyable: object): boolean {
if (gte('3.20.0-beta.4')) {
return _internalIsDestroying(destroyable);
}
return meta(destroyable).isSourceDestroying();
}
/**
* Receives a destroyable, and returns `true` if the destroyable has finished
* destroying. Otherwise returns false.
*
* @example
* ```ts
* const obj = {};
* isDestroyed(obj); // false
* destroy(obj);
* // ...sometime later, after scheduled destruction
* isDestroyed(obj); // true
* ```
*/
export function isDestroyed(destroyable: object): boolean {
if (gte('3.20.0-beta.4')) {
return _internalIsDestroyed(destroyable);
}
return meta(destroyable).isSourceDestroyed();
}
/**
* Asserts that the destroyable was not yet destroyed and is not currently being
* destroyed.
*/
function assertNotDestroyed(destroyable: object): void | never {
assert(`'${destroyable}' was already destroyed.`, !isDestroyed(destroyable));
assert(
`'${destroyable}' is already being destroyed.`,
!isDestroying(destroyable)
);
}
/**
* This function is used to associate a destroyable object with a parent.
* When the parent is destroyed, all registered children will also be destroyed.
*
* Returns the associated child for convenience.
*
* @example
* ```ts
* class CustomSelect extends Component {
* constructor() {
* // obj is now a child of the component. When the component is destroyed,
* // obj will also be destroyed, and have all of its destructors triggered.
* this.obj = associateDestroyableChild(this, {});
* }
* }
* ```
*
* @note Attempting to associate a parent or child that has already been
* destroyed throws an error.
*
* @note Attempting to associate a child to multiple parents throws an error.
*/
export function associateDestroyableChild<T extends object>(
parent: object,
child: T
): T {
if (gte('3.20.0-beta.4')) {
return _internalAssociateDestroyableChild(parent, child);
}
if (DEBUG) assertNotDestroyed(parent);
if (DEBUG) assertNotDestroyed(child);
assert(
`'${child}' is already a child of '${parent}'.`,
!DESTROYABLE_PARENTS.has(child)
);
DESTROYABLE_PARENTS.set(child, parent);
getDestroyableChildren(parent).add(child);
return child;
}
/**
* Receives a destroyable object and a destructor function, and associates the
* function with it.
* When the destroyable is destroyed with `destroy`, or when its parent is
* destroyed, the destructor function will be called.
*
* Multiple destructors can be associated with a given destroyable, and they can
* be associated over time, allowing to dynamically add destructors as needed.
*
* Returns the associated destructor function for convenience.
*
* The destructor function is passed a single argument, which is the destroyable
* itself. This allows the function to be reused multiple times for many
* destroyables, rather than creating a closure function per destroyable.
*
* @example
* ```ts
* function unregisterResize(instance) {
* instance.resize.unregister(instance);
* }
*
* class Modal extends Component {
* @service resize;
*
* constructor() {
* this.resize.register(this, this.layout);
*
* registerDestructor(this, unregisterResize);
* }
* }
* ```
*
* @note Registering a destructor on a destroyed object should throw an error.
*
* @note Attempting to register the same destructor multiple times should throw
* an error.
*/
export function registerDestructor<T extends object>(
destroyable: T,
destructor: Destructor<T>
): Destructor<T> {
if (gte('3.20.0-beta.4')) {
return _internalRegisterDestructor(destroyable, destructor);
}
if (DEBUG) assertNotDestroyed(destroyable);
const destructors = getDestructors(destroyable);
assert(
`'${destructor}' is already registered with '${destroyable}'.`,
!destructors.has(destructor)
);
destructors.add(destructor);
return destructor;
}
/**
* Receives a destroyable and a destructor function, and de-associates the
* destructor from the destroyable.
*
* @example
* ```ts
* class Modal extends Component {
* @service modals;
*
* constructor() {
* this.modals.add(this);
*
* this.modalDestructor = registerDestructor(this, () => this.modals.remove(this));
* }
*
* @action
* pinModal() {
* unregisterDestructor(this, this.modalDestructor);
* }
* }
* ```
*
* @note Calling on a destroyed object throws an error.
*
* @note Calling with a destructor that is not associated with the object throws
* an error.
*/
export function unregisterDestructor<T extends object>(
destroyable: T,
destructor: Destructor<T>
): void {
if (gte('3.20.0-beta.4')) {
return _internalUnregisterDestructor(destroyable, destructor);
}
if (DEBUG) assertNotDestroyed(destroyable);
const destructors = getDestructors(destroyable);
assert(
`'${destructor}' is not registered with '${destroyable}'.`,
destructors.has(destructor)
);
destructors.delete(destructor);
}
/**
* Initiates the destruction of a destroyable object. It runs all associated
* destructors, and then destroys all children recursively.
*
* @example
* ```ts
* const obj = {};
* registerDestructor(obj, () => console.log('destroyed!'));
* destroy(obj); // this will schedule the destructor to be called
* // ...some time later, during scheduled destruction
* // destroyed!
* ```
*
* Destruction via `destroy()` follows these steps:
*
* 1. Mark the destroyable such that `isDestroying(destroyable)` returns `true`
* 2. Schedule calling the destroyable's destructors
* 3. Call `destroy()` on each of the destroyable's associated children
* 4. Schedule setting destroyable such that `isDestroyed(destroyable)` returns
* `true`
*
* This algorithm results in the entire tree of destroyables being first marked
* as destroying, then having all of their destructors called, and finally all
* being marked as `isDestroyed`. There won't be any in between states where
* some items are marked as `isDestroying` while destroying, while others are
* not.
*
* @note Calling `destroy` multiple times on the same destroyable is safe. It
* will not throw an error, and will not take any further action.
*
* @note Calling `destroy` with a destroyable that has no destructors or
* associated children will not throw an error, and will do nothing.
*
*/
export function destroy(destroyable: object): void {
if (gte('3.20.0-beta.4')) {
_internalDestroy(destroyable);
return;
}
if (isDestroying(destroyable) || isDestroyed(destroyable)) return;
const m = meta(destroyable);
m.setSourceDestroying(); // This calls `runDestructors`
}
const RUNNING = new WeakSet();
export function runDestructors(destroyable: object): void {
if (RUNNING.has(destroyable)) return;
RUNNING.add(destroyable);
const m = meta(destroyable);
for (const child of getDestroyableChildren(destroyable)) destroy(child);
for (const destructor of getDestructors(destroyable)) {
schedule('actions', undefined, destructor, destroyable);
}
schedule('destroy', () => {
deleteMeta(destroyable);
m.setSourceDestroyed();
DESTRUCTORS.delete(destroyable);
DESTROYABLE_PARENTS.delete(destroyable);
});
}
interface UndestroyedDestroyablesAssertionError extends Error {
destroyables: object[];
}
/**
* This function sets up the internal destroyables system in order to be able to call
* assertDestroyablesDestroyed later.
*/
export function enableDestroyableTracking() {
if (gte('3.20.2')) {
return _internalEnableDestroyableTracking();
}
if (gte('3.20.0-beta.4')) {
// on 3.20.0-beta.4 through 3.20.2 (estimated) there is an issue with the upstream
// `assertDestroyablesDestroyed` method that triggers the assertion in cases that it
// should not; in order to allow code bases to function on those specific Ember versions
// (including our own test suite) we detect and do nothing
//
// See https://github.com/glimmerjs/glimmer-vm/pull/1119
return;
}
DESTRUCTORS = new Map<object, Set<Destructor>>();
DESTROYABLE_PARENTS = new Map<object, object>();
isTesting = true;
}
/**
* This function asserts that all objects which have associated destructors or
* associated children have been destroyed at the time it is called. It is meant
* to be a low level hook that testing frameworks like `ember-qunit` and
* `ember-mocha` can use to hook into and validate that all destroyables have in
* fact been destroyed.
*/
export function assertDestroyablesDestroyed(): void | never {
if (gte('3.20.2')) {
return _internalAssertDestroyablesDestroyed();
}
if (gte('3.20.0-beta.4')) {
// on 3.20.0-beta.4 through 3.20.2 (estimated) there is an issue with the upstream
// `assertDestroyablesDestroyed` method that triggers the assertion in cases that it
// should not; in order to allow code bases to function on those specific Ember versions
// (including our own test suite) we detect and do nothing
//
// See https://github.com/glimmerjs/glimmer-vm/pull/1119
return;
}
if (!isTesting) {
throw new Error(
'Attempted to assert destroyables destroyed, but you did not start a destroyable test. Did you forget to call `enableDestroyableTracking()`'
);
}
const destructors = DESTRUCTORS as Map<object, WeakSet<Destructor>>;
const children = DESTROYABLE_PARENTS as Map<object, object>;
isTesting = false;
DESTRUCTORS = new WeakMap<object, Set<Destructor>>();
DESTROYABLE_PARENTS = new WeakMap<object, object>();
if (destructors.size > 0 || children.size > 0) {
const error = new Error(
`Some destroyables were not destroyed during this test`
) as UndestroyedDestroyablesAssertionError;
Object.defineProperty(error, 'destroyables', {
get() {
return [...new Set([...destructors.keys(), ...children.keys()])];
}
});
throw error;
}
}