-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisposable.js
46 lines (46 loc) · 1.35 KB
/
disposable.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
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Disposable = void 0;
const events_1 = __importDefault(require("events"));
class Disposable extends events_1.default {
constructor() {
super(...arguments);
this.disposables = [];
this._isDisposed = false;
}
get isDisposed() {
return this._isDisposed;
}
registerDisposable(disposable) {
this.disposables.push(disposable);
}
dispose() {
for (const disposable of this.disposables.reverse()) {
disposable.dispose();
}
this.disposables.length = 0;
this.removeAllListeners();
this._isDisposed = true;
}
/**
* Attempts to dispose all dependencies while swallowing errors.
* __unsafe due to being in a potentially unknown state at the time
* `this.dispose()` is called.
*/
dispose__unsafe() {
for (const disposable of this.disposables.reverse()) {
try {
disposable.dispose__unsafe();
}
catch (e) {
console.error(e);
}
}
this.disposables.length = 0;
this.dispose();
}
}
exports.Disposable = Disposable;