1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.disposableTimeout = exports.DisposableGroup = exports.DisposableCollection = exports.Disposable = void 0;
|
19 | const event_1 = require("./event");
|
20 | const types_1 = require("./types");
|
21 | var Disposable;
|
22 | (function (Disposable) {
|
23 | function is(arg) {
|
24 | return (0, types_1.isObject)(arg) && (0, types_1.isFunction)(arg.dispose);
|
25 | }
|
26 | Disposable.is = is;
|
27 | function create(func) {
|
28 | return { dispose: func };
|
29 | }
|
30 | Disposable.create = create;
|
31 | })(Disposable = exports.Disposable || (exports.Disposable = {}));
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | Object.defineProperty(Disposable, 'NULL', {
|
37 | configurable: false,
|
38 | enumerable: true,
|
39 | get() {
|
40 | return { dispose: () => { } };
|
41 | }
|
42 | });
|
43 | class DisposableCollection {
|
44 | constructor(...toDispose) {
|
45 | this.disposables = [];
|
46 | this.onDisposeEmitter = new event_1.Emitter();
|
47 | this.disposingElements = false;
|
48 | toDispose.forEach(d => this.push(d));
|
49 | }
|
50 | |
51 |
|
52 |
|
53 |
|
54 | get onDispose() {
|
55 | return this.onDisposeEmitter.event;
|
56 | }
|
57 | checkDisposed() {
|
58 | if (this.disposed && !this.disposingElements) {
|
59 | this.onDisposeEmitter.fire(undefined);
|
60 | this.onDisposeEmitter.dispose();
|
61 | }
|
62 | }
|
63 | get disposed() {
|
64 | return this.disposables.length === 0;
|
65 | }
|
66 | dispose() {
|
67 | if (this.disposed || this.disposingElements) {
|
68 | return;
|
69 | }
|
70 | this.disposingElements = true;
|
71 | while (!this.disposed) {
|
72 | try {
|
73 | this.disposables.pop().dispose();
|
74 | }
|
75 | catch (e) {
|
76 | console.error(e);
|
77 | }
|
78 | }
|
79 | this.disposingElements = false;
|
80 | this.checkDisposed();
|
81 | }
|
82 | push(disposable) {
|
83 | const disposables = this.disposables;
|
84 | disposables.push(disposable);
|
85 | const originalDispose = disposable.dispose.bind(disposable);
|
86 | const toRemove = Disposable.create(() => {
|
87 | const index = disposables.indexOf(disposable);
|
88 | if (index !== -1) {
|
89 | disposables.splice(index, 1);
|
90 | }
|
91 | this.checkDisposed();
|
92 | });
|
93 | disposable.dispose = () => {
|
94 | toRemove.dispose();
|
95 | disposable.dispose = originalDispose;
|
96 | originalDispose();
|
97 | };
|
98 | return toRemove;
|
99 | }
|
100 | pushAll(disposables) {
|
101 | return disposables.map(disposable => this.push(disposable));
|
102 | }
|
103 | }
|
104 | exports.DisposableCollection = DisposableCollection;
|
105 | var DisposableGroup;
|
106 | (function (DisposableGroup) {
|
107 | function canPush(candidate) {
|
108 | return Boolean(candidate && candidate.push);
|
109 | }
|
110 | DisposableGroup.canPush = canPush;
|
111 | function canAdd(candidate) {
|
112 | return Boolean(candidate && candidate.add);
|
113 | }
|
114 | DisposableGroup.canAdd = canAdd;
|
115 | })(DisposableGroup = exports.DisposableGroup || (exports.DisposableGroup = {}));
|
116 | function disposableTimeout(...args) {
|
117 | const handle = setTimeout(...args);
|
118 | return { dispose: () => clearTimeout(handle) };
|
119 | }
|
120 | exports.disposableTimeout = disposableTimeout;
|
121 |
|
\ | No newline at end of file |