generated from wopjs/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
41 lines (38 loc) · 1 KB
/
utils.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
import type { Disposer, IDisposable } from "./interface";
export type PickMethods<T> = Pick<
T,
{
// eslint-disable-next-line @typescript-eslint/ban-types
[K in keyof T]: T[K] extends Function ? K : never;
}[keyof T]
>;
export type OmitMethods<T> = Pick<
T,
{
// eslint-disable-next-line @typescript-eslint/ban-types
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T]
>;
/**
* Dispose a disposable object or a disposer function. Log the error if any.
* @param disposable A disposable object or a disposer function. Do nothing otherwise.
*/
export const dispose = (disposable: any): void => {
try {
if (disposable) {
if ((disposable as IDisposable).dispose) {
// isDisposableObject
(disposable as IDisposable).dispose();
} else if (
disposable.constructor &&
disposable.call &&
disposable.apply
) {
// isFunction
(disposable as Disposer)();
}
}
} catch (e) {
console.error(e);
}
};