-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdispose.ts
More file actions
25 lines (22 loc) · 623 Bytes
/
dispose.ts
File metadata and controls
25 lines (22 loc) · 623 Bytes
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
import { isObjectOf } from "@core/unknownutil/is/object-of";
import { isFunction } from "@core/unknownutil/is/function";
const isDisposable = isObjectOf({
[Symbol.dispose]: isFunction,
});
const isAsyncDisposable = isObjectOf({
[Symbol.asyncDispose]: isFunction,
});
/**
* Dispose the resource.
*
* It tries to call Symbol.dispose or Symbol.asyncDispose if the resource has
* the method.
*/
export async function dispose(resource: unknown): Promise<void> {
if (isDisposable(resource)) {
resource[Symbol.dispose]();
}
if (isAsyncDisposable(resource)) {
await resource[Symbol.asyncDispose]();
}
}