@celljs/core 3.8.0 → 3.9.0
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/esm/browser/application/frontend-application-state.d.ts +4 -0
- package/esm/browser/application/frontend-application-state.js +14 -0
- package/esm/browser/application/frontend-application-state.js.map +1 -0
- package/esm/browser/application/frontend-application.d.ts +30 -0
- package/esm/browser/application/frontend-application.js +142 -0
- package/esm/browser/application/frontend-application.js.map +1 -0
- package/esm/browser/application/index.d.ts +2 -0
- package/esm/browser/application/index.js +3 -0
- package/esm/browser/application/index.js.map +1 -0
- package/esm/browser/browser.d.ts +22 -0
- package/esm/browser/browser.js +48 -0
- package/esm/browser/browser.js.map +1 -0
- package/esm/browser/index.d.ts +3 -0
- package/esm/browser/index.js +4 -0
- package/esm/browser/index.js.map +1 -0
- package/esm/browser/shell/application-shell.d.ts +4 -0
- package/esm/browser/shell/application-shell.js +18 -0
- package/esm/browser/shell/application-shell.js.map +1 -0
- package/esm/browser/shell/index.d.ts +2 -0
- package/esm/browser/shell/index.js +3 -0
- package/esm/browser/shell/index.js.map +1 -0
- package/esm/browser/shell/shell-protocol.d.ts +4 -0
- package/esm/browser/shell/shell-protocol.js +2 -0
- package/esm/browser/shell/shell-protocol.js.map +1 -0
- package/esm/browser/static-module.d.ts +3 -0
- package/esm/browser/static-module.js +4 -0
- package/esm/browser/static-module.js.map +1 -0
- package/esm/common/annotation/aspect.d.ts +7 -0
- package/esm/common/annotation/aspect.js +10 -0
- package/esm/common/annotation/aspect.js.map +1 -0
- package/esm/common/annotation/autowired-provider.d.ts +7 -0
- package/esm/common/annotation/autowired-provider.js +25 -0
- package/esm/common/annotation/autowired-provider.js.map +1 -0
- package/esm/common/annotation/autowired.d.ts +22 -0
- package/esm/common/annotation/autowired.js +48 -0
- package/esm/common/annotation/autowired.js.map +1 -0
- package/esm/common/annotation/component.d.ts +57 -0
- package/esm/common/annotation/component.js +71 -0
- package/esm/common/annotation/component.js.map +1 -0
- package/esm/common/annotation/constant.d.ts +8 -0
- package/esm/common/annotation/constant.js +12 -0
- package/esm/common/annotation/constant.js.map +1 -0
- package/esm/common/annotation/decorate.d.ts +1 -0
- package/esm/common/annotation/decorate.js +2 -0
- package/esm/common/annotation/decorate.js.map +1 -0
- package/esm/common/annotation/detached/autowired-provider.d.ts +2 -0
- package/esm/common/annotation/detached/autowired-provider.js +12 -0
- package/esm/common/annotation/detached/autowired-provider.js.map +1 -0
- package/esm/common/annotation/detached/autowired.d.ts +2 -0
- package/esm/common/annotation/detached/autowired.js +9 -0
- package/esm/common/annotation/detached/autowired.js.map +1 -0
- package/esm/common/annotation/detached/index.d.ts +3 -0
- package/esm/common/annotation/detached/index.js +4 -0
- package/esm/common/annotation/detached/index.js.map +1 -0
- package/esm/common/annotation/detached/value.d.ts +2 -0
- package/esm/common/annotation/detached/value.js +9 -0
- package/esm/common/annotation/detached/value.js.map +1 -0
- package/esm/common/annotation/index.d.ts +17 -0
- package/esm/common/annotation/index.js +18 -0
- package/esm/common/annotation/index.js.map +1 -0
- package/esm/common/annotation/inject.d.ts +1 -0
- package/esm/common/annotation/inject.js +2 -0
- package/esm/common/annotation/inject.js.map +1 -0
- package/esm/common/annotation/injectable.d.ts +1 -0
- package/esm/common/annotation/injectable.js +2 -0
- package/esm/common/annotation/injectable.js.map +1 -0
- package/esm/common/annotation/named.d.ts +1 -0
- package/esm/common/annotation/named.js +2 -0
- package/esm/common/annotation/named.js.map +1 -0
- package/esm/common/annotation/optional.d.ts +1 -0
- package/esm/common/annotation/optional.js +2 -0
- package/esm/common/annotation/optional.js.map +1 -0
- package/esm/common/annotation/post-construct.d.ts +1 -0
- package/esm/common/annotation/post-construct.js +2 -0
- package/esm/common/annotation/post-construct.js.map +1 -0
- package/esm/common/annotation/pre-destroy.d.ts +1 -0
- package/esm/common/annotation/pre-destroy.js +2 -0
- package/esm/common/annotation/pre-destroy.js.map +1 -0
- package/esm/common/annotation/service.d.ts +4 -0
- package/esm/common/annotation/service.js +14 -0
- package/esm/common/annotation/service.js.map +1 -0
- package/esm/common/annotation/tagged.d.ts +1 -0
- package/esm/common/annotation/tagged.js +2 -0
- package/esm/common/annotation/tagged.js.map +1 -0
- package/esm/common/annotation/target-name.d.ts +1 -0
- package/esm/common/annotation/target-name.js +2 -0
- package/esm/common/annotation/target-name.js.map +1 -0
- package/esm/common/annotation/unmanaged.d.ts +1 -0
- package/esm/common/annotation/unmanaged.js +2 -0
- package/esm/common/annotation/unmanaged.js.map +1 -0
- package/esm/common/annotation/value.d.ts +15 -0
- package/esm/common/annotation/value.js +50 -0
- package/esm/common/annotation/value.js.map +1 -0
- package/esm/common/aop/aop-protocol.d.ts +38 -0
- package/esm/common/aop/aop-protocol.js +8 -0
- package/esm/common/aop/aop-protocol.js.map +1 -0
- package/esm/common/aop/aop-proxy-factory.d.ts +6 -0
- package/esm/common/aop/aop-proxy-factory.js +67 -0
- package/esm/common/aop/aop-proxy-factory.js.map +1 -0
- package/esm/common/aop/class-filter.d.ts +6 -0
- package/esm/common/aop/class-filter.js +28 -0
- package/esm/common/aop/class-filter.js.map +1 -0
- package/esm/common/aop/index.d.ts +3 -0
- package/esm/common/aop/index.js +4 -0
- package/esm/common/aop/index.js.map +1 -0
- package/esm/common/application/application-entry.d.ts +1 -0
- package/esm/common/application/application-entry.js +23 -0
- package/esm/common/application/application-entry.js.map +1 -0
- package/esm/common/application/application-error.d.ts +20 -0
- package/esm/common/application/application-error.js +44 -0
- package/esm/common/application/application-error.js.map +1 -0
- package/esm/common/application/application-factory.d.ts +8 -0
- package/esm/common/application/application-factory.js +19 -0
- package/esm/common/application/application-factory.js.map +1 -0
- package/esm/common/application/application-protocol.d.ts +52 -0
- package/esm/common/application/application-protocol.js +103 -0
- package/esm/common/application/application-protocol.js.map +1 -0
- package/esm/common/application/application-state.d.ts +3 -0
- package/esm/common/application/application-state.js +15 -0
- package/esm/common/application/application-state.js.map +1 -0
- package/esm/common/application/application.d.ts +6 -0
- package/esm/common/application/application.js +31 -0
- package/esm/common/application/application.js.map +1 -0
- package/esm/common/application/index.d.ts +2 -0
- package/esm/common/application/index.js +3 -0
- package/esm/common/application/index.js.map +1 -0
- package/esm/common/config/config-protocol.d.ts +4 -0
- package/esm/common/config/config-protocol.js +2 -0
- package/esm/common/config/config-protocol.js.map +1 -0
- package/esm/common/config/config-provider.d.ts +6 -0
- package/esm/common/config/config-provider.js +26 -0
- package/esm/common/config/config-provider.js.map +1 -0
- package/esm/common/config/config-util.d.ts +4 -0
- package/esm/common/config/config-util.js +16 -0
- package/esm/common/config/config-util.js.map +1 -0
- package/esm/common/config/dynamic-config.d.ts +3 -0
- package/esm/common/config/dynamic-config.js +3 -0
- package/esm/common/config/dynamic-config.js.map +1 -0
- package/esm/common/config/index.d.ts +3 -0
- package/esm/common/config/index.js +4 -0
- package/esm/common/config/index.js.map +1 -0
- package/esm/common/constants.d.ts +5 -0
- package/esm/common/constants.js +6 -0
- package/esm/common/constants.js.map +1 -0
- package/esm/common/container/auto-bind.d.ts +3 -0
- package/esm/common/container/auto-bind.js +98 -0
- package/esm/common/container/auto-bind.js.map +1 -0
- package/esm/common/container/container-factory.d.ts +4 -0
- package/esm/common/container/container-factory.js +9 -0
- package/esm/common/container/container-factory.js.map +1 -0
- package/esm/common/container/container-provider.d.ts +6 -0
- package/esm/common/container/container-provider.js +23 -0
- package/esm/common/container/container-provider.js.map +1 -0
- package/esm/common/container/container-util.d.ts +9 -0
- package/esm/common/container/container-util.js +29 -0
- package/esm/common/container/container-util.js.map +1 -0
- package/esm/common/container/dynamic-container.d.ts +2 -0
- package/esm/common/container/dynamic-container.js +4 -0
- package/esm/common/container/dynamic-container.js.map +1 -0
- package/esm/common/container/index.d.ts +8 -0
- package/esm/common/container/index.js +6 -0
- package/esm/common/container/index.js.map +1 -0
- package/esm/common/container/scope.d.ts +5 -0
- package/esm/common/container/scope.js +7 -0
- package/esm/common/container/scope.js.map +1 -0
- package/esm/common/el/core-context-initializer.d.ts +6 -0
- package/esm/common/el/core-context-initializer.js +36 -0
- package/esm/common/el/core-context-initializer.js.map +1 -0
- package/esm/common/el/expression-compiler.d.ts +19 -0
- package/esm/common/el/expression-compiler.js +187 -0
- package/esm/common/el/expression-compiler.js.map +1 -0
- package/esm/common/el/expression-context-provider.d.ts +9 -0
- package/esm/common/el/expression-context-provider.js +43 -0
- package/esm/common/el/expression-context-provider.js.map +1 -0
- package/esm/common/el/expression-handler.d.ts +11 -0
- package/esm/common/el/expression-handler.js +95 -0
- package/esm/common/el/expression-handler.js.map +1 -0
- package/esm/common/el/expression-protocol.d.ts +32 -0
- package/esm/common/el/expression-protocol.js +6 -0
- package/esm/common/el/expression-protocol.js.map +1 -0
- package/esm/common/el/index.d.ts +6 -0
- package/esm/common/el/index.js +7 -0
- package/esm/common/el/index.js.map +1 -0
- package/esm/common/el/jexl-engine-provider.d.ts +5 -0
- package/esm/common/el/jexl-engine-provider.js +22 -0
- package/esm/common/el/jexl-engine-provider.js.map +1 -0
- package/esm/common/error/custom-error.d.ts +26 -0
- package/esm/common/error/custom-error.js +40 -0
- package/esm/common/error/custom-error.js.map +1 -0
- package/esm/common/error/error-protocol.d.ts +3 -0
- package/esm/common/error/error-protocol.js +2 -0
- package/esm/common/error/error-protocol.js.map +1 -0
- package/esm/common/error/illegal-argument-error.d.ts +3 -0
- package/esm/common/error/illegal-argument-error.js +4 -0
- package/esm/common/error/illegal-argument-error.js.map +1 -0
- package/esm/common/error/illegal-state-error.d.ts +3 -0
- package/esm/common/error/illegal-state-error.js +4 -0
- package/esm/common/error/illegal-state-error.js.map +1 -0
- package/esm/common/error/index.d.ts +5 -0
- package/esm/common/error/index.js +6 -0
- package/esm/common/error/index.js.map +1 -0
- package/esm/common/error/invalid-mime-type-error.d.ts +5 -0
- package/esm/common/error/invalid-mime-type-error.js +8 -0
- package/esm/common/error/invalid-mime-type-error.js.map +1 -0
- package/esm/common/index.d.ts +12 -0
- package/esm/common/index.js +13 -0
- package/esm/common/index.js.map +1 -0
- package/esm/common/logger/abstract-logger.d.ts +18 -0
- package/esm/common/logger/abstract-logger.js +69 -0
- package/esm/common/logger/abstract-logger.js.map +1 -0
- package/esm/common/logger/index.d.ts +4 -0
- package/esm/common/logger/index.js +5 -0
- package/esm/common/logger/index.js.map +1 -0
- package/esm/common/logger/logger-protocol.d.ts +30 -0
- package/esm/common/logger/logger-protocol.js +8 -0
- package/esm/common/logger/logger-protocol.js.map +1 -0
- package/esm/common/logger/logger-service.d.ts +4 -0
- package/esm/common/logger/logger-service.js +18 -0
- package/esm/common/logger/logger-service.js.map +1 -0
- package/esm/common/logger/logger.d.ts +7 -0
- package/esm/common/logger/logger.js +35 -0
- package/esm/common/logger/logger.js.map +1 -0
- package/esm/common/provider/component-filter-registry.d.ts +11 -0
- package/esm/common/provider/component-filter-registry.js +66 -0
- package/esm/common/provider/component-filter-registry.js.map +1 -0
- package/esm/common/provider/index.d.ts +5 -0
- package/esm/common/provider/index.js +6 -0
- package/esm/common/provider/index.js.map +1 -0
- package/esm/common/provider/provider-creator.d.ts +6 -0
- package/esm/common/provider/provider-creator.js +19 -0
- package/esm/common/provider/provider-creator.js.map +1 -0
- package/esm/common/provider/provider-protocol.d.ts +48 -0
- package/esm/common/provider/provider-protocol.js +5 -0
- package/esm/common/provider/provider-protocol.js.map +1 -0
- package/esm/common/provider/provider-util.d.ts +5 -0
- package/esm/common/provider/provider-util.js +10 -0
- package/esm/common/provider/provider-util.js.map +1 -0
- package/esm/common/provider/provider.d.ts +13 -0
- package/esm/common/provider/provider.js +54 -0
- package/esm/common/provider/provider.js.map +1 -0
- package/esm/common/static-module.d.ts +3 -0
- package/esm/common/static-module.js +9 -0
- package/esm/common/static-module.js.map +1 -0
- package/esm/common/test/index.d.ts +1 -0
- package/esm/common/test/index.js +2 -0
- package/esm/common/test/index.js.map +1 -0
- package/esm/common/test/mock-logger.d.ts +14 -0
- package/esm/common/test/mock-logger.js +41 -0
- package/esm/common/test/mock-logger.js.map +1 -0
- package/esm/common/test/test-module.d.ts +2 -0
- package/esm/common/test/test-module.js +12 -0
- package/esm/common/test/test-module.js.map +1 -0
- package/esm/common/utils/annotation-util.d.ts +4 -0
- package/esm/common/utils/annotation-util.js +24 -0
- package/esm/common/utils/annotation-util.js.map +1 -0
- package/esm/common/utils/assert.d.ts +173 -0
- package/esm/common/utils/assert.js +283 -0
- package/esm/common/utils/assert.js.map +1 -0
- package/esm/common/utils/async.d.ts +367 -0
- package/esm/common/utils/async.js +935 -0
- package/esm/common/utils/async.js.map +1 -0
- package/esm/common/utils/byte-util.d.ts +11 -0
- package/esm/common/utils/byte-util.js +75 -0
- package/esm/common/utils/byte-util.js.map +1 -0
- package/esm/common/utils/cancellation.d.ts +21 -0
- package/esm/common/utils/cancellation.js +89 -0
- package/esm/common/utils/cancellation.js.map +1 -0
- package/esm/common/utils/class-util.d.ts +2 -0
- package/esm/common/utils/class-util.js +19 -0
- package/esm/common/utils/class-util.js.map +1 -0
- package/esm/common/utils/disposable.d.ts +29 -0
- package/esm/common/utils/disposable.js +78 -0
- package/esm/common/utils/disposable.js.map +1 -0
- package/esm/common/utils/emitter.d.ts +63 -0
- package/esm/common/utils/emitter.js +221 -0
- package/esm/common/utils/emitter.js.map +1 -0
- package/esm/common/utils/error-util.d.ts +19 -0
- package/esm/common/utils/error-util.js +35 -0
- package/esm/common/utils/error-util.js.map +1 -0
- package/esm/common/utils/event.d.ts +40 -0
- package/esm/common/utils/event.js +90 -0
- package/esm/common/utils/event.js.map +1 -0
- package/esm/common/utils/global-util.d.ts +1 -0
- package/esm/common/utils/global-util.js +16 -0
- package/esm/common/utils/global-util.js.map +1 -0
- package/esm/common/utils/index.d.ts +21 -0
- package/esm/common/utils/index.js +22 -0
- package/esm/common/utils/index.js.map +1 -0
- package/esm/common/utils/metadata-util.d.ts +1 -0
- package/esm/common/utils/metadata-util.js +25 -0
- package/esm/common/utils/metadata-util.js.map +1 -0
- package/esm/common/utils/mime-type-util.d.ts +36 -0
- package/esm/common/utils/mime-type-util.js +168 -0
- package/esm/common/utils/mime-type-util.js.map +1 -0
- package/esm/common/utils/mime-type.d.ts +36 -0
- package/esm/common/utils/mime-type.js +284 -0
- package/esm/common/utils/mime-type.js.map +1 -0
- package/esm/common/utils/os.d.ts +19 -0
- package/esm/common/utils/os.js +46 -0
- package/esm/common/utils/os.js.map +1 -0
- package/esm/common/utils/prioritizeable.d.ts +17 -0
- package/esm/common/utils/prioritizeable.js +38 -0
- package/esm/common/utils/prioritizeable.js.map +1 -0
- package/esm/common/utils/promise-util.d.ts +5 -0
- package/esm/common/utils/promise-util.js +9 -0
- package/esm/common/utils/promise-util.js.map +1 -0
- package/esm/common/utils/proxy-util.d.ts +4 -0
- package/esm/common/utils/proxy-util.js +34 -0
- package/esm/common/utils/proxy-util.js.map +1 -0
- package/esm/common/utils/types.d.ts +16 -0
- package/esm/common/utils/types.js +2 -0
- package/esm/common/utils/types.js.map +1 -0
- package/esm/common/utils/url-util.d.ts +3 -0
- package/esm/common/utils/url-util.js +56 -0
- package/esm/common/utils/url-util.js.map +1 -0
- package/esm/common/utils/uuid.d.ts +2 -0
- package/esm/common/utils/uuid.js +61 -0
- package/esm/common/utils/uuid.js.map +1 -0
- package/esm/node/application/backend-application-state.d.ts +4 -0
- package/esm/node/application/backend-application-state.js +14 -0
- package/esm/node/application/backend-application-state.js.map +1 -0
- package/esm/node/application/backend-application.d.ts +10 -0
- package/esm/node/application/backend-application.js +52 -0
- package/esm/node/application/backend-application.js.map +1 -0
- package/esm/node/application/index.d.ts +2 -0
- package/esm/node/application/index.js +3 -0
- package/esm/node/application/index.js.map +1 -0
- package/esm/node/constants.d.ts +15 -0
- package/esm/node/constants.js +16 -0
- package/esm/node/constants.js.map +1 -0
- package/esm/node/context/context.d.ts +15 -0
- package/esm/node/context/context.js +58 -0
- package/esm/node/context/context.js.map +1 -0
- package/esm/node/context/index.d.ts +1 -0
- package/esm/node/context/index.js +2 -0
- package/esm/node/context/index.js.map +1 -0
- package/esm/node/index.d.ts +3 -0
- package/esm/node/index.js +4 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/static-module.d.ts +3 -0
- package/esm/node/static-module.js +4 -0
- package/esm/node/static-module.js.map +1 -0
- package/esm/node/utils/crc.d.ts +1 -0
- package/esm/node/utils/crc.js +7 -0
- package/esm/node/utils/crc.js.map +1 -0
- package/esm/node/utils/index.d.ts +1 -0
- package/esm/node/utils/index.js +2 -0
- package/esm/node/utils/index.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/common/application/application-factory.d.ts +1 -0
- package/lib/common/application/application-factory.d.ts.map +1 -1
- package/lib/common/application/application-factory.js +1 -1
- package/lib/common/application/application-factory.js.map +1 -1
- package/package.json +124 -4
- package/src/common/application/application-factory.ts +1 -1
|
@@ -0,0 +1,935 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-shadow */
|
|
2
|
+
import { CancellationError, CancellationTokenSource } from './cancellation.js';
|
|
3
|
+
import { Disposable } from './disposable.js';
|
|
4
|
+
import { Emitter } from './emitter.js';
|
|
5
|
+
export function isThenable(obj) {
|
|
6
|
+
return !!obj && typeof obj.then === 'function';
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A function to allow a promise resolution to be delayed by a number of milliseconds. Usage is as follows:
|
|
10
|
+
*
|
|
11
|
+
* `const stringValue = await myPromise.then(delay(600)).then(value => value.toString());`
|
|
12
|
+
*
|
|
13
|
+
* @param ms the number of millisecond to delay
|
|
14
|
+
* @returns a function that returns a promise that returns the given value, but delayed
|
|
15
|
+
*/
|
|
16
|
+
export function delay(ms) {
|
|
17
|
+
return value => new Promise((resolve, reject) => {
|
|
18
|
+
setTimeout(() => resolve(value), ms);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Constructs a promise that will resolve after a given delay.
|
|
23
|
+
* @param ms the number of milliseconds to wait
|
|
24
|
+
*/
|
|
25
|
+
export async function wait(ms) {
|
|
26
|
+
await delay(ms)(undefined);
|
|
27
|
+
}
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
export function waitForEvent(event, ms, thisArg, disposables) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const registration = setTimeout(() => {
|
|
32
|
+
listener.dispose();
|
|
33
|
+
reject(new CancellationError());
|
|
34
|
+
}, ms);
|
|
35
|
+
const listener = event((evt) => {
|
|
36
|
+
clearTimeout(registration);
|
|
37
|
+
listener.dispose();
|
|
38
|
+
resolve(evt);
|
|
39
|
+
}, thisArg, disposables);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
export function createCancelablePromise(callback) {
|
|
43
|
+
const source = new CancellationTokenSource();
|
|
44
|
+
const thenable = callback(source.token);
|
|
45
|
+
const promise = new Promise((resolve, reject) => {
|
|
46
|
+
const subscription = source.token.onCancellationRequested(() => {
|
|
47
|
+
subscription.dispose();
|
|
48
|
+
source.dispose();
|
|
49
|
+
reject(new CancellationError());
|
|
50
|
+
});
|
|
51
|
+
Promise.resolve(thenable).then(value => {
|
|
52
|
+
subscription.dispose();
|
|
53
|
+
source.dispose();
|
|
54
|
+
resolve(value);
|
|
55
|
+
}, err => {
|
|
56
|
+
subscription.dispose();
|
|
57
|
+
source.dispose();
|
|
58
|
+
reject(err);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
return new class {
|
|
62
|
+
cancel() {
|
|
63
|
+
source.cancel();
|
|
64
|
+
}
|
|
65
|
+
then(resolve, reject) {
|
|
66
|
+
return promise.then(resolve, reject);
|
|
67
|
+
}
|
|
68
|
+
catch(reject) {
|
|
69
|
+
return this.then(undefined, reject);
|
|
70
|
+
}
|
|
71
|
+
finally(onfinally) {
|
|
72
|
+
return promise.finally(onfinally);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export function raceCancellation(promise, token, defaultValue) {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const ref = token.onCancellationRequested(() => {
|
|
79
|
+
ref.dispose();
|
|
80
|
+
resolve(defaultValue);
|
|
81
|
+
});
|
|
82
|
+
promise.then(resolve, reject).finally(() => ref.dispose());
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
export function raceCancellationError(promise, token) {
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
const ref = token.onCancellationRequested(() => {
|
|
88
|
+
ref.dispose();
|
|
89
|
+
reject(new CancellationError());
|
|
90
|
+
});
|
|
91
|
+
promise.then(resolve, reject).finally(() => ref.dispose());
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
export async function raceCancellablePromises(cancellablePromises) {
|
|
95
|
+
let resolvedPromiseIndex = -1;
|
|
96
|
+
const promises = cancellablePromises.map((promise, index) => promise.then(result => {
|
|
97
|
+
resolvedPromiseIndex = index;
|
|
98
|
+
return result;
|
|
99
|
+
}));
|
|
100
|
+
const result = await Promise.race(promises);
|
|
101
|
+
cancellablePromises.forEach((cancellablePromise, index) => {
|
|
102
|
+
if (index !== resolvedPromiseIndex) {
|
|
103
|
+
cancellablePromise.cancel();
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
export function raceTimeout(promise, timeout, onTimeout) {
|
|
109
|
+
let promiseResolve = undefined;
|
|
110
|
+
const timer = setTimeout(() => {
|
|
111
|
+
promiseResolve === null || promiseResolve === void 0 ? void 0 : promiseResolve(undefined);
|
|
112
|
+
onTimeout === null || onTimeout === void 0 ? void 0 : onTimeout();
|
|
113
|
+
}, timeout);
|
|
114
|
+
return Promise.race([
|
|
115
|
+
promise.finally(() => clearTimeout(timer)),
|
|
116
|
+
new Promise(resolve => promiseResolve = resolve)
|
|
117
|
+
]);
|
|
118
|
+
}
|
|
119
|
+
export function asPromise(callback) {
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
const item = callback();
|
|
122
|
+
if (isThenable(item)) {
|
|
123
|
+
item.then(resolve, reject);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
resolve(item);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A helper to prevent accumulation of sequential async tasks.
|
|
132
|
+
*
|
|
133
|
+
* Imagine a mail man with the sole task of delivering letters. As soon as
|
|
134
|
+
* a letter submitted for delivery, he drives to the destination, delivers it
|
|
135
|
+
* and returns to his base. Imagine that during the trip, N more letters were submitted.
|
|
136
|
+
* When the mail man returns, he picks those N letters and delivers them all in a
|
|
137
|
+
* single trip. Even though N+1 submissions occurred, only 2 deliveries were made.
|
|
138
|
+
*
|
|
139
|
+
* The throttler implements this via the queue() method, by providing it a task
|
|
140
|
+
* factory. Following the example:
|
|
141
|
+
*
|
|
142
|
+
* const throttler = new Throttler();
|
|
143
|
+
* const letters = [];
|
|
144
|
+
*
|
|
145
|
+
* function deliver() {
|
|
146
|
+
* const lettersToDeliver = letters;
|
|
147
|
+
* letters = [];
|
|
148
|
+
* return makeTheTrip(lettersToDeliver);
|
|
149
|
+
* }
|
|
150
|
+
*
|
|
151
|
+
* function onLetterReceived(l) {
|
|
152
|
+
* letters.push(l);
|
|
153
|
+
* throttler.queue(deliver);
|
|
154
|
+
* }
|
|
155
|
+
*/
|
|
156
|
+
export class Throttler {
|
|
157
|
+
constructor() {
|
|
158
|
+
this.activePromise = undefined;
|
|
159
|
+
this.queuedPromise = undefined;
|
|
160
|
+
this.queuedPromiseFactory = undefined;
|
|
161
|
+
}
|
|
162
|
+
queue(promiseFactory) {
|
|
163
|
+
if (this.activePromise) {
|
|
164
|
+
this.queuedPromiseFactory = promiseFactory;
|
|
165
|
+
if (!this.queuedPromise) {
|
|
166
|
+
const onComplete = () => {
|
|
167
|
+
this.queuedPromise = undefined;
|
|
168
|
+
const result = this.queue(this.queuedPromiseFactory);
|
|
169
|
+
this.queuedPromiseFactory = undefined;
|
|
170
|
+
return result;
|
|
171
|
+
};
|
|
172
|
+
this.queuedPromise = new Promise(resolve => {
|
|
173
|
+
this.activePromise.then(onComplete, onComplete).then(resolve);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return new Promise((resolve, reject) => {
|
|
177
|
+
this.queuedPromise.then(resolve, reject);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
this.activePromise = promiseFactory();
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
182
|
+
this.activePromise.then((result) => {
|
|
183
|
+
this.activePromise = undefined;
|
|
184
|
+
resolve(result);
|
|
185
|
+
}, (err) => {
|
|
186
|
+
this.activePromise = undefined;
|
|
187
|
+
reject(err);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
export class Sequencer {
|
|
193
|
+
constructor() {
|
|
194
|
+
this.current = Promise.resolve(undefined);
|
|
195
|
+
}
|
|
196
|
+
queue(promiseTask) {
|
|
197
|
+
return this.current = this.current.then(() => promiseTask(), () => promiseTask());
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
export class SequencerByKey {
|
|
201
|
+
constructor() {
|
|
202
|
+
this.promiseMap = new Map();
|
|
203
|
+
}
|
|
204
|
+
queue(key, promiseTask) {
|
|
205
|
+
var _a;
|
|
206
|
+
const runningPromise = (_a = this.promiseMap.get(key)) !== null && _a !== void 0 ? _a : Promise.resolve();
|
|
207
|
+
const newPromise = runningPromise
|
|
208
|
+
.catch(() => { })
|
|
209
|
+
.then(promiseTask)
|
|
210
|
+
.finally(() => {
|
|
211
|
+
if (this.promiseMap.get(key) === newPromise) {
|
|
212
|
+
this.promiseMap.delete(key);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
this.promiseMap.set(key, newPromise);
|
|
216
|
+
return newPromise;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const timeoutDeferred = (timeout, fn) => {
|
|
220
|
+
let scheduled = true;
|
|
221
|
+
const handle = setTimeout(() => {
|
|
222
|
+
scheduled = false;
|
|
223
|
+
fn();
|
|
224
|
+
}, timeout);
|
|
225
|
+
return {
|
|
226
|
+
isTriggered: () => scheduled,
|
|
227
|
+
dispose: () => {
|
|
228
|
+
clearTimeout(handle);
|
|
229
|
+
scheduled = false;
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
const microtaskDeferred = (fn) => {
|
|
234
|
+
let scheduled = true;
|
|
235
|
+
queueMicrotask(() => {
|
|
236
|
+
if (scheduled) {
|
|
237
|
+
scheduled = false;
|
|
238
|
+
fn();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
return {
|
|
242
|
+
isTriggered: () => scheduled,
|
|
243
|
+
dispose: () => {
|
|
244
|
+
scheduled = false;
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
/** Can be passed into the Delayed to defer using a microtask */
|
|
249
|
+
export const MicrotaskDelay = Symbol('MicrotaskDelay');
|
|
250
|
+
/**
|
|
251
|
+
* A helper to delay (debounce) execution of a task that is being requested often.
|
|
252
|
+
*
|
|
253
|
+
* Following the throttler, now imagine the mail man wants to optimize the number of
|
|
254
|
+
* trips proactively. The trip itself can be long, so he decides not to make the trip
|
|
255
|
+
* as soon as a letter is submitted. Instead he waits a while, in case more
|
|
256
|
+
* letters are submitted. After said waiting period, if no letters were submitted, he
|
|
257
|
+
* decides to make the trip. Imagine that N more letters were submitted after the first
|
|
258
|
+
* one, all within a short period of time between each other. Even though N+1
|
|
259
|
+
* submissions occurred, only 1 delivery was made.
|
|
260
|
+
*
|
|
261
|
+
* The delayer offers this behavior via the trigger() method, into which both the task
|
|
262
|
+
* to be executed and the waiting period (delay) must be passed in as arguments. Following
|
|
263
|
+
* the example:
|
|
264
|
+
*
|
|
265
|
+
* const delayer = new Delayer(WAITING_PERIOD);
|
|
266
|
+
* const letters = [];
|
|
267
|
+
*
|
|
268
|
+
* function letterReceived(l) {
|
|
269
|
+
* letters.push(l);
|
|
270
|
+
* delayer.trigger(() => { return makeTheTrip(); });
|
|
271
|
+
* }
|
|
272
|
+
*/
|
|
273
|
+
export class Delayer {
|
|
274
|
+
constructor(defaultDelay) {
|
|
275
|
+
this.defaultDelay = defaultDelay;
|
|
276
|
+
this.deferred = undefined;
|
|
277
|
+
this.completionPromise = undefined;
|
|
278
|
+
this.doResolve = undefined;
|
|
279
|
+
this.doReject = undefined;
|
|
280
|
+
this.task = undefined;
|
|
281
|
+
}
|
|
282
|
+
trigger(task, delay = this.defaultDelay) {
|
|
283
|
+
this.task = task;
|
|
284
|
+
this.cancelTimeout();
|
|
285
|
+
if (!this.completionPromise) {
|
|
286
|
+
this.completionPromise = new Promise((resolve, reject) => {
|
|
287
|
+
this.doResolve = resolve;
|
|
288
|
+
this.doReject = reject;
|
|
289
|
+
}).then(() => {
|
|
290
|
+
this.completionPromise = undefined;
|
|
291
|
+
this.doResolve = undefined;
|
|
292
|
+
if (this.task) {
|
|
293
|
+
const task = this.task;
|
|
294
|
+
this.task = undefined;
|
|
295
|
+
return task();
|
|
296
|
+
}
|
|
297
|
+
return undefined;
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
const fn = () => {
|
|
301
|
+
var _a;
|
|
302
|
+
this.deferred = undefined;
|
|
303
|
+
(_a = this.doResolve) === null || _a === void 0 ? void 0 : _a.call(this, undefined);
|
|
304
|
+
};
|
|
305
|
+
this.deferred = delay === MicrotaskDelay ? microtaskDeferred(fn) : timeoutDeferred(delay, fn);
|
|
306
|
+
return this.completionPromise;
|
|
307
|
+
}
|
|
308
|
+
isTriggered() {
|
|
309
|
+
var _a;
|
|
310
|
+
return !!((_a = this.deferred) === null || _a === void 0 ? void 0 : _a.isTriggered());
|
|
311
|
+
}
|
|
312
|
+
cancel() {
|
|
313
|
+
this.cancelTimeout();
|
|
314
|
+
if (this.completionPromise) {
|
|
315
|
+
if (this.doReject) {
|
|
316
|
+
this.doReject(new CancellationError());
|
|
317
|
+
}
|
|
318
|
+
this.completionPromise = undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
cancelTimeout() {
|
|
322
|
+
var _a;
|
|
323
|
+
(_a = this.deferred) === null || _a === void 0 ? void 0 : _a.dispose();
|
|
324
|
+
this.deferred = undefined;
|
|
325
|
+
}
|
|
326
|
+
dispose() {
|
|
327
|
+
this.cancel();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* A helper to delay execution of a task that is being requested often, while
|
|
332
|
+
* preventing accumulation of consecutive executions, while the task runs.
|
|
333
|
+
*
|
|
334
|
+
* The mail man is clever and waits for a certain amount of time, before going
|
|
335
|
+
* out to deliver letters. While the mail man is going out, more letters arrive
|
|
336
|
+
* and can only be delivered once he is back. Once he is back the mail man will
|
|
337
|
+
* do one more trip to deliver the letters that have accumulated while he was out.
|
|
338
|
+
*/
|
|
339
|
+
export class ThrottledDelayer {
|
|
340
|
+
constructor(defaultDelay) {
|
|
341
|
+
this.delayer = new Delayer(defaultDelay);
|
|
342
|
+
this.throttler = new Throttler();
|
|
343
|
+
}
|
|
344
|
+
trigger(promiseFactory, delay) {
|
|
345
|
+
return this.delayer.trigger(() => this.throttler.queue(promiseFactory), delay);
|
|
346
|
+
}
|
|
347
|
+
isTriggered() {
|
|
348
|
+
return this.delayer.isTriggered();
|
|
349
|
+
}
|
|
350
|
+
cancel() {
|
|
351
|
+
this.delayer.cancel();
|
|
352
|
+
}
|
|
353
|
+
dispose() {
|
|
354
|
+
this.delayer.dispose();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* A barrier that is initially closed and then becomes opened permanently.
|
|
359
|
+
*/
|
|
360
|
+
export class Barrier {
|
|
361
|
+
constructor() {
|
|
362
|
+
this._isOpen = false;
|
|
363
|
+
this._promise = new Promise((c, e) => {
|
|
364
|
+
this._completePromise = c;
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
isOpen() {
|
|
368
|
+
return this._isOpen;
|
|
369
|
+
}
|
|
370
|
+
open() {
|
|
371
|
+
this._isOpen = true;
|
|
372
|
+
this._completePromise(true);
|
|
373
|
+
}
|
|
374
|
+
wait() {
|
|
375
|
+
return this._promise;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* A barrier that is initially closed and then becomes opened permanently after a certain period of
|
|
380
|
+
* time or when open is called explicitly
|
|
381
|
+
*/
|
|
382
|
+
export class AutoOpenBarrier extends Barrier {
|
|
383
|
+
constructor(autoOpenTimeMs) {
|
|
384
|
+
super();
|
|
385
|
+
this._timeout = setTimeout(() => this.open(), autoOpenTimeMs);
|
|
386
|
+
}
|
|
387
|
+
open() {
|
|
388
|
+
clearTimeout(this._timeout);
|
|
389
|
+
super.open();
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
export function timeout(millis, token) {
|
|
393
|
+
if (!token) {
|
|
394
|
+
return createCancelablePromise(token => timeout(millis, token));
|
|
395
|
+
}
|
|
396
|
+
return new Promise((resolve, reject) => {
|
|
397
|
+
const handle = setTimeout(() => {
|
|
398
|
+
disposable.dispose();
|
|
399
|
+
resolve();
|
|
400
|
+
}, millis);
|
|
401
|
+
const disposable = token.onCancellationRequested(() => {
|
|
402
|
+
clearTimeout(handle);
|
|
403
|
+
disposable.dispose();
|
|
404
|
+
reject(new CancellationError());
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
export function disposableTimeout(handler, timeout = 0) {
|
|
409
|
+
const timer = setTimeout(handler, timeout);
|
|
410
|
+
return Disposable.create(() => clearTimeout(timer));
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Runs the provided list of promise factories in sequential order. The returned
|
|
414
|
+
* promise will complete to an array of results from each promise.
|
|
415
|
+
*/
|
|
416
|
+
export function sequence(promiseFactories) {
|
|
417
|
+
const results = [];
|
|
418
|
+
let index = 0;
|
|
419
|
+
const len = promiseFactories.length;
|
|
420
|
+
function next() {
|
|
421
|
+
return index < len ? promiseFactories[index++]() : undefined;
|
|
422
|
+
}
|
|
423
|
+
function thenHandler(result) {
|
|
424
|
+
if (result !== undefined && result !== undefined) {
|
|
425
|
+
results.push(result);
|
|
426
|
+
}
|
|
427
|
+
const n = next();
|
|
428
|
+
if (n) {
|
|
429
|
+
return n.then(thenHandler);
|
|
430
|
+
}
|
|
431
|
+
return Promise.resolve(results);
|
|
432
|
+
}
|
|
433
|
+
return Promise.resolve(undefined).then(thenHandler);
|
|
434
|
+
}
|
|
435
|
+
export function first(promiseFactories, shouldStop = t => !!t, defaultValue = undefined) {
|
|
436
|
+
let index = 0;
|
|
437
|
+
const len = promiseFactories.length;
|
|
438
|
+
const loop = () => {
|
|
439
|
+
if (index >= len) {
|
|
440
|
+
return Promise.resolve(defaultValue);
|
|
441
|
+
}
|
|
442
|
+
const factory = promiseFactories[index++];
|
|
443
|
+
const promise = Promise.resolve(factory());
|
|
444
|
+
return promise.then(result => {
|
|
445
|
+
if (shouldStop(result)) {
|
|
446
|
+
return Promise.resolve(result);
|
|
447
|
+
}
|
|
448
|
+
return loop();
|
|
449
|
+
});
|
|
450
|
+
};
|
|
451
|
+
return loop();
|
|
452
|
+
}
|
|
453
|
+
export function firstParallel(promiseList, shouldStop = t => !!t, defaultValue = undefined) {
|
|
454
|
+
if (promiseList.length === 0) {
|
|
455
|
+
return Promise.resolve(defaultValue);
|
|
456
|
+
}
|
|
457
|
+
let todo = promiseList.length;
|
|
458
|
+
const finish = () => {
|
|
459
|
+
var _a, _b;
|
|
460
|
+
todo = -1;
|
|
461
|
+
for (const promise of promiseList) {
|
|
462
|
+
(_b = (_a = promise).cancel) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
return new Promise((resolve, reject) => {
|
|
466
|
+
for (const promise of promiseList) {
|
|
467
|
+
promise.then(result => {
|
|
468
|
+
if (--todo >= 0 && shouldStop(result)) {
|
|
469
|
+
finish();
|
|
470
|
+
resolve(result);
|
|
471
|
+
}
|
|
472
|
+
else if (todo === 0) {
|
|
473
|
+
resolve(defaultValue);
|
|
474
|
+
}
|
|
475
|
+
})
|
|
476
|
+
.catch(err => {
|
|
477
|
+
if (--todo >= 0) {
|
|
478
|
+
finish();
|
|
479
|
+
reject(err);
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* A helper to queue N promises and run them all with a max degree of parallelism. The helper
|
|
487
|
+
* ensures that at any time no more than M promises are running at the same time.
|
|
488
|
+
*/
|
|
489
|
+
export class Limiter {
|
|
490
|
+
constructor(maxDegreeOfParalellism) {
|
|
491
|
+
this._size = 0;
|
|
492
|
+
this.maxDegreeOfParalellism = maxDegreeOfParalellism;
|
|
493
|
+
this.outstandingPromises = [];
|
|
494
|
+
this.runningPromises = 0;
|
|
495
|
+
this._onDrained = new Emitter();
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* An event that fires when every promise in the queue
|
|
499
|
+
* has started to execute. In other words: no work is
|
|
500
|
+
* pending to be scheduled.
|
|
501
|
+
*
|
|
502
|
+
* This is NOT an event that signals when all promises
|
|
503
|
+
* have finished though.
|
|
504
|
+
*/
|
|
505
|
+
get onDrained() {
|
|
506
|
+
return this._onDrained.event;
|
|
507
|
+
}
|
|
508
|
+
get size() {
|
|
509
|
+
return this._size;
|
|
510
|
+
}
|
|
511
|
+
queue(factory) {
|
|
512
|
+
this._size++;
|
|
513
|
+
return new Promise((c, e) => {
|
|
514
|
+
this.outstandingPromises.push({ factory, c, e });
|
|
515
|
+
this.consume();
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
consume() {
|
|
519
|
+
while (this.outstandingPromises.length && this.runningPromises < this.maxDegreeOfParalellism) {
|
|
520
|
+
const iLimitedTask = this.outstandingPromises.shift();
|
|
521
|
+
this.runningPromises++;
|
|
522
|
+
const promise = iLimitedTask.factory();
|
|
523
|
+
promise.then(iLimitedTask.c, iLimitedTask.e);
|
|
524
|
+
promise.then(() => this.consumed(), () => this.consumed());
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
consumed() {
|
|
528
|
+
this._size--;
|
|
529
|
+
this.runningPromises--;
|
|
530
|
+
if (this.outstandingPromises.length > 0) {
|
|
531
|
+
this.consume();
|
|
532
|
+
}
|
|
533
|
+
else {
|
|
534
|
+
this._onDrained.fire();
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
dispose() {
|
|
538
|
+
this._onDrained.dispose();
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* A queue is handles one promise at a time and guarantees that at any time only one promise is executing.
|
|
543
|
+
*/
|
|
544
|
+
export class Queue extends Limiter {
|
|
545
|
+
constructor() {
|
|
546
|
+
super(1);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
export class TimeoutTimer {
|
|
550
|
+
constructor(runner, timeout) {
|
|
551
|
+
this._token = -1;
|
|
552
|
+
if (typeof runner === 'function' && typeof timeout === 'number') {
|
|
553
|
+
this.setIfNotSet(runner, timeout);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
dispose() {
|
|
557
|
+
this.cancel();
|
|
558
|
+
}
|
|
559
|
+
cancel() {
|
|
560
|
+
if (this._token !== -1) {
|
|
561
|
+
clearTimeout(this._token);
|
|
562
|
+
this._token = -1;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
cancelAndSet(runner, timeout) {
|
|
566
|
+
this.cancel();
|
|
567
|
+
this._token = setTimeout(() => {
|
|
568
|
+
this._token = -1;
|
|
569
|
+
runner();
|
|
570
|
+
}, timeout);
|
|
571
|
+
}
|
|
572
|
+
setIfNotSet(runner, timeout) {
|
|
573
|
+
if (this._token !== -1) {
|
|
574
|
+
// timer is already set
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
this._token = setTimeout(() => {
|
|
578
|
+
this._token = -1;
|
|
579
|
+
runner();
|
|
580
|
+
}, timeout);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
export class IntervalTimer {
|
|
584
|
+
constructor() {
|
|
585
|
+
this._token = -1;
|
|
586
|
+
}
|
|
587
|
+
dispose() {
|
|
588
|
+
this.cancel();
|
|
589
|
+
}
|
|
590
|
+
cancel() {
|
|
591
|
+
if (this._token !== -1) {
|
|
592
|
+
clearInterval(this._token);
|
|
593
|
+
this._token = -1;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
cancelAndSet(runner, interval) {
|
|
597
|
+
this.cancel();
|
|
598
|
+
this._token = setInterval(() => {
|
|
599
|
+
runner();
|
|
600
|
+
}, interval);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
export class RunOnceScheduler {
|
|
604
|
+
constructor(runner, delay) {
|
|
605
|
+
this.timeoutToken = -1;
|
|
606
|
+
this.runner = runner;
|
|
607
|
+
this.timeout = delay;
|
|
608
|
+
this.timeoutHandler = this.onTimeout.bind(this);
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Dispose RunOnceScheduler
|
|
612
|
+
*/
|
|
613
|
+
dispose() {
|
|
614
|
+
this.cancel();
|
|
615
|
+
this.runner = undefined;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Cancel current scheduled runner (if any).
|
|
619
|
+
*/
|
|
620
|
+
cancel() {
|
|
621
|
+
if (this.isScheduled()) {
|
|
622
|
+
clearTimeout(this.timeoutToken);
|
|
623
|
+
this.timeoutToken = -1;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Cancel previous runner (if any) & schedule a new runner.
|
|
628
|
+
*/
|
|
629
|
+
schedule(delay = this.timeout) {
|
|
630
|
+
this.cancel();
|
|
631
|
+
this.timeoutToken = setTimeout(this.timeoutHandler, delay);
|
|
632
|
+
}
|
|
633
|
+
get delay() {
|
|
634
|
+
return this.timeout;
|
|
635
|
+
}
|
|
636
|
+
set delay(value) {
|
|
637
|
+
this.timeout = value;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Returns true if scheduled.
|
|
641
|
+
*/
|
|
642
|
+
isScheduled() {
|
|
643
|
+
return this.timeoutToken !== -1;
|
|
644
|
+
}
|
|
645
|
+
onTimeout() {
|
|
646
|
+
this.timeoutToken = -1;
|
|
647
|
+
if (this.runner) {
|
|
648
|
+
this.doRun();
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
doRun() {
|
|
652
|
+
if (this.runner) {
|
|
653
|
+
this.runner();
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Same as `RunOnceScheduler`, but doesn't count the time spent in sleep mode.
|
|
659
|
+
* > **NOTE**: Only offers 1s resolution.
|
|
660
|
+
*
|
|
661
|
+
* When calling `setTimeout` with 3hrs, and putting the computer immediately to sleep
|
|
662
|
+
* for 8hrs, `setTimeout` will fire **as soon as the computer wakes from sleep**. But
|
|
663
|
+
* this scheduler will execute 3hrs **after waking the computer from sleep**.
|
|
664
|
+
*/
|
|
665
|
+
export class ProcessTimeRunOnceScheduler {
|
|
666
|
+
constructor(runner, delay) {
|
|
667
|
+
if (delay % 1000 !== 0) {
|
|
668
|
+
console.warn(`ProcessTimeRunOnceScheduler resolution is 1s, ${delay}ms is not a multiple of 1000ms.`);
|
|
669
|
+
}
|
|
670
|
+
this.runner = runner;
|
|
671
|
+
this.timeout = delay;
|
|
672
|
+
this.counter = 0;
|
|
673
|
+
this.intervalToken = -1;
|
|
674
|
+
this.intervalHandler = this.onInterval.bind(this);
|
|
675
|
+
}
|
|
676
|
+
dispose() {
|
|
677
|
+
this.cancel();
|
|
678
|
+
this.runner = undefined;
|
|
679
|
+
}
|
|
680
|
+
cancel() {
|
|
681
|
+
if (this.isScheduled()) {
|
|
682
|
+
clearInterval(this.intervalToken);
|
|
683
|
+
this.intervalToken = -1;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Cancel previous runner (if any) & schedule a new runner.
|
|
688
|
+
*/
|
|
689
|
+
schedule(delay = this.timeout) {
|
|
690
|
+
if (delay % 1000 !== 0) {
|
|
691
|
+
console.warn(`ProcessTimeRunOnceScheduler resolution is 1s, ${delay}ms is not a multiple of 1000ms.`);
|
|
692
|
+
}
|
|
693
|
+
this.cancel();
|
|
694
|
+
this.counter = Math.ceil(delay / 1000);
|
|
695
|
+
this.intervalToken = setInterval(this.intervalHandler, 1000);
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Returns true if scheduled.
|
|
699
|
+
*/
|
|
700
|
+
isScheduled() {
|
|
701
|
+
return this.intervalToken !== -1;
|
|
702
|
+
}
|
|
703
|
+
onInterval() {
|
|
704
|
+
this.counter--;
|
|
705
|
+
if (this.counter > 0) {
|
|
706
|
+
// still need to wait
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
// time elapsed
|
|
710
|
+
clearInterval(this.intervalToken);
|
|
711
|
+
this.intervalToken = -1;
|
|
712
|
+
if (this.runner) {
|
|
713
|
+
this.runner();
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
export class RunOnceWorker extends RunOnceScheduler {
|
|
718
|
+
constructor(runner, timeout) {
|
|
719
|
+
super(runner, timeout);
|
|
720
|
+
this.units = [];
|
|
721
|
+
}
|
|
722
|
+
work(unit) {
|
|
723
|
+
this.units.push(unit);
|
|
724
|
+
if (!this.isScheduled()) {
|
|
725
|
+
this.schedule();
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
doRun() {
|
|
729
|
+
const units = this.units;
|
|
730
|
+
this.units = [];
|
|
731
|
+
if (this.runner) {
|
|
732
|
+
this.runner(units);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
dispose() {
|
|
736
|
+
this.units = [];
|
|
737
|
+
super.dispose();
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
export async function retry(task, delay, retries) {
|
|
741
|
+
let lastError;
|
|
742
|
+
for (let i = 0; i < retries; i++) {
|
|
743
|
+
try {
|
|
744
|
+
return await task();
|
|
745
|
+
}
|
|
746
|
+
catch (error) {
|
|
747
|
+
lastError = error;
|
|
748
|
+
await timeout(delay);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
throw lastError;
|
|
752
|
+
}
|
|
753
|
+
export class TaskSequentializer {
|
|
754
|
+
hasPending(taskId) {
|
|
755
|
+
if (!this._pending) {
|
|
756
|
+
return false;
|
|
757
|
+
}
|
|
758
|
+
if (typeof taskId === 'number') {
|
|
759
|
+
return this._pending.taskId === taskId;
|
|
760
|
+
}
|
|
761
|
+
return !!this._pending;
|
|
762
|
+
}
|
|
763
|
+
get pending() {
|
|
764
|
+
return this._pending ? this._pending.promise : undefined;
|
|
765
|
+
}
|
|
766
|
+
cancelPending() {
|
|
767
|
+
var _a;
|
|
768
|
+
(_a = this._pending) === null || _a === void 0 ? void 0 : _a.cancel();
|
|
769
|
+
}
|
|
770
|
+
setPending(taskId, promise, onCancel) {
|
|
771
|
+
this._pending = { taskId, cancel: () => onCancel === null || onCancel === void 0 ? void 0 : onCancel(), promise };
|
|
772
|
+
promise.then(() => this.donePending(taskId), () => this.donePending(taskId));
|
|
773
|
+
return promise;
|
|
774
|
+
}
|
|
775
|
+
donePending(taskId) {
|
|
776
|
+
if (this._pending && taskId === this._pending.taskId) {
|
|
777
|
+
// only set pending to done if the promise finished that is associated with that taskId
|
|
778
|
+
this._pending = undefined;
|
|
779
|
+
// schedule the next task now that we are free if we have any
|
|
780
|
+
this.triggerNext();
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
triggerNext() {
|
|
784
|
+
if (this._next) {
|
|
785
|
+
const next = this._next;
|
|
786
|
+
this._next = undefined;
|
|
787
|
+
// Run next task and complete on the associated promise
|
|
788
|
+
next.run().then(next.promiseResolve, next.promiseReject);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
setNext(run) {
|
|
792
|
+
// this is our first next task, so we create associated promise with it
|
|
793
|
+
// so that we can return a promise that completes when the task has
|
|
794
|
+
// completed.
|
|
795
|
+
if (!this._next) {
|
|
796
|
+
let promiseResolve;
|
|
797
|
+
let promiseReject;
|
|
798
|
+
const promise = new Promise((resolve, reject) => {
|
|
799
|
+
promiseResolve = resolve;
|
|
800
|
+
promiseReject = reject;
|
|
801
|
+
});
|
|
802
|
+
this._next = {
|
|
803
|
+
run,
|
|
804
|
+
promise,
|
|
805
|
+
promiseResolve: promiseResolve,
|
|
806
|
+
promiseReject: promiseReject
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
else {
|
|
810
|
+
this._next.run = run;
|
|
811
|
+
}
|
|
812
|
+
return this._next.promise;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
// #endregion
|
|
816
|
+
// #region
|
|
817
|
+
/**
|
|
818
|
+
* The `IntervalCounter` allows to count the number
|
|
819
|
+
* of calls to `increment()` over a duration of
|
|
820
|
+
* `interval`. This utility can be used to conditionally
|
|
821
|
+
* throttle a frequent task when a certain threshold
|
|
822
|
+
* is reached.
|
|
823
|
+
*/
|
|
824
|
+
export class IntervalCounter {
|
|
825
|
+
constructor(interval, nowFn = () => Date.now()) {
|
|
826
|
+
this.interval = interval;
|
|
827
|
+
this.nowFn = nowFn;
|
|
828
|
+
this.lastIncrementTime = 0;
|
|
829
|
+
this.value = 0;
|
|
830
|
+
}
|
|
831
|
+
increment() {
|
|
832
|
+
const now = this.nowFn();
|
|
833
|
+
// We are outside of the range of `interval` and as such
|
|
834
|
+
// start counting from 0 and remember the time
|
|
835
|
+
if (now - this.lastIncrementTime > this.interval) {
|
|
836
|
+
this.lastIncrementTime = now;
|
|
837
|
+
this.value = 0;
|
|
838
|
+
}
|
|
839
|
+
this.value++;
|
|
840
|
+
return this.value;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Creates a promise whose resolution or rejection can be controlled imperatively.
|
|
845
|
+
*/
|
|
846
|
+
export class DeferredPromise {
|
|
847
|
+
get isRejected() {
|
|
848
|
+
return this.rejected;
|
|
849
|
+
}
|
|
850
|
+
get isResolved() {
|
|
851
|
+
return this.resolved;
|
|
852
|
+
}
|
|
853
|
+
get isSettled() {
|
|
854
|
+
return this.rejected || this.resolved;
|
|
855
|
+
}
|
|
856
|
+
constructor() {
|
|
857
|
+
this.rejected = false;
|
|
858
|
+
this.resolved = false;
|
|
859
|
+
this.p = new Promise((c, e) => {
|
|
860
|
+
this.completeCallback = c;
|
|
861
|
+
this.errorCallback = e;
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
complete(value) {
|
|
865
|
+
return new Promise(resolve => {
|
|
866
|
+
this.completeCallback(value);
|
|
867
|
+
this.resolved = true;
|
|
868
|
+
resolve();
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
error(err) {
|
|
872
|
+
return new Promise(resolve => {
|
|
873
|
+
this.errorCallback(err);
|
|
874
|
+
this.rejected = true;
|
|
875
|
+
resolve();
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
cancel() {
|
|
879
|
+
new Promise(resolve => {
|
|
880
|
+
this.errorCallback(new CancellationError());
|
|
881
|
+
this.rejected = true;
|
|
882
|
+
resolve();
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
// #endregion
|
|
887
|
+
// #region Promises
|
|
888
|
+
export var Promises;
|
|
889
|
+
(function (Promises) {
|
|
890
|
+
/**
|
|
891
|
+
* A drop-in replacement for `Promise.all` with the only difference
|
|
892
|
+
* that the method awaits every promise to either fulfill or reject.
|
|
893
|
+
*
|
|
894
|
+
* Similar to `Promise.all`, only the first error will be returned
|
|
895
|
+
* if any.
|
|
896
|
+
*/
|
|
897
|
+
async function settled(promises) {
|
|
898
|
+
let firstError = undefined;
|
|
899
|
+
const result = await Promise.all(promises.map(promise => promise.then(value => value, error => {
|
|
900
|
+
if (!firstError) {
|
|
901
|
+
firstError = error;
|
|
902
|
+
}
|
|
903
|
+
return undefined; // do not rethrow so that other promises can settle
|
|
904
|
+
})));
|
|
905
|
+
if (typeof firstError !== 'undefined') {
|
|
906
|
+
throw firstError;
|
|
907
|
+
}
|
|
908
|
+
return result; // cast is needed and protected by the `throw` above
|
|
909
|
+
}
|
|
910
|
+
Promises.settled = settled;
|
|
911
|
+
/**
|
|
912
|
+
* A helper to create a new `Promise<T>` with a body that is a promise
|
|
913
|
+
* itself. By default, an error that raises from the async body will
|
|
914
|
+
* end up as a unhandled rejection, so this utility properly awaits the
|
|
915
|
+
* body and rejects the promise as a normal promise does without async
|
|
916
|
+
* body.
|
|
917
|
+
*
|
|
918
|
+
* This method should only be used in rare cases where otherwise `async`
|
|
919
|
+
* cannot be used (e.g. when callbacks are involved that require this).
|
|
920
|
+
*/
|
|
921
|
+
function withAsyncBody(bodyFn) {
|
|
922
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
923
|
+
return new Promise(async (resolve, reject) => {
|
|
924
|
+
try {
|
|
925
|
+
await bodyFn(resolve, reject);
|
|
926
|
+
}
|
|
927
|
+
catch (error) {
|
|
928
|
+
reject(error);
|
|
929
|
+
}
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
Promises.withAsyncBody = withAsyncBody;
|
|
933
|
+
})(Promises || (Promises = {}));
|
|
934
|
+
// #endregion
|
|
935
|
+
//# sourceMappingURL=async.js.map
|