@idlebox/common 1.3.4 → 1.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/package-public.d.ts +40 -0
- package/generate/main.ts +56 -0
- package/generate/package.json +3 -0
- package/generate/tsconfig.json +9 -0
- package/lib/_export_all_in_one_index.cjs +3 -1
- package/lib/_export_all_in_one_index.cjs.map +1 -1
- package/lib/_export_all_in_one_index.js +1 -0
- package/lib/_export_all_in_one_index.js.map +1 -1
- package/lib/string/concatType.generated.cjs +9 -0
- package/lib/string/concatType.generated.cjs.map +10 -0
- package/lib/string/concatType.generated.js +5 -0
- package/lib/string/concatType.generated.js.map +1 -0
- package/package.json +2 -1
- package/src/array/arrayDiff.ts +0 -31
- package/src/array/arraySame.ts +0 -15
- package/src/array/arrayUnique.ts +0 -50
- package/src/array/normalizeArray.ts +0 -13
- package/src/array/sortAlpha.ts +0 -15
- package/src/date/consts.ts +0 -5
- package/src/date/isInvalid.ts +0 -6
- package/src/date/sibling.ts +0 -28
- package/src/date/timeString.ts +0 -150
- package/src/date/unix.ts +0 -13
- package/src/debugging/tryInspect.ts +0 -37
- package/src/error/convertUnknown.ts +0 -10
- package/src/error/getFrame.ts +0 -13
- package/src/function/asyncCallbackList.ts +0 -75
- package/src/function/callbackList.ts +0 -84
- package/src/function/delayCallbackList.ts +0 -45
- package/src/function/functionName.ts +0 -39
- package/src/lifecycle/dispose/bridges/rxjs.ts +0 -6
- package/src/lifecycle/dispose/disposableEvent.ts +0 -117
- package/src/lifecycle/dispose/disposedError.ts +0 -16
- package/src/lifecycle/dispose/lifecycle.async.ts +0 -61
- package/src/lifecycle/dispose/lifecycle.global.ts +0 -61
- package/src/lifecycle/dispose/lifecycle.sync.ts +0 -79
- package/src/lifecycle/dispose/lifecycle.ts +0 -28
- package/src/lifecycle/event/event.ts +0 -63
- package/src/lifecycle/promise/cancel.ts +0 -16
- package/src/lifecycle/promise/cancellationToken/driver.browser.ts +0 -55
- package/src/lifecycle/promise/cancellationToken/driver.common.ts +0 -43
- package/src/lifecycle/promise/cancellationToken/source.ts +0 -48
- package/src/lifecycle/promise/deferredPromise.ts +0 -102
- package/src/lifecycle/timeout/timeout.ts +0 -48
- package/src/lifecycle/timeout/timeoutError.ts +0 -16
- package/src/log/logger.ts +0 -148
- package/src/mapSet/customSet.ts +0 -91
- package/src/mapSet/extendMap.ts +0 -40
- package/src/misc/assertNotNull.ts +0 -21
- package/src/object/definePublicConstant.ts +0 -10
- package/src/object/initOnRead.ts +0 -27
- package/src/object/objectPath.ts +0 -10
- package/src/object/objectSame.ts +0 -52
- package/src/path/isAbsolute.ts +0 -11
- package/src/path/normalizePath.ts +0 -8
- package/src/path/pathArray.ts +0 -42
- package/src/platform/globalObject.ts +0 -18
- package/src/platform/globalSingleton.ts +0 -82
- package/src/platform/globalSymbol.ts +0 -36
- package/src/platform/os.ts +0 -45
- package/src/promise/awaitIterator.ts +0 -19
- package/src/promise/finishAllPromise.ts +0 -50
- package/src/promise/promiseBool.ts +0 -10
- package/src/promise/promisePool.ts +0 -40
- package/src/promise/timeoutPromisePool.ts +0 -22
- package/src/reflection/classes/hookClass.ts +0 -47
- package/src/reflection/classes/singleton.ts +0 -33
- package/src/reflection/methods/bind.ts +0 -30
- package/src/reflection/methods/initOnRead.ts +0 -11
- package/src/reflection/methods/memorize.ts +0 -33
- package/src/string/castCase.ts +0 -44
- package/src/string/escapeRegexp.ts +0 -4
- package/src/string/pad2.ts +0 -11
- package/src/string/sizeString.ts +0 -52
- package/src/tsconfig.json +0 -12
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { nameFunction } from './functionName';
|
|
2
|
-
|
|
3
|
-
export interface MyAsyncCallback<Argument extends unknown[]> {
|
|
4
|
-
displayName?: string;
|
|
5
|
-
|
|
6
|
-
(...param: Argument): Promise<void | undefined | boolean> | void | undefined | boolean;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* like CallbackList, but async
|
|
11
|
-
*/
|
|
12
|
-
export class AsyncCallbackList<Argument extends unknown[]> {
|
|
13
|
-
protected list: MyAsyncCallback<Argument>[] = [];
|
|
14
|
-
protected running: boolean = false;
|
|
15
|
-
|
|
16
|
-
constructor() {
|
|
17
|
-
this.run = (this.run as any).bind(this);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
count() {
|
|
21
|
-
return this.list.length;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
reset() {
|
|
25
|
-
if (this.running) {
|
|
26
|
-
throw new Error("Can not reset when it's running.");
|
|
27
|
-
}
|
|
28
|
-
this.list.length = 0;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @param name optional name of `item` (will assign displayName to `item`)
|
|
33
|
-
* @returns function list length
|
|
34
|
-
*/
|
|
35
|
-
add(item: MyAsyncCallback<Argument>, name?: string): number {
|
|
36
|
-
if (this.running) {
|
|
37
|
-
throw new Error("Can not add callback when it's running.");
|
|
38
|
-
}
|
|
39
|
-
if (name) {
|
|
40
|
-
nameFunction(name, item);
|
|
41
|
-
}
|
|
42
|
-
return this.list.push(item);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @returns if removed: return `item`; if did not exists: return null
|
|
47
|
-
*/
|
|
48
|
-
remove(item: MyAsyncCallback<Argument>): null | MyAsyncCallback<Argument> {
|
|
49
|
-
if (this.running) {
|
|
50
|
-
throw new Error("Can not remove callback when it's running.");
|
|
51
|
-
}
|
|
52
|
-
const found = this.list.indexOf(item);
|
|
53
|
-
if (found !== -1) {
|
|
54
|
-
return this.list.splice(found, 1)[0];
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Stop run if one callback return `true`
|
|
61
|
-
* @returns {boolean} true if one callback return true
|
|
62
|
-
*/
|
|
63
|
-
async run(...argument: Argument): Promise<boolean> {
|
|
64
|
-
this.running = true;
|
|
65
|
-
let ret: boolean | undefined | void;
|
|
66
|
-
for (const cb of this.list) {
|
|
67
|
-
ret = await cb(...argument);
|
|
68
|
-
if (ret === true) {
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
this.running = false;
|
|
73
|
-
return ret || false;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { nameFunction } from './functionName';
|
|
2
|
-
|
|
3
|
-
export interface MyCallback<Argument extends unknown[]> {
|
|
4
|
-
displayName?: string;
|
|
5
|
-
|
|
6
|
-
(...param: Argument): any;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Manage a list of callback
|
|
11
|
-
*/
|
|
12
|
-
export class CallbackList<Argument extends unknown[]> {
|
|
13
|
-
protected list: MyCallback<Argument>[] = [];
|
|
14
|
-
protected running: boolean = false;
|
|
15
|
-
protected stop: boolean = false;
|
|
16
|
-
|
|
17
|
-
constructor() {
|
|
18
|
-
this.run = (this.run as any).bind(this);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
count() {
|
|
22
|
-
return this.list.length;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
reset() {
|
|
26
|
-
if (this.running) {
|
|
27
|
-
throw new Error("Can not reset when it's running.");
|
|
28
|
-
}
|
|
29
|
-
this.list.length = 0;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @param name optional name of `item` (will assign displayName to `item`)
|
|
34
|
-
* @returns function list length
|
|
35
|
-
*/
|
|
36
|
-
add(item: MyCallback<Argument>, name?: string): number {
|
|
37
|
-
if (this.running) {
|
|
38
|
-
throw new Error("Can not add callback when it's running.");
|
|
39
|
-
}
|
|
40
|
-
if (name) {
|
|
41
|
-
nameFunction(name, item);
|
|
42
|
-
}
|
|
43
|
-
return this.list.push(item);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @returns if removed: return `item`; if did not exists: return null
|
|
48
|
-
*/
|
|
49
|
-
remove(item: MyCallback<Argument>): null | MyCallback<Argument> {
|
|
50
|
-
if (this.running) {
|
|
51
|
-
throw new Error("Can not remove callback when it's running.");
|
|
52
|
-
}
|
|
53
|
-
const found = this.list.indexOf(item);
|
|
54
|
-
if (found !== -1) {
|
|
55
|
-
return this.list.splice(found, 1)[0];
|
|
56
|
-
}
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* @returns {boolean} true if every callback called, false if stop in middle
|
|
62
|
-
*/
|
|
63
|
-
run(...argument: Argument): boolean {
|
|
64
|
-
if (this.running) {
|
|
65
|
-
throw new Error("can not run CallbackList in it's callback.");
|
|
66
|
-
}
|
|
67
|
-
this.stop = false;
|
|
68
|
-
|
|
69
|
-
this.running = true;
|
|
70
|
-
for (const cb of this.list) {
|
|
71
|
-
if (this.stop) break;
|
|
72
|
-
cb(...argument);
|
|
73
|
-
}
|
|
74
|
-
this.running = false;
|
|
75
|
-
|
|
76
|
-
const ret = !this.stop;
|
|
77
|
-
this.stop = false;
|
|
78
|
-
return ret;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
stopRun() {
|
|
82
|
-
this.stop = true;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { nameFunction } from './functionName';
|
|
2
|
-
|
|
3
|
-
export interface MyDelayCallback<Argument extends unknown[]> {
|
|
4
|
-
displayName?: string;
|
|
5
|
-
|
|
6
|
-
(...param: Argument): void;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* remember arguments after run
|
|
11
|
-
* run all later added function with memorized argument
|
|
12
|
-
*/
|
|
13
|
-
export class DelayCallbackList<Argument extends unknown[]> {
|
|
14
|
-
private delayArgument?: Argument;
|
|
15
|
-
private delayComplete: boolean = false;
|
|
16
|
-
|
|
17
|
-
protected list?: MyDelayCallback<Argument>[] = [];
|
|
18
|
-
|
|
19
|
-
count() {
|
|
20
|
-
return this.list?.length ?? 0;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
add(item: MyDelayCallback<Argument>, name?: string) {
|
|
24
|
-
if (name) {
|
|
25
|
-
nameFunction(name, item);
|
|
26
|
-
}
|
|
27
|
-
if (this.delayComplete) {
|
|
28
|
-
item(...this.delayArgument!);
|
|
29
|
-
} else {
|
|
30
|
-
this.list!.push(item);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
run(argument: Argument) {
|
|
35
|
-
if (this.delayComplete) {
|
|
36
|
-
throw new Error('call to delay callback twice!');
|
|
37
|
-
}
|
|
38
|
-
this.delayComplete = true;
|
|
39
|
-
this.delayArgument = argument;
|
|
40
|
-
this.list!.forEach((cb) => {
|
|
41
|
-
cb(...argument);
|
|
42
|
-
});
|
|
43
|
-
delete this.list;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Function with displayName
|
|
3
|
-
*/
|
|
4
|
-
export interface NamedFunction extends Function {
|
|
5
|
-
displayName: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Get displayName/name of a function
|
|
10
|
-
*/
|
|
11
|
-
export function functionName(func: Function) {
|
|
12
|
-
return (func as NamedFunction).displayName || func.name;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Set displayName of a function
|
|
17
|
-
*/
|
|
18
|
-
export function nameFunction<T extends Function>(name: string, func: T): T & NamedFunction {
|
|
19
|
-
return Object.assign(func, {
|
|
20
|
-
displayName: name,
|
|
21
|
-
inspect() {
|
|
22
|
-
return `[Function: ${name}]`;
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface MaybeNamedFunction extends Function {
|
|
28
|
-
displayName?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Assert function must have oneof displayName/name property
|
|
33
|
-
*/
|
|
34
|
-
export function assertFunctionHasName(func: MaybeNamedFunction) {
|
|
35
|
-
if (!func.displayName && !func.name) {
|
|
36
|
-
console.error(func);
|
|
37
|
-
throw new TypeError('function must have name!');
|
|
38
|
-
}
|
|
39
|
-
}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { IDisposable } from './lifecycle';
|
|
2
|
-
|
|
3
|
-
declare const AbortController: new () => any;
|
|
4
|
-
|
|
5
|
-
export interface IEventListenerOptions {
|
|
6
|
-
capture?: boolean;
|
|
7
|
-
once?: boolean;
|
|
8
|
-
passive?: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface IEventHostObject<T extends Function> {
|
|
12
|
-
addEventListener(type: string, handler: T, options?: IEventListenerOptions): any;
|
|
13
|
-
removeEventListener(type: string, handler: T, options?: IEventListenerOptions): any;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface IEventEmitterObject<T extends Function> {
|
|
17
|
-
addListener(type: string, handler: T): any;
|
|
18
|
-
removeListener(type: string, handler: T): any;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function addDisposableEventListener<T extends Function>(
|
|
22
|
-
target: IEventHostObject<T> | IEventEmitterObject<T>,
|
|
23
|
-
type: string,
|
|
24
|
-
options: IEventListenerOptions,
|
|
25
|
-
handler: T
|
|
26
|
-
): IDisposable;
|
|
27
|
-
|
|
28
|
-
export function addDisposableEventListener<T extends Function>(
|
|
29
|
-
target: IEventHostObject<T> | IEventEmitterObject<T>,
|
|
30
|
-
type: string,
|
|
31
|
-
handler: T
|
|
32
|
-
): IDisposable;
|
|
33
|
-
|
|
34
|
-
export function addDisposableEventListener<T extends Function>(
|
|
35
|
-
target: IEventHostObject<T> | IEventEmitterObject<T>,
|
|
36
|
-
type: string,
|
|
37
|
-
_options: IEventListenerOptions | T | undefined,
|
|
38
|
-
_handler?: T
|
|
39
|
-
): IDisposable {
|
|
40
|
-
if (!_handler) {
|
|
41
|
-
if (typeof _options === 'function') {
|
|
42
|
-
_handler = _options;
|
|
43
|
-
_options = undefined;
|
|
44
|
-
} else {
|
|
45
|
-
throw new Error('missing handler');
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const handler = _handler as T;
|
|
50
|
-
const options = _options as IEventListenerOptions;
|
|
51
|
-
|
|
52
|
-
let remove: IDisposable['dispose'];
|
|
53
|
-
|
|
54
|
-
if ('addEventListener' in target) {
|
|
55
|
-
if (passiveSupported === undefined || abortSupported === undefined) {
|
|
56
|
-
checkAllSupport(target);
|
|
57
|
-
}
|
|
58
|
-
const [abort, xOptions] = check(options);
|
|
59
|
-
target.addEventListener(type, handler, xOptions);
|
|
60
|
-
if (abort) {
|
|
61
|
-
remove = () => {
|
|
62
|
-
abort;
|
|
63
|
-
};
|
|
64
|
-
} else {
|
|
65
|
-
remove = () => {
|
|
66
|
-
target.removeEventListener(type, handler, xOptions);
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
target.addListener(type, handler);
|
|
71
|
-
remove = () => {
|
|
72
|
-
target.removeListener(type, handler);
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
return { dispose: remove };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
let passiveSupported: boolean;
|
|
79
|
-
let abortSupported: boolean;
|
|
80
|
-
function check(options: IEventListenerOptions = {}): [{ abort(): void } | undefined, IEventListenerOptions] {
|
|
81
|
-
if (!passiveSupported) {
|
|
82
|
-
return [undefined, (options.capture || false) as any];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (abortSupported) {
|
|
86
|
-
const controller = new AbortController();
|
|
87
|
-
(options as any).signal = controller.signal;
|
|
88
|
-
return [controller, options];
|
|
89
|
-
} else {
|
|
90
|
-
return [undefined, options];
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function checkAllSupport(ele: IEventHostObject<any>) {
|
|
95
|
-
passiveSupported = checkSupport('passive', ele);
|
|
96
|
-
abortSupported = typeof AbortController !== 'undefined' && checkSupport('signal', ele);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function checkSupport(field: string, ele: IEventHostObject<any>) {
|
|
100
|
-
let supported = false;
|
|
101
|
-
try {
|
|
102
|
-
const options = {
|
|
103
|
-
get [field]() {
|
|
104
|
-
// This function will be called when the browser
|
|
105
|
-
// attempts to access the passive property.
|
|
106
|
-
supported = true;
|
|
107
|
-
return undefined;
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
ele.addEventListener('_test_', null, options);
|
|
112
|
-
ele.removeEventListener('_test_', null, options);
|
|
113
|
-
} catch (err) {
|
|
114
|
-
supported = false;
|
|
115
|
-
}
|
|
116
|
-
return supported;
|
|
117
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { getErrorFrame } from '../../error/getFrame';
|
|
2
|
-
import { tryInspect } from '../../debugging/tryInspect';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Error when call dispose() twice
|
|
6
|
-
*/
|
|
7
|
-
export class DisposedError extends Error {
|
|
8
|
-
constructor(object: any, previous: Error) {
|
|
9
|
-
super(`Object [${tryInspect(object)}] has already disposed at ${getErrorFrame(previous, 2)}.`);
|
|
10
|
-
this.name = 'Warning';
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function isDisposedError(error: any): boolean {
|
|
15
|
-
return error instanceof DisposedError;
|
|
16
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { convertCatchedError } from '../../error/convertUnknown';
|
|
2
|
-
import { Emitter, EventRegister } from '../event/event';
|
|
3
|
-
import { DisposedError } from './disposedError';
|
|
4
|
-
import { IAsyncDisposable, IDisposableBaseInternal } from './lifecycle';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Async version of Disposable
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
export class AsyncDisposable implements IAsyncDisposable, IDisposableBaseInternal {
|
|
11
|
-
private readonly _disposables: IAsyncDisposable[] = [];
|
|
12
|
-
|
|
13
|
-
protected readonly _onDisposeError = new Emitter<Error>();
|
|
14
|
-
public readonly onDisposeError: EventRegister<Error> = this._onDisposeError.register;
|
|
15
|
-
|
|
16
|
-
protected readonly _onBeforeDispose = new Emitter<void>();
|
|
17
|
-
public readonly onBeforeDispose: EventRegister<void> = this._onBeforeDispose.register;
|
|
18
|
-
|
|
19
|
-
private _disposed?: Error;
|
|
20
|
-
|
|
21
|
-
public get hasDisposed() {
|
|
22
|
-
return !!this._disposed;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @throws if already disposed
|
|
27
|
-
*/
|
|
28
|
-
public assertNotDisposed() {
|
|
29
|
-
if (this._disposed) {
|
|
30
|
-
throw new DisposedError(this, this._disposed);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* register a disposable object
|
|
36
|
-
*/
|
|
37
|
-
public _register<T extends IAsyncDisposable>(d: T): T {
|
|
38
|
-
this.assertNotDisposed();
|
|
39
|
-
this._disposables.unshift(d);
|
|
40
|
-
return d;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public async dispose(): Promise<void> {
|
|
44
|
-
if (this._disposed) {
|
|
45
|
-
console.warn(new DisposedError(this, this._disposed).message);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
this._onBeforeDispose.fireNoError();
|
|
49
|
-
this._disposed = new Error('disposed');
|
|
50
|
-
|
|
51
|
-
this._disposables.push(this._onBeforeDispose);
|
|
52
|
-
this._disposables.push(this._onDisposeError);
|
|
53
|
-
for (const cb of this._disposables) {
|
|
54
|
-
try {
|
|
55
|
-
await cb.dispose();
|
|
56
|
-
} catch (e) {
|
|
57
|
-
this._onDisposeError.fire(convertCatchedError(e));
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { globalSingletonStrong } from '../../platform/globalSingleton';
|
|
2
|
-
import { createSymbol } from '../../platform/globalSymbol';
|
|
3
|
-
import { IDisposable } from './lifecycle';
|
|
4
|
-
import { AsyncDisposable } from './lifecycle.async';
|
|
5
|
-
|
|
6
|
-
const symbol = createSymbol('lifecycle', 'application');
|
|
7
|
-
|
|
8
|
-
function create() {
|
|
9
|
-
return new AsyncDisposable();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Add object into global disposable store, it will be dispose when call to `disposeGlobal`
|
|
14
|
-
*/
|
|
15
|
-
export function registerGlobalLifecycle(object: IDisposable) {
|
|
16
|
-
globalSingletonStrong(symbol, create)._register(object);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Same as disposeGlobal, but do not throw by duplicate call
|
|
21
|
-
*/
|
|
22
|
-
export function ensureDisposeGlobal() {
|
|
23
|
-
const obj = globalSingletonStrong<AsyncDisposable>(symbol);
|
|
24
|
-
if (obj && !obj.hasDisposed) {
|
|
25
|
-
return Promise.resolve(obj.dispose());
|
|
26
|
-
} else {
|
|
27
|
-
return Promise.resolve();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Dispose the global disposable store
|
|
33
|
-
* this function must be manually called by user, when registerGlobalLifecycle is used
|
|
34
|
-
*
|
|
35
|
-
* @throws when call twice
|
|
36
|
-
*/
|
|
37
|
-
export function disposeGlobal() {
|
|
38
|
-
const obj = globalSingletonStrong<AsyncDisposable>(symbol);
|
|
39
|
-
if (obj && obj.hasDisposed) {
|
|
40
|
-
throw new Error('global already disposed.');
|
|
41
|
-
} else if (obj) {
|
|
42
|
-
return Promise.resolve(obj.dispose());
|
|
43
|
-
} else {
|
|
44
|
-
return Promise.resolve();
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Note: sub-class should singleton
|
|
50
|
-
* @alpha
|
|
51
|
-
*/
|
|
52
|
-
export abstract class LifecycleObject extends AsyncDisposable {
|
|
53
|
-
/** sub-class should shutdown program in this method */
|
|
54
|
-
protected abstract done(): void;
|
|
55
|
-
|
|
56
|
-
public async dispose(): Promise<void> {
|
|
57
|
-
return super.dispose().finally(() => {
|
|
58
|
-
this.done();
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { DisposedError } from './disposedError';
|
|
2
|
-
import { Emitter, EventRegister } from '../event/event';
|
|
3
|
-
import { IDisposable, IDisposableBaseInternal } from './lifecycle';
|
|
4
|
-
|
|
5
|
-
export abstract class DisposableOnce implements IDisposable {
|
|
6
|
-
private _disposed?: Error;
|
|
7
|
-
|
|
8
|
-
public get hasDisposed() {
|
|
9
|
-
return !!this._disposed;
|
|
10
|
-
}
|
|
11
|
-
public dispose(): void {
|
|
12
|
-
if (this._disposed) {
|
|
13
|
-
console.warn(new DisposedError(this, this._disposed).message);
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
this._disposed = new Error('disposed');
|
|
17
|
-
this._dispose();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
protected abstract _dispose(): void;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Standalone disposable class, can use as instance or base class.
|
|
25
|
-
*/
|
|
26
|
-
export class Disposable implements IDisposable, IDisposableBaseInternal {
|
|
27
|
-
private readonly _disposables: IDisposable[] = [];
|
|
28
|
-
|
|
29
|
-
protected readonly _onDisposeError = new Emitter<Error>();
|
|
30
|
-
public readonly onDisposeError: EventRegister<Error> = this._onDisposeError.register;
|
|
31
|
-
|
|
32
|
-
protected readonly _onBeforeDispose = new Emitter<void>();
|
|
33
|
-
public readonly onBeforeDispose: EventRegister<void> = this._onBeforeDispose.register;
|
|
34
|
-
|
|
35
|
-
private _disposed?: Error;
|
|
36
|
-
|
|
37
|
-
public get hasDisposed() {
|
|
38
|
-
return !!this._disposed;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @throws if already disposed
|
|
43
|
-
*/
|
|
44
|
-
public assertNotDisposed() {
|
|
45
|
-
if (this._disposed) {
|
|
46
|
-
throw new DisposedError(this, this._disposed);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public _register<T extends IDisposable>(d: T): T {
|
|
51
|
-
this.assertNotDisposed();
|
|
52
|
-
this._disposables.unshift(d);
|
|
53
|
-
return d;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public dispose(): void {
|
|
57
|
-
if (this._disposed) {
|
|
58
|
-
console.warn(new DisposedError(this, this._disposed).message);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
this._onBeforeDispose.fireNoError();
|
|
62
|
-
this._disposed = new Error('disposed');
|
|
63
|
-
|
|
64
|
-
this._disposables.push(this._onBeforeDispose);
|
|
65
|
-
this._disposables.push(this._onDisposeError);
|
|
66
|
-
for (const item of this._disposables.values()) {
|
|
67
|
-
try {
|
|
68
|
-
item.dispose();
|
|
69
|
-
} catch (e) {
|
|
70
|
-
if (e instanceof Error) {
|
|
71
|
-
this._onDisposeError.fire(e);
|
|
72
|
-
} else {
|
|
73
|
-
console.error('error during dispose, throw:', e);
|
|
74
|
-
this._onDisposeError.fire(new Error('' + e));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { EventRegister } from '../event/event';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @private
|
|
5
|
-
*/
|
|
6
|
-
export interface IDisposableBaseInternal {
|
|
7
|
-
onDisposeError: EventRegister<Error>;
|
|
8
|
-
onBeforeDispose: EventRegister<void>;
|
|
9
|
-
readonly hasDisposed: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/** @public */
|
|
13
|
-
export interface IDisposable {
|
|
14
|
-
dispose(): void;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** @public */
|
|
18
|
-
export interface IAsyncDisposable {
|
|
19
|
-
dispose(): void | Promise<void>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Convert "dispose function" to disposable object
|
|
24
|
-
* @public
|
|
25
|
-
*/
|
|
26
|
-
export function toDisposable(fn: () => void): IDisposable {
|
|
27
|
-
return { dispose: fn };
|
|
28
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { IDisposable } from '../dispose/lifecycle';
|
|
2
|
-
|
|
3
|
-
export interface EventHandler<T> {
|
|
4
|
-
(data: T): void;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface EventRegister<T> {
|
|
8
|
-
(callback: EventHandler<T>): IDisposable;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class Emitter<T> implements IDisposable {
|
|
12
|
-
private readonly _callbacks: EventHandler<T>[] = [];
|
|
13
|
-
|
|
14
|
-
constructor() {
|
|
15
|
-
this.handle = this.handle.bind(this);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
listenerCount() {
|
|
19
|
-
return this._callbacks.length;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
fire(data: T) {
|
|
23
|
-
for (const callback of this._callbacks) {
|
|
24
|
-
callback(data);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Same with `fire`, but do not stop run when catch error
|
|
30
|
-
*/
|
|
31
|
-
public fireNoError(data: T) {
|
|
32
|
-
for (const callback of this._callbacks) {
|
|
33
|
-
try {
|
|
34
|
-
callback(data);
|
|
35
|
-
} catch (e) {
|
|
36
|
-
console.error('Error ignored: ', e);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
get register(): EventRegister<T> {
|
|
42
|
-
return this.handle;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
handle(callback: EventHandler<T>): IDisposable {
|
|
46
|
-
this._callbacks.unshift(callback);
|
|
47
|
-
return {
|
|
48
|
-
dispose: () => {
|
|
49
|
-
const index = this._callbacks.indexOf(callback);
|
|
50
|
-
if (index !== -1) {
|
|
51
|
-
this._callbacks.splice(index, 1);
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
dispose() {
|
|
58
|
-
this._callbacks.length = 0;
|
|
59
|
-
this.fire = this.handle = () => {
|
|
60
|
-
throw new Error('Event is disposed');
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const canceledName = 'Canceled';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Error when cancel() is called
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export class CanceledError extends Error {
|
|
8
|
-
constructor() {
|
|
9
|
-
super(canceledName);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** @public */
|
|
14
|
-
export function isCanceledError(error: any): boolean {
|
|
15
|
-
return error instanceof CanceledError;
|
|
16
|
-
}
|