@idlebox/common 1.3.15 → 1.3.17

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.
Files changed (90) hide show
  1. package/{dist → lib}/common-alpha.d.ts +10 -0
  2. package/{dist → lib}/common-beta.d.ts +10 -0
  3. package/{dist → lib}/common-public.d.ts +10 -0
  4. package/{dist → lib}/common.d.ts +10 -0
  5. package/lib/function/callbackList.cjs +4 -0
  6. package/lib/function/callbackList.cjs.map +1 -1
  7. package/lib/function/callbackList.js +4 -0
  8. package/lib/function/callbackList.js.map +1 -1
  9. package/lib/string/concatType.generated.cjs +8 -0
  10. package/lib/string/concatType.generated.cjs.map +1 -1
  11. package/lib/string/concatType.generated.js +8 -0
  12. package/lib/string/concatType.generated.js.map +1 -1
  13. package/{dist → lib}/tsdoc-metadata.json +1 -1
  14. package/package.json +13 -12
  15. package/src/array/arrayDiff.ts +31 -0
  16. package/src/array/arraySame.ts +15 -0
  17. package/src/array/arrayUnique.ts +50 -0
  18. package/src/array/normalizeArray.ts +13 -0
  19. package/src/array/sortAlpha.ts +15 -0
  20. package/src/date/consts.ts +5 -0
  21. package/src/date/isInvalid.ts +6 -0
  22. package/src/date/sibling.ts +28 -0
  23. package/src/date/timeString.ts +150 -0
  24. package/src/date/unix.ts +13 -0
  25. package/src/debugging/serializable.ts +146 -0
  26. package/src/debugging/tryInspect.ts +37 -0
  27. package/src/error/convertUnknown.ts +10 -0
  28. package/src/error/getFrame.ts +13 -0
  29. package/src/function/asyncCallbackList.ts +75 -0
  30. package/src/function/callbackList.ts +88 -0
  31. package/src/function/delayCallbackList.ts +45 -0
  32. package/src/function/functionName.ts +39 -0
  33. package/src/index.generated.ts +289 -0
  34. package/src/lifecycle/dispose/bridges/rxjs.ts +6 -0
  35. package/src/lifecycle/dispose/disposableEvent.ts +117 -0
  36. package/src/lifecycle/dispose/disposedError.ts +16 -0
  37. package/src/lifecycle/dispose/lifecycle.async.ts +61 -0
  38. package/src/lifecycle/dispose/lifecycle.global.ts +61 -0
  39. package/src/lifecycle/dispose/lifecycle.sync.ts +79 -0
  40. package/src/lifecycle/dispose/lifecycle.ts +28 -0
  41. package/src/lifecycle/event/event.ts +81 -0
  42. package/src/lifecycle/event/memorized.ts +39 -0
  43. package/src/lifecycle/promise/cancel.ts +16 -0
  44. package/src/lifecycle/promise/cancellationToken/driver.browser.ts +55 -0
  45. package/src/lifecycle/promise/cancellationToken/driver.common.ts +43 -0
  46. package/src/lifecycle/promise/cancellationToken/source.ts +48 -0
  47. package/src/lifecycle/promise/deferredPromise.ts +104 -0
  48. package/src/lifecycle/timeout/timeout.ts +48 -0
  49. package/src/lifecycle/timeout/timeoutError.ts +16 -0
  50. package/src/log/logger.ts +148 -0
  51. package/src/mapSet/customSet.ts +91 -0
  52. package/src/mapSet/extendMap.ts +40 -0
  53. package/src/misc/assertNotNull.ts +21 -0
  54. package/src/object/definePublicConstant.ts +10 -0
  55. package/src/object/initOnRead.ts +27 -0
  56. package/src/object/objectPath.ts +10 -0
  57. package/src/object/objectSame.ts +52 -0
  58. package/src/path/isAbsolute.ts +11 -0
  59. package/src/path/normalizePath.ts +8 -0
  60. package/src/path/pathArray.ts +42 -0
  61. package/src/platform/globalObject.ts +22 -0
  62. package/src/platform/globalSingleton.ts +82 -0
  63. package/src/platform/globalSymbol.ts +36 -0
  64. package/src/platform/os.ts +46 -0
  65. package/src/promise/awaitIterator.ts +19 -0
  66. package/src/promise/finishAllPromise.ts +50 -0
  67. package/src/promise/promiseBool.ts +10 -0
  68. package/src/promise/promisePool.ts +40 -0
  69. package/src/promise/timeoutPromisePool.ts +22 -0
  70. package/src/reflection/classes/hookClass.ts +47 -0
  71. package/src/reflection/classes/singleton.ts +33 -0
  72. package/src/reflection/methods/bind.ts +30 -0
  73. package/src/reflection/methods/initOnRead.ts +11 -0
  74. package/src/reflection/methods/memorize.ts +33 -0
  75. package/src/string/castCase.ts +44 -0
  76. package/src/string/concatType.generated.ts +265 -0
  77. package/src/string/concatType.generator.ts +31 -0
  78. package/src/string/escapeRegexp.ts +4 -0
  79. package/src/string/pad2.ts +11 -0
  80. package/src/string/sizeString.ts +52 -0
  81. package/src/tsconfig.json +14 -0
  82. package/src/typingHelper/deep.partial.ts +16 -0
  83. package/src/typingHelper/deep.readonly.ts +16 -0
  84. package/src/typingHelper/deep.required.ts +16 -0
  85. package/src/typingHelper/deep.writable.ts +16 -0
  86. package/src/typingHelper/literal.ts +1 -0
  87. package/docs/common.api.json +0 -21737
  88. package/docs/common.api.md +0 -1139
  89. package/docs/package-public.d.ts +0 -952
  90. package/docs/tsdoc-metadata.json +0 -11
@@ -0,0 +1,289 @@
1
+ // DO NOT EDIT THIS FILE
2
+ // @ts-ignore
3
+ /* eslint-disable */
4
+
5
+ /* array/arrayDiff.ts */
6
+ // Identifiers
7
+ export {IArrayUpdate} from "./array/arrayDiff";
8
+ export {arrayDiff} from "./array/arrayDiff";
9
+ /* array/arraySame.ts */
10
+ // Identifiers
11
+ export {isArraySame} from "./array/arraySame";
12
+ /* array/arrayUnique.ts */
13
+ // Identifiers
14
+ export {arrayUnique} from "./array/arrayUnique";
15
+ export {arrayUniqueReference} from "./array/arrayUnique";
16
+ export {IUniqueIdFactory} from "./array/arrayUnique";
17
+ export {uniqueFilter} from "./array/arrayUnique";
18
+ /* array/normalizeArray.ts */
19
+ // Identifiers
20
+ export {normalizeArray} from "./array/normalizeArray";
21
+ /* array/sortAlpha.ts */
22
+ // Identifiers
23
+ export {sortByString} from "./array/sortAlpha";
24
+ /* date/consts.ts */
25
+ // Identifiers
26
+ export {oneSecond} from "./date/consts";
27
+ export {oneMinute} from "./date/consts";
28
+ export {oneHour} from "./date/consts";
29
+ export {oneDay} from "./date/consts";
30
+ export {oneWeek} from "./date/consts";
31
+ /* date/isInvalid.ts */
32
+ // Identifiers
33
+ export {isDateInvalid} from "./date/isInvalid";
34
+ /* date/sibling.ts */
35
+ // Identifiers
36
+ export {nextSecond} from "./date/sibling";
37
+ export {nextMinute} from "./date/sibling";
38
+ export {nextHour} from "./date/sibling";
39
+ export {nextDay} from "./date/sibling";
40
+ export {nextWeek} from "./date/sibling";
41
+ export {nextMonth} from "./date/sibling";
42
+ export {nextYear} from "./date/sibling";
43
+ /* string/pad2.ts */
44
+ // Identifiers
45
+ export {pad2} from "./string/pad2";
46
+ /* date/timeString.ts */
47
+ // Identifiers
48
+ export {humanDate} from "./date/timeString";
49
+ /* date/unix.ts */
50
+ // Identifiers
51
+ export {getTimeStamp} from "./date/unix";
52
+ export {fromTimeStamp} from "./date/unix";
53
+ /* debugging/serializable.ts */
54
+ // Identifiers
55
+ export {isScalar} from "./debugging/serializable";
56
+ export {SerializableKind} from "./debugging/serializable";
57
+ export {isSerializable} from "./debugging/serializable";
58
+ export {getTypeOf} from "./debugging/serializable";
59
+ export {assertSerializable} from "./debugging/serializable";
60
+ /* debugging/tryInspect.ts */
61
+ // Identifiers
62
+ export {tryInspect} from "./debugging/tryInspect";
63
+ /* error/getFrame.ts */
64
+ // Identifiers
65
+ export {getErrorFrame} from "./error/getFrame";
66
+ /* error/convertUnknown.ts */
67
+ // Identifiers
68
+ export {convertCatchedError} from "./error/convertUnknown";
69
+ /* function/functionName.ts */
70
+ // Identifiers
71
+ export {NamedFunction} from "./function/functionName";
72
+ export {functionName} from "./function/functionName";
73
+ export {nameFunction} from "./function/functionName";
74
+ export {MaybeNamedFunction} from "./function/functionName";
75
+ export {assertFunctionHasName} from "./function/functionName";
76
+ /* function/asyncCallbackList.ts */
77
+ // Identifiers
78
+ export {MyAsyncCallback} from "./function/asyncCallbackList";
79
+ export {AsyncCallbackList} from "./function/asyncCallbackList";
80
+ /* function/callbackList.ts */
81
+ // Identifiers
82
+ export {MyCallback} from "./function/callbackList";
83
+ export {CallbackList} from "./function/callbackList";
84
+ /* function/delayCallbackList.ts */
85
+ // Identifiers
86
+ export {MyDelayCallback} from "./function/delayCallbackList";
87
+ export {DelayCallbackList} from "./function/delayCallbackList";
88
+ /* lifecycle/event/event.ts */
89
+ // Identifiers
90
+ export {EventHandler} from "./lifecycle/event/event";
91
+ export {EventRegister} from "./lifecycle/event/event";
92
+ export {Emitter} from "./lifecycle/event/event";
93
+ /* lifecycle/dispose/lifecycle.ts */
94
+ // Identifiers
95
+ export {IDisposableBaseInternal} from "./lifecycle/dispose/lifecycle";
96
+ export {IDisposable} from "./lifecycle/dispose/lifecycle";
97
+ export {IAsyncDisposable} from "./lifecycle/dispose/lifecycle";
98
+ export {toDisposable} from "./lifecycle/dispose/lifecycle";
99
+ /* lifecycle/dispose/disposableEvent.ts */
100
+ // Identifiers
101
+ export {IEventListenerOptions} from "./lifecycle/dispose/disposableEvent";
102
+ export {IEventHostObject} from "./lifecycle/dispose/disposableEvent";
103
+ export {IEventEmitterObject} from "./lifecycle/dispose/disposableEvent";
104
+ export {addDisposableEventListener} from "./lifecycle/dispose/disposableEvent";
105
+ /* lifecycle/dispose/disposedError.ts */
106
+ // Identifiers
107
+ export {DisposedError} from "./lifecycle/dispose/disposedError";
108
+ export {isDisposedError} from "./lifecycle/dispose/disposedError";
109
+ /* lifecycle/dispose/lifecycle.async.ts */
110
+ // Identifiers
111
+ export {AsyncDisposable} from "./lifecycle/dispose/lifecycle.async";
112
+ /* platform/globalObject.ts */
113
+ // Identifiers
114
+ export {globalObject} from "./platform/globalObject";
115
+ export {ensureGlobalObject} from "./platform/globalObject";
116
+ export {x} from "./platform/globalObject";
117
+ /* platform/globalSingleton.ts */
118
+ // Identifiers
119
+ export {globalSingletonStrong} from "./platform/globalSingleton";
120
+ export {globalSingletonDelete} from "./platform/globalSingleton";
121
+ export {globalSingleton} from "./platform/globalSingleton";
122
+ /* platform/globalSymbol.ts */
123
+ // Identifiers
124
+ export {createSymbol} from "./platform/globalSymbol";
125
+ export {deleteSymbol} from "./platform/globalSymbol";
126
+ /* lifecycle/dispose/lifecycle.global.ts */
127
+ // Identifiers
128
+ export {registerGlobalLifecycle} from "./lifecycle/dispose/lifecycle.global";
129
+ export {ensureDisposeGlobal} from "./lifecycle/dispose/lifecycle.global";
130
+ export {disposeGlobal} from "./lifecycle/dispose/lifecycle.global";
131
+ export {LifecycleObject} from "./lifecycle/dispose/lifecycle.global";
132
+ /* lifecycle/dispose/lifecycle.sync.ts */
133
+ // Identifiers
134
+ export {DisposableOnce} from "./lifecycle/dispose/lifecycle.sync";
135
+ export {Disposable} from "./lifecycle/dispose/lifecycle.sync";
136
+ /* lifecycle/dispose/bridges/rxjs.ts */
137
+ // Identifiers
138
+ export {unsubscribableToDisposable} from "./lifecycle/dispose/bridges/rxjs";
139
+ /* lifecycle/event/memorized.ts */
140
+ // Identifiers
141
+ export {MemorizedEmitter} from "./lifecycle/event/memorized";
142
+ /* lifecycle/promise/cancel.ts */
143
+ // Identifiers
144
+ export {CanceledError} from "./lifecycle/promise/cancel";
145
+ export {isCanceledError} from "./lifecycle/promise/cancel";
146
+ /* lifecycle/promise/deferredPromise.ts */
147
+ // Identifiers
148
+ export {ValueCallback} from "./lifecycle/promise/deferredPromise";
149
+ export {ProgressCallback} from "./lifecycle/promise/deferredPromise";
150
+ export {IProgressHolder} from "./lifecycle/promise/deferredPromise";
151
+ export {DeferredPromise} from "./lifecycle/promise/deferredPromise";
152
+ /* lifecycle/promise/cancellationToken/driver.common.ts */
153
+ // Identifiers
154
+ /* lifecycle/promise/cancellationToken/source.ts */
155
+ // Identifiers
156
+ export {CancellationToken} from "./lifecycle/promise/cancellationToken/source";
157
+ export {CancellationTokenSource} from "./lifecycle/promise/cancellationToken/source";
158
+ /* lifecycle/promise/cancellationToken/driver.browser.ts */
159
+ // Identifiers
160
+ /* lifecycle/timeout/timeoutError.ts */
161
+ // Identifiers
162
+ export {TimeoutError} from "./lifecycle/timeout/timeoutError";
163
+ export {isTimeoutError} from "./lifecycle/timeout/timeoutError";
164
+ /* lifecycle/timeout/timeout.ts */
165
+ // Identifiers
166
+ export {timeout} from "./lifecycle/timeout/timeout";
167
+ export {sleep} from "./lifecycle/timeout/timeout";
168
+ export {timeoutPromise} from "./lifecycle/timeout/timeout";
169
+ /* platform/os.ts */
170
+ // Identifiers
171
+ export {isElectron} from "./platform/os";
172
+ export {isElectronRenderer} from "./platform/os";
173
+ export {isElectronMain} from "./platform/os";
174
+ export {isWindows} from "./platform/os";
175
+ export {isMacintosh} from "./platform/os";
176
+ export {isLinux} from "./platform/os";
177
+ export {isNative} from "./platform/os";
178
+ export {isWeb} from "./platform/os";
179
+ export {userAgent} from "./platform/os";
180
+ export {sepList} from "./platform/os";
181
+ /* log/logger.ts */
182
+ // Identifiers
183
+ export {ColorKind} from "./log/logger";
184
+ export {WrappedConsoleOptions} from "./log/logger";
185
+ export {WrappedConsole} from "./log/logger";
186
+ /* mapSet/customSet.ts */
187
+ // Identifiers
188
+ export {Finder} from "./mapSet/customSet";
189
+ export {RegexpFinder} from "./mapSet/customSet";
190
+ export {CustomSet} from "./mapSet/customSet";
191
+ /* mapSet/extendMap.ts */
192
+ // Identifiers
193
+ export {MapLike} from "./mapSet/extendMap";
194
+ export {ExtendMap} from "./mapSet/extendMap";
195
+ /* misc/assertNotNull.ts */
196
+ // Identifiers
197
+ export {assertNotNull} from "./misc/assertNotNull";
198
+ export {throwNull} from "./misc/assertNotNull";
199
+ /* object/definePublicConstant.ts */
200
+ // Identifiers
201
+ export {definePublicConstant} from "./object/definePublicConstant";
202
+ /* object/initOnRead.ts */
203
+ // Identifiers
204
+ export {InitFunc} from "./object/initOnRead";
205
+ export {initOnRead} from "./object/initOnRead";
206
+ /* object/objectPath.ts */
207
+ // Identifiers
208
+ export {objectPath} from "./object/objectPath";
209
+ /* object/objectSame.ts */
210
+ // Identifiers
211
+ export {isObjectSame} from "./object/objectSame";
212
+ export {isObjectSameRecursive} from "./object/objectSame";
213
+ /* path/isAbsolute.ts */
214
+ // Identifiers
215
+ export {isAbsolute} from "./path/isAbsolute";
216
+ /* path/normalizePath.ts */
217
+ // Identifiers
218
+ export {normalizePath} from "./path/normalizePath";
219
+ /* path/pathArray.ts */
220
+ // Identifiers
221
+ export {PathArray} from "./path/pathArray";
222
+ /* promise/awaitIterator.ts */
223
+ // Identifiers
224
+ export {awaitIterator} from "./promise/awaitIterator";
225
+ /* promise/finishAllPromise.ts */
226
+ // Identifiers
227
+ export {PromiseResultArray} from "./promise/finishAllPromise";
228
+ export {finishAllPromise} from "./promise/finishAllPromise";
229
+ /* promise/promiseBool.ts */
230
+ // Identifiers
231
+ export {promiseBool} from "./promise/promiseBool";
232
+ /* promise/promisePool.ts */
233
+ // Identifiers
234
+ export {PromisePool} from "./promise/promisePool";
235
+ /* promise/timeoutPromisePool.ts */
236
+ // Identifiers
237
+ export {TimeoutPromisePool} from "./promise/timeoutPromisePool";
238
+ /* reflection/classes/hookClass.ts */
239
+ // Identifiers
240
+ export {hookClassSymbol} from "./reflection/classes/hookClass";
241
+ export {hookClass} from "./reflection/classes/hookClass";
242
+ /* reflection/classes/singleton.ts */
243
+ // Identifiers
244
+ export {singletonSymbol} from "./reflection/classes/singleton";
245
+ export {SingletonType} from "./reflection/classes/singleton";
246
+ export {singleton} from "./reflection/classes/singleton";
247
+ /* reflection/methods/bind.ts */
248
+ // Identifiers
249
+ export {bindThis} from "./reflection/methods/bind";
250
+ /* reflection/methods/initOnRead.ts */
251
+ // Identifiers
252
+ export {init} from "./reflection/methods/initOnRead";
253
+ /* reflection/methods/memorize.ts */
254
+ // Identifiers
255
+ export {memorizeValueSymbol} from "./reflection/methods/memorize";
256
+ export {memo} from "./reflection/methods/memorize";
257
+ /* string/castCase.ts */
258
+ // Identifiers
259
+ export {camelCase} from "./string/castCase";
260
+ export {ucfirst} from "./string/castCase";
261
+ export {lcfirst} from "./string/castCase";
262
+ export {linux_case} from "./string/castCase";
263
+ export {linux_case_hyphen} from "./string/castCase";
264
+ /* string/concatType.generated.ts */
265
+ // Identifiers
266
+ export {concatStringType} from "./string/concatType.generated";
267
+ /* string/escapeRegexp.ts */
268
+ // Identifiers
269
+ export {escapeRegExp} from "./string/escapeRegexp";
270
+ /* string/sizeString.ts */
271
+ // Identifiers
272
+ export {humanSizeSI} from "./string/sizeString";
273
+ export {humanSize} from "./string/sizeString";
274
+ export {humanSpeed} from "./string/sizeString";
275
+ /* typingHelper/literal.ts */
276
+ // Identifiers
277
+ export {Primitive} from "./typingHelper/literal";
278
+ /* typingHelper/deep.partial.ts */
279
+ // Identifiers
280
+ export {DeepPartial} from "./typingHelper/deep.partial";
281
+ /* typingHelper/deep.readonly.ts */
282
+ // Identifiers
283
+ export {DeepReadonly} from "./typingHelper/deep.readonly";
284
+ /* typingHelper/deep.required.ts */
285
+ // Identifiers
286
+ export {DeepNonNullable} from "./typingHelper/deep.required";
287
+ /* typingHelper/deep.writable.ts */
288
+ // Identifiers
289
+ export {DeepWriteable} from "./typingHelper/deep.writable";
@@ -0,0 +1,6 @@
1
+ interface Unsubscribable {
2
+ unsubscribe(): void;
3
+ }
4
+ export function unsubscribableToDisposable(subscription: Unsubscribable) {
5
+ return { dispose: () => subscription.unsubscribe() };
6
+ }
@@ -0,0 +1,117 @@
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
+ }
@@ -0,0 +1,16 @@
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
+ }
@@ -0,0 +1,61 @@
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
+ }
@@ -0,0 +1,61 @@
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
+ }
@@ -0,0 +1,79 @@
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
+ }
@@ -0,0 +1,28 @@
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
+ }