@daiso-tech/core 0.31.1 → 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.
Files changed (60) hide show
  1. package/dist/async/_module-exports.d.ts +1 -0
  2. package/dist/async/_module-exports.js +1 -0
  3. package/dist/async/_module-exports.js.map +1 -1
  4. package/dist/async/async.events.d.ts +82 -0
  5. package/dist/async/async.events.js +59 -0
  6. package/dist/async/async.events.js.map +1 -0
  7. package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.d.ts +15 -0
  8. package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.js +50 -0
  9. package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.js.map +1 -0
  10. package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.d.ts +23 -0
  11. package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.js +44 -0
  12. package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.js.map +1 -0
  13. package/dist/async/utilities/lazy-promise/lazy-promise.d.ts +87 -41
  14. package/dist/async/utilities/lazy-promise/lazy-promise.js +141 -86
  15. package/dist/async/utilities/lazy-promise/lazy-promise.js.map +1 -1
  16. package/dist/async/utilities/retry/retry-or-fail.d.ts +1 -1
  17. package/dist/async/utilities/retry/retry-or-fail.js +1 -1
  18. package/dist/async/utilities/retry/retry-or-fail.js.map +1 -1
  19. package/dist/async/utilities/retry/retry.d.ts +1 -1
  20. package/dist/async/utilities/retry/retry.js.map +1 -1
  21. package/dist/cache/implementations/derivables/cache/cache.d.ts +13 -8
  22. package/dist/cache/implementations/derivables/cache/cache.js +7 -23
  23. package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
  24. package/dist/cache/implementations/derivables/cache-factory/cache-factory.d.ts +3 -22
  25. package/dist/cache/implementations/derivables/cache-factory/cache-factory.js +2 -42
  26. package/dist/cache/implementations/derivables/cache-factory/cache-factory.js.map +1 -1
  27. package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.d.ts +5 -8
  28. package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js +5 -19
  29. package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
  30. package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +13 -8
  31. package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +9 -28
  32. package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
  33. package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.d.ts +3 -29
  34. package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js +2 -48
  35. package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js.map +1 -1
  36. package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +14 -8
  37. package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +7 -24
  38. package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
  39. package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +3 -25
  40. package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js +2 -47
  41. package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
  42. package/dist/utilities/classes/_module.d.ts +1 -0
  43. package/dist/utilities/classes/_module.js +1 -0
  44. package/dist/utilities/classes/_module.js.map +1 -1
  45. package/dist/utilities/classes/sync-event-bus/_module.d.ts +1 -0
  46. package/dist/utilities/classes/sync-event-bus/_module.js +2 -0
  47. package/dist/utilities/classes/sync-event-bus/_module.js.map +1 -0
  48. package/dist/utilities/classes/sync-event-bus/sync-event-bus.d.ts +16 -0
  49. package/dist/utilities/classes/sync-event-bus/sync-event-bus.js +53 -0
  50. package/dist/utilities/classes/sync-event-bus/sync-event-bus.js.map +1 -0
  51. package/dist/utilities/contracts/_module.d.ts +1 -0
  52. package/dist/utilities/contracts/_module.js +1 -0
  53. package/dist/utilities/contracts/_module.js.map +1 -1
  54. package/dist/utilities/contracts/sync-event-bus-listenable.d.ts +13 -0
  55. package/dist/utilities/contracts/sync-event-bus-listenable.js +5 -0
  56. package/dist/utilities/contracts/sync-event-bus-listenable.js.map +1 -0
  57. package/dist/utilities/functions/invokable.d.ts +4 -0
  58. package/dist/utilities/functions/invokable.js +9 -0
  59. package/dist/utilities/functions/invokable.js.map +1 -1
  60. package/package.json +1 -1
@@ -1,3 +1,4 @@
1
1
  export * from "../async/backof-policies/_module.js";
2
2
  export * from "../async/utilities/_module.js";
3
3
  export * from "../async/async.errors.js";
4
+ export * from "../async/async.events.js";
@@ -1,4 +1,5 @@
1
1
  export * from "../async/backof-policies/_module.js";
2
2
  export * from "../async/utilities/_module.js";
3
3
  export * from "../async/async.errors.js";
4
+ export * from "../async/async.events.js";
4
5
  //# sourceMappingURL=_module-exports.js.map
@@ -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,56 +2,79 @@
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 LazyPromiseOnFinally = () => Promisable<void>;
13
- /**
14
- *
15
- * IMPORT_PATH: ```"@daiso-tech/core/async"```
16
- * @group Utilities
17
- */
18
- export type LazyPromiseOnSuccess<TValue> = (value: TValue) => Promisable<void>;
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 LazyPromiseOnError = (error: unknown) => Promisable<void>;
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 LazyPromiseCallbacks<TValue = unknown> = {
31
- onFinally?: LazyPromiseOnFinally;
32
- onSuccess?: LazyPromiseOnSuccess<TValue>;
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 LazyPromiseSettingsBase = {
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 LazyPromiseSettings<TValue = unknown> = LazyPromiseCallbacks<TValue> & LazyPromiseSettingsBase & {
53
- abortSignal?: AbortSignal | null;
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
@@ -59,7 +82,7 @@ export type LazyPromiseSettings<TValue = unknown> = LazyPromiseCallbacks<TValue>
59
82
  * - <i>setRetryAttempts</i>
60
83
  * - <i>setRetryTimeout</i>
61
84
  * - <i>setRetryPolicy</i>
62
- * - <i>backoffPolicy</i>
85
+ * - <i>setBackoffPolicy</i>
63
86
  * - <i>setTotalTimeout</i>
64
87
  * - <i>setAbortSignal</i>
65
88
  *
@@ -75,7 +98,7 @@ export type LazyPromiseSettings<TValue = unknown> = LazyPromiseCallbacks<TValue>
75
98
  * IMPORT_PATH: ```"@daiso-tech/core/async"```
76
99
  * @group Utilities
77
100
  */
78
- export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
101
+ export declare class LazyPromise<TValue> implements PromiseLike<TValue>, ISyncEventListenable<AsyncEvents<TValue>> {
79
102
  /**
80
103
  * The <i>wrapFn</i> is convience method used for wrapping async <i>{@link Invokable}</i> with a <i>LazyPromise</i>.
81
104
  * @example
@@ -91,7 +114,7 @@ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
91
114
  * .setRetryTimeout(TimeSpan.fromMinutes(1));
92
115
  * ```
93
116
  */
94
- static wrapFn<TArgs extends unknown[], TReturn>(fn: Invokable<TArgs, Promisable<TReturn>>, settings?: LazyPromiseSettings<TReturn>): InvokableFn<TArgs, LazyPromise<TReturn>>;
117
+ static wrapFn<TArgs extends unknown[], TReturn>(fn: Invokable<TArgs, Promisable<TReturn>>, settings?: LazyPromiseSettings): InvokableFn<TArgs, LazyPromise<TReturn>>;
95
118
  /**
96
119
  * The <i>delay</i> method creates a <i>{@link LazyPromise}</i> that will be fulfilled after given <i>time</i>.
97
120
  *
@@ -111,22 +134,44 @@ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
111
134
  /**
112
135
  * The <i>all<i> method works similarly to <i>{@link Promise.all}</i> with the key distinction that it operates lazily.
113
136
  */
114
- static all<TValue>(promises: LazyPromise<TValue>[]): LazyPromise<TValue[]>;
137
+ static all<TValue>(promises: LazyPromise<TValue>[], settings?: LazyPromiseSettings): LazyPromise<TValue[]>;
115
138
  /**
116
139
  * The <i>allSettled<i> method works similarly to <i>{@link Promise.allSettled}</i> with the key distinction that it operates lazily.
117
140
  */
118
- static allSettled<TValue>(promises: LazyPromise<TValue>[]): LazyPromise<PromiseSettledResult<TValue>[]>;
141
+ static allSettled<TValue>(promises: LazyPromise<TValue>[], settings?: LazyPromiseSettings): LazyPromise<PromiseSettledResult<TValue>[]>;
119
142
  /**
120
143
  * The <i>race<i> method works similarly to <i>{@link Promise.race}</i> with the key distinction that it operates lazily.
121
144
  */
122
- static race<TValue>(promises: LazyPromise<TValue>[]): LazyPromise<TValue>;
145
+ static race<TValue>(promises: LazyPromise<TValue>[], settings?: LazyPromiseSettings): LazyPromise<TValue>;
123
146
  /**
124
147
  * The <i>any<i> method works similarly to <i>{@link Promise.any}</i> with the key distinction that it operates lazily.
125
148
  */
126
- 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>;
127
171
  private promise;
128
172
  private asyncFn;
129
173
  private readonly settings;
174
+ private readonly eventBus;
130
175
  /**
131
176
  * @example
132
177
  * ```ts
@@ -139,19 +184,17 @@ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
139
184
  * // "I am lazy" will only logged when awaited or then method i called.
140
185
  * await promise;
141
186
  * ```
187
+ *
188
+ * You can pass sync or async <i>{@link Invokable}</i>.
142
189
  */
143
- constructor(asyncFn: AsyncLazy<TValue>, settings?: LazyPromiseSettings<TValue>);
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;
144
193
  private applyRetryTimeout;
145
194
  private applyRetry;
146
195
  private applyTotalTimeout;
147
196
  private applyAbort;
148
197
  private applySettings;
149
- /**
150
- * @throws {AbortAsyncError} {@link AbortAsyncError}
151
- * @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
152
- * @throws {RetryAsyncError} {@link RetryAsyncError}
153
- */
154
- then<TResult1 = TValue, TResult2 = never>(onfulfilled?: ((value: TValue) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
155
198
  /**
156
199
  * The <i>setBackoffPolicy</i> method is used for setting a custom <i>{@link BackoffPolicy}</i>.
157
200
  * ```ts
@@ -270,9 +313,12 @@ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
270
313
  * ```
271
314
  */
272
315
  setAbortSignal(abortSignal: AbortSignal | null): LazyPromise<TValue>;
273
- onFinally(cb: LazyPromiseOnFinally): LazyPromise<TValue>;
274
- onSuccess(cb: LazyPromiseOnSuccess<TValue>): LazyPromise<TValue>;
275
- onError(cb: LazyPromiseOnError): LazyPromise<TValue>;
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>;
276
322
  /**
277
323
  * The <i>defer</i> method executes the <i>LazyPromise</i> without awaiting it.
278
324
  * @example