forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresourceLifecycle.ts
More file actions
45 lines (38 loc) · 1.1 KB
/
Copy pathresourceLifecycle.ts
File metadata and controls
45 lines (38 loc) · 1.1 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { IDisposable } from '../types';
interface IDisposables extends IDisposable {
push(...disposable: IDisposable[]): void;
}
/**
* Safely dispose each of the disposables.
*/
export async function disposeAll(disposables: IDisposable[]): Promise<void> {
await Promise.all(
disposables.map(async (d) => {
try {
return Promise.resolve(d.dispose());
} catch (err) {
// do nothing
}
return Promise.resolve();
}),
);
}
/**
* A list of disposables.
*/
export class Disposables implements IDisposables {
private disposables: IDisposable[] = [];
constructor(...disposables: IDisposable[]) {
this.disposables.push(...disposables);
}
public push(...disposables: IDisposable[]): void {
this.disposables.push(...disposables);
}
public async dispose(): Promise<void> {
const { disposables } = this;
this.disposables = [];
await disposeAll(disposables);
}
}