UNPKG

1.15 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4/**
5 * Based off of VS Code
6 * https://github.com/microsoft/vscode/blob/7bed4ce3e9f5059b5fc638c348f064edabcce5d2/src/vs/workbench/api/common/extHostTypes.ts#L65
7 */
8export class Disposable {
9 static from(...inDisposables: { dispose(): any }[]): Disposable {
10 let disposables: ReadonlyArray<{ dispose(): any }> | undefined = inDisposables;
11 return new Disposable(function () {
12 if (disposables) {
13 for (const disposable of disposables) {
14 if (disposable && typeof disposable.dispose === 'function') {
15 disposable.dispose();
16 }
17 }
18 disposables = undefined;
19 }
20 });
21 }
22
23 #callOnDispose?: () => any;
24
25 constructor(callOnDispose: () => any) {
26 this.#callOnDispose = callOnDispose;
27 }
28
29 dispose(): any {
30 if (typeof this.#callOnDispose === 'function') {
31 this.#callOnDispose();
32 this.#callOnDispose = undefined;
33 }
34 }
35}