-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdestroyable.ts
More file actions
30 lines (25 loc) · 602 Bytes
/
Copy pathdestroyable.ts
File metadata and controls
30 lines (25 loc) · 602 Bytes
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
export abstract class Destroyable implements Disposable {
protected _abortSignal?: AbortSignal;
protected _destroyed: boolean;
constructor(abortSignal?: AbortSignal) {
this._abortSignal = abortSignal;
this._destroyed = false;
this._abortSignal?.addEventListener(
'abort',
() => {
this.destroy();
},
{ once: true },
);
}
destroy() {
this._destroyed = true;
}
[Symbol.dispose](): void {
this.destroy();
}
// Firefox fix (Symbol.dispose is undefined in FF)
[Symbol.for('Symbol.dispose')](): void {
this.destroy();
}
}