@daiso-tech/core 0.31.0 → 0.32.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/dist/async/_module-exports.d.ts +1 -0
- package/dist/async/_module-exports.js +1 -0
- package/dist/async/_module-exports.js.map +1 -1
- package/dist/async/async.events.d.ts +82 -0
- package/dist/async/async.events.js +59 -0
- package/dist/async/async.events.js.map +1 -0
- package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.d.ts +15 -0
- package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.js +50 -0
- package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.js.map +1 -0
- package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.d.ts +23 -0
- package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.js +44 -0
- package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.js.map +1 -0
- package/dist/async/utilities/lazy-promise/lazy-promise.d.ts +101 -51
- package/dist/async/utilities/lazy-promise/lazy-promise.js +155 -96
- package/dist/async/utilities/lazy-promise/lazy-promise.js.map +1 -1
- package/dist/async/utilities/retry/retry-or-fail.d.ts +1 -1
- package/dist/async/utilities/retry/retry-or-fail.js +1 -1
- package/dist/async/utilities/retry/retry-or-fail.js.map +1 -1
- package/dist/async/utilities/retry/retry.d.ts +1 -1
- package/dist/async/utilities/retry/retry.js.map +1 -1
- package/dist/cache/contracts/cache.contract.d.ts +2 -2
- package/dist/cache/implementations/derivables/cache/cache.d.ts +20 -159
- package/dist/cache/implementations/derivables/cache/cache.js +14 -174
- package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.d.ts +11 -32
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js +10 -52
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js.map +1 -1
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.d.ts +5 -5
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.js +5 -5
- package/dist/collection/implementations/_shared.d.ts +2 -0
- package/dist/collection/implementations/_shared.js +2 -0
- package/dist/collection/implementations/_shared.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.d.ts +5 -8
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js +5 -19
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.d.ts +5 -5
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js +5 -5
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +18 -13
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +14 -33
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.d.ts +9 -35
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js +8 -54
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js.map +1 -1
- package/dist/lock/contracts/lock.contract.d.ts +4 -4
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.d.ts +1 -2
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +1 -2
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +47 -24
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +40 -40
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +0 -204
- package/dist/lock/implementations/derivables/lock-provider/lock.js +0 -204
- package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +11 -37
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js +10 -59
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.d.ts +1 -2
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js +1 -2
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.d.ts +0 -1
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js +0 -1
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.d.ts +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +1 -1
- package/dist/serde/contracts/flexible-serde.contract.d.ts +1 -1
- package/dist/serde/implementations/derivables/serde.d.ts +7 -7
- package/dist/serde/implementations/derivables/serde.js +7 -7
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.d.ts +0 -1
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.js +0 -1
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.js.map +1 -1
- package/dist/utilities/classes/_module.d.ts +1 -0
- package/dist/utilities/classes/_module.js +1 -0
- package/dist/utilities/classes/_module.js.map +1 -1
- package/dist/utilities/classes/sync-event-bus/_module.d.ts +1 -0
- package/dist/utilities/classes/sync-event-bus/_module.js +2 -0
- package/dist/utilities/classes/sync-event-bus/_module.js.map +1 -0
- package/dist/utilities/classes/sync-event-bus/sync-event-bus.d.ts +16 -0
- package/dist/utilities/classes/sync-event-bus/sync-event-bus.js +53 -0
- package/dist/utilities/classes/sync-event-bus/sync-event-bus.js.map +1 -0
- package/dist/utilities/contracts/_module.d.ts +1 -0
- package/dist/utilities/contracts/_module.js +1 -0
- package/dist/utilities/contracts/_module.js.map +1 -1
- package/dist/utilities/contracts/sync-event-bus-listenable.d.ts +13 -0
- package/dist/utilities/contracts/sync-event-bus-listenable.js +5 -0
- package/dist/utilities/contracts/sync-event-bus-listenable.js.map +1 -0
- package/dist/utilities/functions/invokable.d.ts +4 -0
- package/dist/utilities/functions/invokable.js +9 -0
- package/dist/utilities/functions/invokable.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_module-exports.js","sourceRoot":"","sources":["../../src/async/_module-exports.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC;AACnD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"_module-exports.js","sourceRoot":"","sources":["../../src/async/_module-exports.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC;AACnD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Async
|
|
3
|
+
*/
|
|
4
|
+
import type { AbortAsyncError, RetryAsyncError, TimeoutAsyncError } from "../async/async.errors.js";
|
|
5
|
+
import { BaseEvent } from "../event-bus/contracts/_shared.js";
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
9
|
+
* @group Events
|
|
10
|
+
*/
|
|
11
|
+
export declare class FailureAsyncEvent extends BaseEvent<{
|
|
12
|
+
error: unknown;
|
|
13
|
+
}> {
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
18
|
+
* @group Events
|
|
19
|
+
*/
|
|
20
|
+
export declare class SuccessAsyncEvent<TValue> extends BaseEvent<{
|
|
21
|
+
value: TValue;
|
|
22
|
+
}> {
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
27
|
+
* @group Events
|
|
28
|
+
*/
|
|
29
|
+
export declare class FinallyAsyncEvent extends BaseEvent<{}> {
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
34
|
+
* @group Events
|
|
35
|
+
*/
|
|
36
|
+
export declare class RetryAttemptAsyncEvent extends BaseEvent<{
|
|
37
|
+
attempt: number;
|
|
38
|
+
error: unknown;
|
|
39
|
+
}> {
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
44
|
+
* @group Events
|
|
45
|
+
*/
|
|
46
|
+
export declare class RetryTimeoutAsyncEvent extends BaseEvent<{
|
|
47
|
+
error: TimeoutAsyncError;
|
|
48
|
+
}> {
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
53
|
+
* @group Events
|
|
54
|
+
*/
|
|
55
|
+
export declare class RetryFailureAsyncEvent extends BaseEvent<{
|
|
56
|
+
error: RetryAsyncError;
|
|
57
|
+
}> {
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
*
|
|
61
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
62
|
+
* @group Events
|
|
63
|
+
*/
|
|
64
|
+
export declare class TotalTimeoutFailureAsyncEvent extends BaseEvent<{
|
|
65
|
+
error: TimeoutAsyncError;
|
|
66
|
+
}> {
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
*
|
|
70
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
71
|
+
* @group Events
|
|
72
|
+
*/
|
|
73
|
+
export declare class AbortAsyncEvent extends BaseEvent<{
|
|
74
|
+
error: AbortAsyncError;
|
|
75
|
+
}> {
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
80
|
+
* @group Events
|
|
81
|
+
*/
|
|
82
|
+
export type AsyncEvents<TValue> = FailureAsyncEvent | SuccessAsyncEvent<TValue> | FinallyAsyncEvent | RetryAttemptAsyncEvent | RetryTimeoutAsyncEvent | RetryFailureAsyncEvent | TotalTimeoutFailureAsyncEvent | AbortAsyncEvent;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { BaseEvent } from "../event-bus/contracts/_shared.js";
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
5
|
+
* @group Events
|
|
6
|
+
*/
|
|
7
|
+
export class FailureAsyncEvent extends BaseEvent {
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
12
|
+
* @group Events
|
|
13
|
+
*/
|
|
14
|
+
export class SuccessAsyncEvent extends BaseEvent {
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
19
|
+
* @group Events
|
|
20
|
+
*/
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
22
|
+
export class FinallyAsyncEvent extends BaseEvent {
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
27
|
+
* @group Events
|
|
28
|
+
*/
|
|
29
|
+
export class RetryAttemptAsyncEvent extends BaseEvent {
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
34
|
+
* @group Events
|
|
35
|
+
*/
|
|
36
|
+
export class RetryTimeoutAsyncEvent extends BaseEvent {
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
*
|
|
40
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
41
|
+
* @group Events
|
|
42
|
+
*/
|
|
43
|
+
export class RetryFailureAsyncEvent extends BaseEvent {
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
48
|
+
* @group Events
|
|
49
|
+
*/
|
|
50
|
+
export class TotalTimeoutFailureAsyncEvent extends BaseEvent {
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
*
|
|
54
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
55
|
+
* @group Events
|
|
56
|
+
*/
|
|
57
|
+
export class AbortAsyncEvent extends BaseEvent {
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=async.events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async.events.js","sourceRoot":"","sources":["../../src/async/async.events.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAE7D;;;;GAIG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAErC;CAAG;AAEL;;;;GAIG;AACH,MAAM,OAAO,iBAA0B,SAAQ,SAE7C;CAAG;AAEL;;;;GAIG;AACH,mEAAmE;AACnE,MAAM,OAAO,iBAAkB,SAAQ,SAAa;CAAG;AAEvD;;;;GAIG;AACH,MAAM,OAAO,sBAAuB,SAAQ,SAG1C;CAAG;AAEL;;;;GAIG;AACH,MAAM,OAAO,sBAAuB,SAAQ,SAE1C;CAAG;AAEL;;;;GAIG;AACH,MAAM,OAAO,sBAAuB,SAAQ,SAE1C;CAAG;AAEL;;;;GAIG;AACH,MAAM,OAAO,6BAA8B,SAAQ,SAEjD;CAAG;AAEL;;;;GAIG;AACH,MAAM,OAAO,eAAgB,SAAQ,SAEnC;CAAG"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Async
|
|
3
|
+
*/
|
|
4
|
+
import { type Invokable } from "../../../utilities/_module-exports.js";
|
|
5
|
+
import type { ILazyPromiseListener } from "../../../async/utilities/lazy-promise/lazy-promise-listener.contract.js";
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare class LazyPromiseEventBus<TEventMap extends Record<string, unknown>> implements ILazyPromiseListener<TEventMap> {
|
|
10
|
+
private readonly eventMap;
|
|
11
|
+
dispatch<TEventName extends keyof TEventMap>(eventName: Extract<TEventName, string>, event: TEventMap[TEventName]): void;
|
|
12
|
+
addListener<TEventName extends keyof TEventMap>(eventName: Extract<TEventName, string>, listener: Invokable<[event: TEventMap[TEventName]]>): void;
|
|
13
|
+
removeListener<TEventName extends keyof TEventMap>(eventName: Extract<TEventName, string>, listener: Invokable<[event: TEventMap[TEventName]]>): void;
|
|
14
|
+
clear(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Async
|
|
3
|
+
*/
|
|
4
|
+
import { callInvokable } from "../../../utilities/_module-exports.js";
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export class LazyPromiseEventBus {
|
|
9
|
+
eventMap = new Map();
|
|
10
|
+
dispatch(eventName, event) {
|
|
11
|
+
if (this.eventMap.size === 0) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const eventListeners = this.eventMap.get(eventName);
|
|
15
|
+
if (eventListeners === undefined) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (eventListeners.size === 0) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
for (const eventListener of eventListeners) {
|
|
22
|
+
callInvokable(eventListener, event);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
addListener(eventName, listener) {
|
|
26
|
+
let eventListeners = this.eventMap.get(eventName);
|
|
27
|
+
if (eventListeners === undefined) {
|
|
28
|
+
eventListeners = new Set();
|
|
29
|
+
this.eventMap.set(eventName, eventListeners);
|
|
30
|
+
}
|
|
31
|
+
eventListeners.add(listener);
|
|
32
|
+
}
|
|
33
|
+
removeListener(eventName, listener) {
|
|
34
|
+
const eventListeners = this.eventMap.get(eventName);
|
|
35
|
+
if (eventListeners === undefined) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
eventListeners.delete(listener);
|
|
39
|
+
if (eventListeners.size === 0) {
|
|
40
|
+
this.eventMap.delete(eventName);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
clear() {
|
|
44
|
+
for (const eventListeners of this.eventMap.values()) {
|
|
45
|
+
eventListeners.clear();
|
|
46
|
+
}
|
|
47
|
+
this.eventMap.clear();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=lazy-promise-event-bus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy-promise-event-bus.js","sourceRoot":"","sources":["../../../../src/async/utilities/lazy-promise/lazy-promise-event-bus.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,aAAa,EAAkB,MAAM,gCAAgC,CAAC;AAG/E;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAGX,QAAQ,GAAG,IAAI,GAAG,EAAwC,CAAC;IAE5E,QAAQ,CACJ,SAAsC,EACtC,KAA4B;QAE5B,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;YAC3B,OAAO;QACX,CAAC;QACD,MAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACpD,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO;QACX,CAAC;QACD,IAAI,cAAc,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;YAC5B,OAAO;QACX,CAAC;QACD,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE,CAAC;YACzC,aAAa,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;QACxC,CAAC;IACL,CAAC;IAED,WAAW,CACP,SAAsC,EACtC,QAAmD;QAEnD,IAAI,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YAC/B,cAAc,GAAG,IAAI,GAAG,EAAE,CAAC;YAC3B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;QACjD,CAAC;QACD,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACjC,CAAC;IAED,cAAc,CACV,SAAsC,EACtC,QAAmD;QAEnD,MAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACpD,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO;QACX,CAAC;QACD,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAChC,IAAI,cAAc,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;YAC5B,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACpC,CAAC;IACL,CAAC;IAED,KAAK;QACD,KAAK,MAAM,cAAc,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;YAClD,cAAc,CAAC,KAAK,EAAE,CAAC;QAC3B,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;CACJ"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Async
|
|
3
|
+
*/
|
|
4
|
+
import { type Invokable } from "../../../utilities/_module-exports.js";
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
8
|
+
* @group Utilities
|
|
9
|
+
*/
|
|
10
|
+
export type ILazyPromiseListener<TEventMap extends Record<string, unknown>> = {
|
|
11
|
+
addListener<TEventName extends keyof TEventMap>(eventName: Extract<TEventName, string>, listener: Invokable<[event: TEventMap[TEventName]]>): void;
|
|
12
|
+
removeListener<TEventName extends keyof TEventMap>(eventName: Extract<TEventName, string>, listener: Invokable<[event: TEventMap[TEventName]]>): void;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare class LazyPromiseEventBus<TEventMap extends Record<string, unknown>> implements ILazyPromiseListener<TEventMap> {
|
|
18
|
+
private readonly eventMap;
|
|
19
|
+
dispatch<TEventName extends keyof TEventMap>(eventName: Extract<TEventName, string>, event: TEventMap[TEventName]): void;
|
|
20
|
+
addListener<TEventName extends keyof TEventMap>(eventName: Extract<TEventName, string>, listener: Invokable<[event: TEventMap[TEventName]]>): void;
|
|
21
|
+
removeListener<TEventName extends keyof TEventMap>(eventName: Extract<TEventName, string>, listener: Invokable<[event: TEventMap[TEventName]]>): void;
|
|
22
|
+
clear(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Async
|
|
3
|
+
*/
|
|
4
|
+
import { callInvokable } from "../../../utilities/_module-exports.js";
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export class LazyPromiseEventBus {
|
|
9
|
+
eventMap = new Map();
|
|
10
|
+
dispatch(eventName, event) {
|
|
11
|
+
const eventListeners = this.eventMap.get(eventName);
|
|
12
|
+
if (eventListeners === undefined) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
for (const eventListener of eventListeners) {
|
|
16
|
+
callInvokable(eventListener, event);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
addListener(eventName, listener) {
|
|
20
|
+
let eventListeners = this.eventMap.get(eventName);
|
|
21
|
+
if (eventListeners === undefined) {
|
|
22
|
+
eventListeners = new Set();
|
|
23
|
+
this.eventMap.set(eventName, eventListeners);
|
|
24
|
+
}
|
|
25
|
+
eventListeners.add(listener);
|
|
26
|
+
}
|
|
27
|
+
removeListener(eventName, listener) {
|
|
28
|
+
const eventListeners = this.eventMap.get(eventName);
|
|
29
|
+
if (eventListeners === undefined) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
eventListeners.delete(listener);
|
|
33
|
+
if (eventListeners.size === 0) {
|
|
34
|
+
this.eventMap.delete(eventName);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
clear() {
|
|
38
|
+
for (const eventListeners of this.eventMap.values()) {
|
|
39
|
+
eventListeners.clear();
|
|
40
|
+
}
|
|
41
|
+
this.eventMap.clear();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=lazy-promise-listener.contract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy-promise-listener.contract.js","sourceRoot":"","sources":["../../../../src/async/utilities/lazy-promise/lazy-promise-listener.contract.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,aAAa,EAAkB,MAAM,gCAAgC,CAAC;AAmB/E;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAGX,QAAQ,GAAG,IAAI,GAAG,EAAwC,CAAC;IAE5E,QAAQ,CACJ,SAAsC,EACtC,KAA4B;QAE5B,MAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACpD,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO;QACX,CAAC;QACD,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE,CAAC;YACzC,aAAa,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;QACxC,CAAC;IACL,CAAC;IAED,WAAW,CACP,SAAsC,EACtC,QAAmD;QAEnD,IAAI,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YAC/B,cAAc,GAAG,IAAI,GAAG,EAAE,CAAC;YAC3B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;QACjD,CAAC;QACD,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACjC,CAAC;IAED,cAAc,CACV,SAAsC,EACtC,QAAmD;QAEnD,MAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACpD,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO;QACX,CAAC;QACD,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAChC,IAAI,cAAc,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;YAC5B,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACpC,CAAC;IACL,CAAC;IAED,KAAK;QACD,KAAK,MAAM,cAAc,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;YAClD,cAAc,CAAC,KAAK,EAAE,CAAC;QAC3B,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;CACJ"}
|
|
@@ -2,70 +2,95 @@
|
|
|
2
2
|
* @module Async
|
|
3
3
|
*/
|
|
4
4
|
import type { BackoffPolicy } from "../../../async/backof-policies/_module.js";
|
|
5
|
-
import type { AsyncLazy, Invokable, InvokableFn, Promisable, TimeSpan } from "../../../utilities/_module-exports.js";
|
|
5
|
+
import type { AsyncLazy, Invokable, InvokableFn, ISyncEventListenable, Promisable, TimeSpan } from "../../../utilities/_module-exports.js";
|
|
6
6
|
import type { RetryPolicy } from "../../../async/utilities/retry/_module.js";
|
|
7
|
+
import { TimeoutAsyncError } from "../../../async/async.errors.js";
|
|
8
|
+
import { RetryAsyncError } from "../../../async/async.errors.js";
|
|
9
|
+
import { AbortAsyncError } from "../../../async/async.errors.js";
|
|
10
|
+
import { type AsyncEvents } from "../../../async/async.events.js";
|
|
11
|
+
import type { EventClass, EventListener, EventInstance } from "../../../event-bus/contracts/event-bus.contract.js";
|
|
7
12
|
/**
|
|
8
13
|
*
|
|
9
14
|
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
10
15
|
* @group Utilities
|
|
11
16
|
*/
|
|
12
|
-
export type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
export type AsyncEventMap<TValue = unknown> = {
|
|
18
|
+
failure: {
|
|
19
|
+
error: unknown;
|
|
20
|
+
};
|
|
21
|
+
success: {
|
|
22
|
+
value: TValue;
|
|
23
|
+
};
|
|
24
|
+
finally: undefined;
|
|
25
|
+
retryAttempt: {
|
|
26
|
+
attempt: number;
|
|
27
|
+
error: unknown;
|
|
28
|
+
};
|
|
29
|
+
retryTimeoutFailure: {
|
|
30
|
+
error: TimeoutAsyncError;
|
|
31
|
+
};
|
|
32
|
+
retryFailure: {
|
|
33
|
+
error: RetryAsyncError;
|
|
34
|
+
};
|
|
35
|
+
totalTimeoutFailure: {
|
|
36
|
+
error: TimeoutAsyncError;
|
|
37
|
+
};
|
|
38
|
+
abortFailure: {
|
|
39
|
+
error: AbortAsyncError;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
19
42
|
/**
|
|
20
43
|
*
|
|
21
44
|
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
22
45
|
* @group Utilities
|
|
23
46
|
*/
|
|
24
|
-
export type
|
|
47
|
+
export type LazyPromiseSettings = {
|
|
48
|
+
backoffPolicy?: BackoffPolicy | null;
|
|
49
|
+
retryAttempts?: number | null;
|
|
50
|
+
retryPolicy?: RetryPolicy | null;
|
|
51
|
+
retryTimeout?: TimeSpan | null;
|
|
52
|
+
totalTimeout?: TimeSpan | null;
|
|
53
|
+
abortSignal?: AbortSignal | null;
|
|
54
|
+
};
|
|
25
55
|
/**
|
|
26
56
|
*
|
|
27
57
|
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
28
58
|
* @group Utilities
|
|
29
59
|
*/
|
|
30
|
-
export type
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
onError?: LazyPromiseOnError;
|
|
34
|
-
};
|
|
60
|
+
export type LazyPromiseResolve<TValue> = InvokableFn<[
|
|
61
|
+
value: Promisable<TValue>
|
|
62
|
+
], void>;
|
|
35
63
|
/**
|
|
36
64
|
*
|
|
37
65
|
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
38
66
|
* @group Utilities
|
|
39
67
|
*/
|
|
40
|
-
export type
|
|
41
|
-
backoffPolicy?: BackoffPolicy | null;
|
|
42
|
-
retryAttempts?: number | null;
|
|
43
|
-
retryPolicy?: RetryPolicy | null;
|
|
44
|
-
retryTimeout?: TimeSpan | null;
|
|
45
|
-
totalTimeout?: TimeSpan | null;
|
|
46
|
-
};
|
|
68
|
+
export type LazyPromiseReject = InvokableFn<[error: unknown], void>;
|
|
47
69
|
/**
|
|
48
70
|
*
|
|
49
71
|
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
50
72
|
* @group Utilities
|
|
51
73
|
*/
|
|
52
|
-
export type
|
|
53
|
-
|
|
54
|
-
|
|
74
|
+
export type LazyPromiseCallback<TValue> = InvokableFn<[
|
|
75
|
+
resolve: LazyPromiseResolve<TValue>,
|
|
76
|
+
reject: LazyPromiseReject
|
|
77
|
+
], Promisable<void>>;
|
|
55
78
|
/**
|
|
56
79
|
* The <i>LazyPromise</i> class is used for creating lazy <i>{@link PromiseLike}<i> object that will only execute when awaited or when then method is called.
|
|
57
80
|
* The class includes helpful methods
|
|
58
81
|
* - <i>defer</i>
|
|
59
|
-
* - <i>
|
|
60
|
-
* - <i>
|
|
61
|
-
* - <i>
|
|
62
|
-
* - <i>
|
|
63
|
-
* - <i>
|
|
82
|
+
* - <i>setRetryAttempts</i>
|
|
83
|
+
* - <i>setRetryTimeout</i>
|
|
84
|
+
* - <i>setRetryPolicy</i>
|
|
85
|
+
* - <i>setBackoffPolicy</i>
|
|
86
|
+
* - <i>setTotalTimeout</i>
|
|
87
|
+
* - <i>setAbortSignal</i>
|
|
64
88
|
*
|
|
65
89
|
* The order in which these methods are called does not affect their methodality. Internally, the following execution order is applied:
|
|
66
|
-
* 1. <i>
|
|
67
|
-
* 2. <i>
|
|
68
|
-
* 3. <i>
|
|
90
|
+
* 1. <i>setRetryTimeout</i>
|
|
91
|
+
* 2. <i>setRetryAttempts</i>
|
|
92
|
+
* 3. <i>setTotalTimeout</i>
|
|
93
|
+
* 4. <i>setAbortSignal</i>
|
|
69
94
|
*
|
|
70
95
|
* This means that combining all methods ensures the <i>retryAttempts</i> method will continue retrying even if the timeout is triggered,
|
|
71
96
|
* while the <i>abort</i> method takes priority to cancel all operations if activated.
|
|
@@ -73,9 +98,9 @@ export type LazyPromiseSettings<TValue = unknown> = LazyPromiseCallbacks<TValue>
|
|
|
73
98
|
* IMPORT_PATH: ```"@daiso-tech/core/async"```
|
|
74
99
|
* @group Utilities
|
|
75
100
|
*/
|
|
76
|
-
export declare class LazyPromise<TValue> implements PromiseLike<TValue
|
|
101
|
+
export declare class LazyPromise<TValue> implements PromiseLike<TValue>, ISyncEventListenable<AsyncEvents<TValue>> {
|
|
77
102
|
/**
|
|
78
|
-
* The <i>wrapFn</i> is convience method used for wrapping
|
|
103
|
+
* The <i>wrapFn</i> is convience method used for wrapping async <i>{@link Invokable}</i> with a <i>LazyPromise</i>.
|
|
79
104
|
* @example
|
|
80
105
|
* ```ts
|
|
81
106
|
* import { LazyPromise } from "@daiso-tech/core/async";
|
|
@@ -86,10 +111,10 @@ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
|
|
|
86
111
|
*
|
|
87
112
|
* const file = await readFile("none_existing_file.txt")
|
|
88
113
|
* .setRetryAttempts(4)
|
|
89
|
-
* .
|
|
114
|
+
* .setRetryTimeout(TimeSpan.fromMinutes(1));
|
|
90
115
|
* ```
|
|
91
116
|
*/
|
|
92
|
-
static wrapFn<TArgs extends unknown[], TReturn>(fn: Invokable<TArgs, Promisable<TReturn>>, settings?: LazyPromiseSettings
|
|
117
|
+
static wrapFn<TArgs extends unknown[], TReturn>(fn: Invokable<TArgs, Promisable<TReturn>>, settings?: LazyPromiseSettings): InvokableFn<TArgs, LazyPromise<TReturn>>;
|
|
93
118
|
/**
|
|
94
119
|
* The <i>delay</i> method creates a <i>{@link LazyPromise}</i> that will be fulfilled after given <i>time</i>.
|
|
95
120
|
*
|
|
@@ -103,27 +128,50 @@ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
|
|
|
103
128
|
* console.log("a");
|
|
104
129
|
* await LazyPromise.delay(TimeSpan.fromSeconds(2));
|
|
105
130
|
* console.log("b");
|
|
131
|
+
* ```
|
|
106
132
|
*/
|
|
107
133
|
static delay(time: TimeSpan): LazyPromise<void>;
|
|
108
134
|
/**
|
|
109
135
|
* The <i>all<i> method works similarly to <i>{@link Promise.all}</i> with the key distinction that it operates lazily.
|
|
110
136
|
*/
|
|
111
|
-
static all<TValue>(promises: LazyPromise<TValue>[]): LazyPromise<TValue[]>;
|
|
137
|
+
static all<TValue>(promises: LazyPromise<TValue>[], settings?: LazyPromiseSettings): LazyPromise<TValue[]>;
|
|
112
138
|
/**
|
|
113
139
|
* The <i>allSettled<i> method works similarly to <i>{@link Promise.allSettled}</i> with the key distinction that it operates lazily.
|
|
114
140
|
*/
|
|
115
|
-
static allSettled<TValue>(promises: LazyPromise<TValue>[]): LazyPromise<PromiseSettledResult<TValue>[]>;
|
|
141
|
+
static allSettled<TValue>(promises: LazyPromise<TValue>[], settings?: LazyPromiseSettings): LazyPromise<PromiseSettledResult<TValue>[]>;
|
|
116
142
|
/**
|
|
117
143
|
* The <i>race<i> method works similarly to <i>{@link Promise.race}</i> with the key distinction that it operates lazily.
|
|
118
144
|
*/
|
|
119
|
-
static race<TValue>(promises: LazyPromise<TValue>[]): LazyPromise<TValue>;
|
|
145
|
+
static race<TValue>(promises: LazyPromise<TValue>[], settings?: LazyPromiseSettings): LazyPromise<TValue>;
|
|
120
146
|
/**
|
|
121
147
|
* The <i>any<i> method works similarly to <i>{@link Promise.any}</i> with the key distinction that it operates lazily.
|
|
122
148
|
*/
|
|
123
|
-
static any<TValue>(promises: LazyPromise<TValue>[]): LazyPromise<TValue>;
|
|
149
|
+
static any<TValue>(promises: LazyPromise<TValue>[], settings?: LazyPromiseSettings): LazyPromise<TValue>;
|
|
150
|
+
/**
|
|
151
|
+
* The <i>fromCallback</i> is convience method used for wrapping Node js callback functions with a <i>LazyPromise</i>.
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* import { LazyPromise } from "@daiso-tech/core/async";
|
|
155
|
+
* import { readFile } from "node:fs";
|
|
156
|
+
*
|
|
157
|
+
* const lazyPromise = LazyPromise.fromCallback<Buffer>((resolve, reject) => {
|
|
158
|
+
* readFile("FILE_PATH", (err, data) => {
|
|
159
|
+
* if (err !== null) {
|
|
160
|
+
* reject(err);
|
|
161
|
+
* return;
|
|
162
|
+
* }
|
|
163
|
+
* resolve(data);
|
|
164
|
+
* });
|
|
165
|
+
* });
|
|
166
|
+
* const file = await lazyPromise;
|
|
167
|
+
* console.log(file);
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
static fromCallback<TValue>(callback: LazyPromiseCallback<TValue>, settings?: LazyPromiseSettings): LazyPromise<TValue>;
|
|
124
171
|
private promise;
|
|
125
172
|
private asyncFn;
|
|
126
173
|
private readonly settings;
|
|
174
|
+
private readonly eventBus;
|
|
127
175
|
/**
|
|
128
176
|
* @example
|
|
129
177
|
* ```ts
|
|
@@ -135,19 +183,18 @@ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
|
|
|
135
183
|
*
|
|
136
184
|
* // "I am lazy" will only logged when awaited or then method i called.
|
|
137
185
|
* await promise;
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* You can pass sync or async <i>{@link Invokable}</i>.
|
|
138
189
|
*/
|
|
139
|
-
constructor(
|
|
190
|
+
constructor(invokable: AsyncLazy<TValue>, settings?: LazyPromiseSettings);
|
|
191
|
+
addListener<TEventClass extends EventClass<AsyncEvents<TValue>>>(event: TEventClass, listener: EventListener<EventInstance<TEventClass>>): void;
|
|
192
|
+
removeListener<TEventClass extends EventClass<AsyncEvents<TValue>>>(event: TEventClass, listener: EventListener<EventInstance<TEventClass>>): void;
|
|
140
193
|
private applyRetryTimeout;
|
|
141
194
|
private applyRetry;
|
|
142
195
|
private applyTotalTimeout;
|
|
143
196
|
private applyAbort;
|
|
144
197
|
private applySettings;
|
|
145
|
-
/**
|
|
146
|
-
* @throws {AbortAsyncError} {@link AbortAsyncError}
|
|
147
|
-
* @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
|
|
148
|
-
* @throws {RetryAsyncError} {@link RetryAsyncError}
|
|
149
|
-
*/
|
|
150
|
-
then<TResult1 = TValue, TResult2 = never>(onfulfilled?: ((value: TValue) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
|
|
151
198
|
/**
|
|
152
199
|
* The <i>setBackoffPolicy</i> method is used for setting a custom <i>{@link BackoffPolicy}</i>.
|
|
153
200
|
* ```ts
|
|
@@ -266,14 +313,17 @@ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
|
|
|
266
313
|
* ```
|
|
267
314
|
*/
|
|
268
315
|
setAbortSignal(abortSignal: AbortSignal | null): LazyPromise<TValue>;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
316
|
+
/**
|
|
317
|
+
* @throws {AbortAsyncError} {@link AbortAsyncError}
|
|
318
|
+
* @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
|
|
319
|
+
* @throws {RetryAsyncError} {@link RetryAsyncError}
|
|
320
|
+
*/
|
|
321
|
+
then<TResult1 = TValue, TResult2 = never>(onfulfilled?: ((value: TValue) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
|
|
272
322
|
/**
|
|
273
323
|
* The <i>defer</i> method executes the <i>LazyPromise</i> without awaiting it.
|
|
274
324
|
* @example
|
|
275
325
|
* ```ts
|
|
276
|
-
* import { LazyPromise } from "@daiso-tech/core";
|
|
326
|
+
* import { LazyPromise } from "@daiso-tech/core/async";
|
|
277
327
|
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
278
328
|
*
|
|
279
329
|
* const promise =
|