-
Notifications
You must be signed in to change notification settings - Fork 384
/
disposable.ts
404 lines (349 loc) · 10.3 KB
/
disposable.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
395
396
397
398
399
400
401
402
403
404
/** ******************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { MaybePromise } from './async';
import { Emitter, Event } from './event';
export class DisposableStore implements IDisposable {
private toDispose = new Set<IDisposable>();
private _isDisposed = false;
/**
* Dispose of all registered disposables and mark this object as disposed.
*
* Any future disposables added to this object will be disposed of on `add`.
*/
public dispose(): void {
if (this._isDisposed) {
return;
}
markTracked(this);
this._isDisposed = true;
this.clear();
}
/**
* Dispose of all registered disposables but do not mark this object as disposed.
*/
public clear(): void {
this.toDispose.forEach((item) => item.dispose());
this.toDispose.clear();
}
public add<T extends IDisposable>(t: T): T {
if (!t) {
return t;
}
if ((t as any as DisposableStore) === this) {
throw new Error('Cannot register a disposable on itself!');
}
markTracked(t);
if (this._isDisposed) {
// eslint-disable-next-line no-console
console.warn(new Error('Registering disposable on object that has already been disposed of').stack);
t.dispose();
} else {
this.toDispose.add(t);
}
return t;
}
public addAll(disposables: IDisposable[]) {
for (const item of disposables) {
this.add(item);
}
}
}
export interface IDisposable {
/**
* Dispose this object.
*/
dispose(): void;
}
export function isDisposable<E extends object>(thing: E): thing is E & IDisposable {
return typeof (thing as IDisposable).dispose === 'function' && (thing as IDisposable).dispose.length === 0;
}
export function dispose<T extends IDisposable>(disposable: T): T;
export function dispose<T extends IDisposable>(...disposables: Array<T | undefined>): T[];
export function dispose<T extends IDisposable>(disposables: T[]): T[];
export function dispose<T extends IDisposable>(first: T | T[], ...rest: T[]): T | T[] | undefined {
if (Array.isArray(first)) {
first.forEach((d) => {
if (d) {
markTracked(d);
d.dispose();
}
});
return [];
} else if (rest.length === 0) {
if (first) {
markTracked(first);
first.dispose();
return first;
}
return undefined;
} else {
dispose(first);
dispose(rest);
return [];
}
}
export function combinedDisposable(disposables: IDisposable[]): IDisposable {
disposables.forEach(markTracked);
return trackDisposable({ dispose: () => dispose(disposables) });
}
export function toDisposable(fn: () => void): IDisposable {
return {
dispose() {
fn();
},
};
}
export class Disposable implements IDisposable {
protected readonly disposables: IDisposable[] = [];
protected readonly onDisposeEmitter = new Emitter<void>();
constructor(...toDispose: IDisposable[]) {
toDispose.forEach((d) => this.addDispose(d));
}
static create(func: () => void): IDisposable {
return {
dispose: func,
};
}
static NULL = Disposable.create(() => {});
static None = Object.freeze<IDisposable>({ dispose() {} });
get onDispose(): Event<void> {
return this.onDisposeEmitter.event;
}
protected checkDisposed(): void {
if (this.disposed && !this.disposingElements) {
this.onDisposeEmitter.fire(undefined);
}
}
get disposed(): boolean {
return this.disposables.length === 0;
}
private disposingElements = false;
dispose(): void {
if (this.disposed || this.disposingElements) {
return;
}
this.disposingElements = true;
while (!this.disposed) {
try {
this.disposables.pop()!.dispose();
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
}
this.disposingElements = false;
this.checkDisposed();
}
addDispose(disposable: IDisposable): IDisposable;
addDispose(disposable: IDisposable[]): IDisposable[];
addDispose(disposable: IDisposable | IDisposable[]): IDisposable | IDisposable[] {
if (Array.isArray(disposable)) {
const disposables = disposable;
return disposables.map((disposable) => this.addDispose(disposable));
} else {
return this.add(disposable);
}
}
protected registerDispose<T extends IDisposable>(disposable: T): T {
if ((disposable as any as Disposable) === this) {
throw new Error('Cannot register a disposable on itself!');
}
this.add(disposable);
return disposable;
}
private add(disposable: IDisposable): IDisposable {
const disposables = this.disposables;
disposables.push(disposable);
const originalDispose = disposable.dispose.bind(disposable);
const toRemove = Disposable.create(() => {
const index = disposables.indexOf(disposable);
if (index !== -1) {
disposables.splice(index, 1);
}
this.checkDisposed();
});
disposable.dispose = () => {
toRemove.dispose();
originalDispose();
};
return toRemove;
}
}
export class DisposableCollection implements IDisposable {
protected readonly disposables: IDisposable[] = [];
protected readonly onDisposeEmitter = new Emitter<void>();
constructor(...toDispose: IDisposable[]) {
toDispose.forEach((d) => this.push(d));
}
get onDispose(): Event<void> {
return this.onDisposeEmitter.event;
}
protected checkDisposed(): void {
if (this.disposed && !this.disposingElements) {
this.onDisposeEmitter.fire(undefined);
}
}
get disposed(): boolean {
return this.disposables.length === 0;
}
private disposingElements = false;
dispose(): void {
if (this.disposed || this.disposingElements) {
return;
}
const toPromise = [] as any[];
this.disposingElements = true;
while (!this.disposed) {
try {
const maybePromise = this.disposables.pop()!.dispose() as MaybePromise<void>;
if (maybePromise) {
toPromise.push(maybePromise);
}
} catch (e) {
// eslint-disable-next-line no-console
console.error('DisposableCollection.dispose error', e);
}
}
this.disposingElements = false;
this.checkDisposed();
return Promise.all(toPromise) as unknown as MaybePromise<void> as void;
}
push(disposable: IDisposable): IDisposable {
const disposables = this.disposables;
disposables.push(disposable);
const originalDispose = disposable.dispose.bind(disposable);
const toRemove = Disposable.create(() => {
const index = disposables.indexOf(disposable);
if (index !== -1) {
disposables.splice(index, 1);
}
this.checkDisposed();
});
disposable.dispose = () => {
toRemove.dispose();
originalDispose();
};
return toRemove;
}
pushAll(disposables: IDisposable[]): IDisposable[] {
return disposables.map((disposable) => this.push(disposable));
}
}
/**
* Enables logging of potentially leaked disposables.
*
* A disposable is considered leaked if it is not disposed or not registered as the child of
* another disposable. This tracking is very simple an only works for classes that either
* extend Disposable or use a DisposableStore. This means there are a lot of false positives.
*/
const TRACK_DISPOSABLES = false;
const __is_disposable_tracked__ = '__is_disposable_tracked__';
function markTracked<T extends IDisposable>(x: T): void {
if (!TRACK_DISPOSABLES) {
return;
}
if (x && x !== Disposable.None) {
try {
(x as any)[__is_disposable_tracked__] = true;
} catch (_e) {
// noop
}
}
}
function trackDisposable<T extends IDisposable>(x: T): T {
if (!TRACK_DISPOSABLES) {
return x;
}
const stack = new Error('Potentially leaked disposable').stack!;
setTimeout(() => {
if (!(x as any)[__is_disposable_tracked__]) {
// eslint-disable-next-line no-console
console.log(stack);
}
}, 3000);
return x;
}
/**
* Manages the lifecycle of a disposable value that may be changed.
*
* This ensures that when the the disposable value is changed, the previously held disposable is disposed of. You can
* also register a `MutableDisposable` on a `Disposable` to ensure it is automatically cleaned up.
*/
export class MutableDisposable<T extends IDisposable> implements IDisposable {
private _value?: T;
private _isDisposed = false;
constructor() {
trackDisposable(this);
}
get value(): T | undefined {
return this._isDisposed ? undefined : this._value;
}
set value(value: T | undefined) {
if (this._isDisposed || value === this._value) {
return;
}
if (this._value) {
this._value.dispose();
}
if (value) {
markTracked(value);
}
this._value = value;
}
clear() {
this.value = undefined;
}
dispose(): void {
this._isDisposed = true;
markTracked(this);
if (this._value) {
this._value.dispose();
}
this._value = undefined;
}
}
export class RefCountedDisposable {
private _counter = 1;
constructor(private readonly _disposable: IDisposable) {}
get disposed() {
return this._counter <= 0;
}
acquire() {
this._counter++;
return this;
}
release() {
if (--this._counter === 0) {
this._disposable.dispose();
}
return this;
}
}
export class DisposableMap<K, V extends IDisposable = IDisposable> extends Map<K, V> implements IDisposable {
disposeKey(key: K): void {
const disposable = this.get(key);
if (disposable) {
disposable.dispose();
}
this.delete(key);
}
dispose(): void {
for (const disposable of this.values()) {
disposable.dispose();
}
this.clear();
}
}