@daiso-tech/core 0.37.2 → 0.38.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 (155) hide show
  1. package/dist/async/_module-exports.d.ts +1 -1
  2. package/dist/async/_module-exports.js +1 -1
  3. package/dist/async/_module-exports.js.map +1 -1
  4. package/dist/async/async.errors.d.ts +0 -20
  5. package/dist/async/async.errors.js +0 -16
  6. package/dist/async/async.errors.js.map +1 -1
  7. package/dist/async/middlewares/_module.d.ts +0 -1
  8. package/dist/async/middlewares/_module.js +0 -1
  9. package/dist/async/middlewares/_module.js.map +1 -1
  10. package/dist/async/middlewares/bulkhead/bulkhead.middleware.js +1 -1
  11. package/dist/async/middlewares/bulkhead/bulkhead.middleware.js.map +1 -1
  12. package/dist/async/middlewares/fallback/_module.d.ts +1 -0
  13. package/dist/async/middlewares/fallback/_module.js +1 -0
  14. package/dist/async/middlewares/fallback/_module.js.map +1 -1
  15. package/dist/async/middlewares/fallback/fallback.middleware.d.ts +25 -49
  16. package/dist/async/middlewares/fallback/fallback.middleware.js +52 -14
  17. package/dist/async/middlewares/fallback/fallback.middleware.js.map +1 -1
  18. package/dist/async/middlewares/fallback/fallback.types.d.ts +42 -0
  19. package/dist/async/middlewares/fallback/fallback.types.js +6 -0
  20. package/dist/async/middlewares/fallback/fallback.types.js.map +1 -0
  21. package/dist/async/middlewares/hedging/_module.d.ts +2 -2
  22. package/dist/async/middlewares/hedging/_module.js +2 -2
  23. package/dist/async/middlewares/hedging/_module.js.map +1 -1
  24. package/dist/async/middlewares/hedging/{_shared.d.ts → hedging.types.d.ts} +37 -9
  25. package/dist/async/middlewares/hedging/hedging.types.js +6 -0
  26. package/dist/async/middlewares/hedging/hedging.types.js.map +1 -0
  27. package/dist/async/middlewares/hedging/{sequential-hedging/sequential-hedging.middleware.d.ts → sequential-hedging.middleware.d.ts} +3 -3
  28. package/dist/async/middlewares/hedging/sequential-hedging.middleware.js +133 -0
  29. package/dist/async/middlewares/hedging/sequential-hedging.middleware.js.map +1 -0
  30. package/dist/async/middlewares/observe/_module.d.ts +1 -0
  31. package/dist/async/middlewares/observe/_module.js +1 -0
  32. package/dist/async/middlewares/observe/_module.js.map +1 -1
  33. package/dist/async/middlewares/observe/observe.middleware.d.ts +37 -87
  34. package/dist/async/middlewares/observe/observe.middleware.js +53 -2
  35. package/dist/async/middlewares/observe/observe.middleware.js.map +1 -1
  36. package/dist/async/middlewares/observe/observe.types.d.ts +91 -0
  37. package/dist/async/middlewares/observe/observe.types.js +6 -0
  38. package/dist/async/middlewares/observe/observe.types.js.map +1 -0
  39. package/dist/async/middlewares/retry/_module.d.ts +1 -0
  40. package/dist/async/middlewares/retry/_module.js +1 -0
  41. package/dist/async/middlewares/retry/_module.js.map +1 -1
  42. package/dist/async/middlewares/retry/retry.middleware.d.ts +36 -86
  43. package/dist/async/middlewares/retry/retry.middleware.js +70 -22
  44. package/dist/async/middlewares/retry/retry.middleware.js.map +1 -1
  45. package/dist/async/middlewares/retry/retry.types.d.ts +77 -0
  46. package/dist/async/middlewares/retry/retry.types.js +7 -0
  47. package/dist/async/middlewares/retry/retry.types.js.map +1 -0
  48. package/dist/async/middlewares/timeout/_module.d.ts +1 -0
  49. package/dist/async/middlewares/timeout/_module.js +1 -0
  50. package/dist/async/middlewares/timeout/_module.js.map +1 -1
  51. package/dist/async/middlewares/timeout/timeout.middleware.d.ts +1 -47
  52. package/dist/async/middlewares/timeout/timeout.middleware.js +1 -1
  53. package/dist/async/middlewares/timeout/timeout.middleware.js.map +1 -1
  54. package/dist/async/middlewares/timeout/timeout.type.d.ts +51 -0
  55. package/dist/async/middlewares/timeout/timeout.type.js +6 -0
  56. package/dist/async/middlewares/timeout/timeout.type.js.map +1 -0
  57. package/dist/async/utilities/promise-queue/promise-queue.js +19 -8
  58. package/dist/async/utilities/promise-queue/promise-queue.js.map +1 -1
  59. package/dist/event-bus/contracts/event-bus.contract.d.ts +0 -6
  60. package/dist/event-bus/contracts/event-bus.contract.js +1 -7
  61. package/dist/event-bus/contracts/event-bus.contract.js.map +1 -1
  62. package/dist/event-bus/contracts/event-bus.errors.d.ts +0 -32
  63. package/dist/event-bus/contracts/event-bus.errors.js +1 -49
  64. package/dist/event-bus/contracts/event-bus.errors.js.map +1 -1
  65. package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +6 -11
  66. package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
  67. package/dist/lock/contracts/lock.contract.d.ts +2 -18
  68. package/dist/lock/contracts/lock.errors.d.ts +0 -24
  69. package/dist/lock/contracts/lock.errors.js +0 -46
  70. package/dist/lock/contracts/lock.errors.js.map +1 -1
  71. package/dist/lock/implementations/derivables/lock-provider/lock.js +4 -4
  72. package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
  73. package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +15 -19
  74. package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
  75. package/dist/utilities/_module-exports.d.ts +1 -1
  76. package/dist/utilities/_module-exports.js +1 -1
  77. package/dist/utilities/_module-exports.js.map +1 -1
  78. package/dist/utilities/classes/hooks/async-hooks.d.ts +6 -4
  79. package/dist/utilities/classes/hooks/async-hooks.js +3 -1
  80. package/dist/utilities/classes/hooks/async-hooks.js.map +1 -1
  81. package/dist/utilities/classes/hooks/hooks.d.ts +1 -1
  82. package/dist/utilities/classes/hooks/hooks.js +1 -1
  83. package/dist/utilities/classes/hooks/hooks.js.map +1 -1
  84. package/dist/utilities/classes/namespace/namespace.d.ts +2 -2
  85. package/dist/utilities/classes/namespace/namespace.js.map +1 -1
  86. package/dist/utilities/functions/{_module-exports.d.ts → _module.d.ts} +5 -1
  87. package/dist/utilities/functions/{_module-exports.js → _module.js} +6 -2
  88. package/dist/utilities/functions/_module.js.map +1 -0
  89. package/dist/utilities/functions/error-policy.d.ts +54 -0
  90. package/dist/utilities/functions/error-policy.js +63 -0
  91. package/dist/utilities/functions/error-policy.js.map +1 -0
  92. package/dist/utilities/functions/invokable.d.ts +57 -1
  93. package/dist/utilities/functions/invokable.js +3 -1
  94. package/dist/utilities/functions/invokable.js.map +1 -1
  95. package/dist/utilities/functions/is-class.d.ts +5 -0
  96. package/dist/utilities/functions/is-class.js +11 -0
  97. package/dist/utilities/functions/is-class.js.map +1 -0
  98. package/dist/utilities/functions/is-standard-schema.d.ts +2 -0
  99. package/dist/utilities/functions/is-standard-schema.js +5 -0
  100. package/dist/utilities/functions/is-standard-schema.js.map +1 -0
  101. package/dist/utilities/functions/lazy.d.ts +27 -1
  102. package/dist/utilities/functions/lazy.js.map +1 -1
  103. package/dist/utilities/functions/option.d.ts +32 -0
  104. package/dist/utilities/functions/option.js +25 -0
  105. package/dist/utilities/functions/option.js.map +1 -0
  106. package/dist/utilities/functions/resolve-one-or-more.d.ts +5 -1
  107. package/dist/utilities/functions/resolve-one-or-more.js.map +1 -1
  108. package/dist/utilities/functions/result.d.ts +81 -1
  109. package/dist/utilities/functions/result.js +46 -2
  110. package/dist/utilities/functions/result.js.map +1 -1
  111. package/dist/utilities/types/_module.d.ts +1 -7
  112. package/dist/utilities/types/_module.js +1 -7
  113. package/dist/utilities/types/_module.js.map +1 -1
  114. package/dist/utilities/types/any-class.type.d.ts +3 -0
  115. package/dist/utilities/types/any-class.type.js +2 -0
  116. package/dist/utilities/types/any-class.type.js.map +1 -0
  117. package/package.json +4 -7
  118. package/dist/async/middlewares/_shared.d.ts +0 -15
  119. package/dist/async/middlewares/_shared.js +0 -15
  120. package/dist/async/middlewares/_shared.js.map +0 -1
  121. package/dist/async/middlewares/hedging/_shared.js +0 -5
  122. package/dist/async/middlewares/hedging/_shared.js.map +0 -1
  123. package/dist/async/middlewares/hedging/concurrent-hedging/_module.d.ts +0 -1
  124. package/dist/async/middlewares/hedging/concurrent-hedging/_module.js +0 -2
  125. package/dist/async/middlewares/hedging/concurrent-hedging/_module.js.map +0 -1
  126. package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.d.ts +0 -51
  127. package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.js +0 -138
  128. package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.js.map +0 -1
  129. package/dist/async/middlewares/hedging/sequential-hedging/_module.d.ts +0 -1
  130. package/dist/async/middlewares/hedging/sequential-hedging/_module.js +0 -2
  131. package/dist/async/middlewares/hedging/sequential-hedging/_module.js.map +0 -1
  132. package/dist/async/middlewares/hedging/sequential-hedging/sequential-hedging.middleware.js +0 -104
  133. package/dist/async/middlewares/hedging/sequential-hedging/sequential-hedging.middleware.js.map +0 -1
  134. package/dist/utilities/functions/_module-exports.js.map +0 -1
  135. package/dist/utilities/types/at-least-one.type.d.ts +0 -8
  136. package/dist/utilities/types/at-least-one.type.js +0 -5
  137. package/dist/utilities/types/at-least-one.type.js.map +0 -1
  138. package/dist/utilities/types/factory.type.d.ts +0 -44
  139. package/dist/utilities/types/factory.type.js +0 -5
  140. package/dist/utilities/types/factory.type.js.map +0 -1
  141. package/dist/utilities/types/invokable.type.d.ts +0 -20
  142. package/dist/utilities/types/invokable.type.js +0 -5
  143. package/dist/utilities/types/invokable.type.js.map +0 -1
  144. package/dist/utilities/types/items.type.d.ts +0 -8
  145. package/dist/utilities/types/items.type.js +0 -5
  146. package/dist/utilities/types/items.type.js.map +0 -1
  147. package/dist/utilities/types/lazy.type.d.ts +0 -31
  148. package/dist/utilities/types/lazy.type.js +0 -5
  149. package/dist/utilities/types/lazy.type.js.map +0 -1
  150. package/dist/utilities/types/one-or-more.type.d.ts +0 -8
  151. package/dist/utilities/types/one-or-more.type.js +0 -5
  152. package/dist/utilities/types/one-or-more.type.js.map +0 -1
  153. package/dist/utilities/types/result.type.d.ts +0 -18
  154. package/dist/utilities/types/result.type.js +0 -5
  155. package/dist/utilities/types/result.type.js.map +0 -1
@@ -1,4 +1,4 @@
1
1
  export * from "../async/backof-policies/_module.js";
2
2
  export * from "../async/middlewares/_module.js";
3
- export * from "../async/utilities/_module.js";
3
+ export * from "../async/utilities/lazy-promise/_module.js";
4
4
  export * from "../async/async.errors.js";
@@ -1,5 +1,5 @@
1
1
  export * from "../async/backof-policies/_module.js";
2
2
  export * from "../async/middlewares/_module.js";
3
- export * from "../async/utilities/_module.js";
3
+ export * from "../async/utilities/lazy-promise/_module.js";
4
4
  export * from "../async/async.errors.js";
5
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,gCAAgC,CAAC;AAC/C,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,gCAAgC,CAAC;AAC/C,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yBAAyB,CAAC"}
@@ -17,25 +17,6 @@ export declare class AsyncError extends Error {
17
17
  export declare class TimeoutAsyncError extends AsyncError {
18
18
  constructor(message: string, cause?: unknown);
19
19
  }
20
- /**
21
- *
22
- * IMPORT_PATH: `"@daiso-tech/core/async"`
23
- * @group Errors
24
- */
25
- export type RetryAsyncErrorData = {
26
- errors: unknown[];
27
- maxAttempts: number;
28
- };
29
- /**
30
- *
31
- * IMPORT_PATH: `"@daiso-tech/core/async"`
32
- * @group Errors
33
- */
34
- export declare class RetryAsyncError extends AsyncError {
35
- readonly maxAttempts: number;
36
- readonly errors: unknown[];
37
- constructor(message: string, { errors, maxAttempts }: RetryAsyncErrorData);
38
- }
39
20
  /**
40
21
  *
41
22
  * IMPORT_PATH: `"@daiso-tech/core/async"`
@@ -61,7 +42,6 @@ export declare class CapacityFullAsyncError extends AsyncError {
61
42
  export declare const ASYNC_ERRORS: {
62
43
  readonly Base: typeof AsyncError;
63
44
  readonly Timeout: typeof TimeoutAsyncError;
64
- readonly Retry: typeof RetryAsyncError;
65
45
  readonly Hedging: typeof HedgingAsyncError;
66
46
  readonly CapacityFull: typeof CapacityFullAsyncError;
67
47
  };
@@ -23,21 +23,6 @@ export class TimeoutAsyncError extends AsyncError {
23
23
  this.name = TimeoutAsyncError.name;
24
24
  }
25
25
  }
26
- /**
27
- *
28
- * IMPORT_PATH: `"@daiso-tech/core/async"`
29
- * @group Errors
30
- */
31
- export class RetryAsyncError extends AsyncError {
32
- maxAttempts;
33
- errors = [];
34
- constructor(message, { errors, maxAttempts }) {
35
- super(message, errors);
36
- this.errors = errors;
37
- this.maxAttempts = maxAttempts;
38
- this.name = RetryAsyncError.name;
39
- }
40
- }
41
26
  /**
42
27
  *
43
28
  * IMPORT_PATH: `"@daiso-tech/core/async"`
@@ -70,7 +55,6 @@ export class CapacityFullAsyncError extends AsyncError {
70
55
  export const ASYNC_ERRORS = {
71
56
  Base: AsyncError,
72
57
  Timeout: TimeoutAsyncError,
73
- Retry: RetryAsyncError,
74
58
  Hedging: HedgingAsyncError,
75
59
  CapacityFull: CapacityFullAsyncError,
76
60
  };
@@ -1 +1 @@
1
- {"version":3,"file":"async.errors.js","sourceRoot":"","sources":["../../src/async/async.errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;GAIG;AACH,MAAM,OAAO,UAAW,SAAQ,KAAK;IACjC,YAAY,OAAe,EAAE,KAAe;QACxC,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC;IAChC,CAAC;CACJ;AAED;;;;GAIG;AACH,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IAC7C,YAAY,OAAe,EAAE,KAAe;QACxC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC,IAAI,CAAC;IACvC,CAAC;CACJ;AAYD;;;;G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C1B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC;IAChC,CAAC;CACJ;AAED;;;;GAIG;AACH,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IAC7C,YAAY,OAAe,EAAE,KAAe;QACxC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC,IAAI,CAAC;IACvC,CAAC;CACJ;AAED;;;;GAIG;AACH,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IAGzB;IAFpB,YACI,OAAe,EACC,MAAiB;QAEjC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAFP,WAAM,GAAN,MAAM,CAAW;QAGjC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC,IAAI,CAAC;IACvC,CAAC;CACJ;AAED;;;;GAIG;AACH,MAAM,OAAO,sBAAuB,SAAQ,UAAU;IAClD,YAAY,OAAe,EAAE,KAAe;QACxC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC,IAAI,CAAC;IAC5C,CAAC;CACJ;AACD;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IACxB,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,iBAAiB;IAC1B,OAAO,EAAE,iBAAiB;IAC1B,YAAY,EAAE,sBAAsB;CAC9B,CAAC"}
@@ -5,4 +5,3 @@ export * from "../../async/middlewares/hedging/_module.js";
5
5
  export * from "../../async/middlewares/observe/_module.js";
6
6
  export * from "../../async/middlewares/retry/_module.js";
7
7
  export * from "../../async/middlewares/timeout/_module.js";
8
- export * from "../../async/middlewares/_shared.js";
@@ -5,5 +5,4 @@ export * from "../../async/middlewares/hedging/_module.js";
5
5
  export * from "../../async/middlewares/observe/_module.js";
6
6
  export * from "../../async/middlewares/retry/_module.js";
7
7
  export * from "../../async/middlewares/timeout/_module.js";
8
- export * from "../../async/middlewares/_shared.js";
9
8
  //# sourceMappingURL=_module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../src/async/middlewares/_module.ts"],"names":[],"mappings":"AAAA,cAAc,yCAAyC,CAAC;AACxD,cAAc,wCAAwC,CAAC;AACvD,cAAc,yCAAyC,CAAC;AACxD,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AACvD,cAAc,sCAAsC,CAAC;AACrD,cAAc,wCAAwC,CAAC;AACvD,cAAc,gCAAgC,CAAC"}
1
+ {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../src/async/middlewares/_module.ts"],"names":[],"mappings":"AAAA,cAAc,yCAAyC,CAAC;AACxD,cAAc,wCAAwC,CAAC;AACvD,cAAc,yCAAyC,CAAC;AACxD,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AACvD,cAAc,sCAAsC,CAAC;AACrD,cAAc,wCAAwC,CAAC"}
@@ -2,7 +2,7 @@
2
2
  * @module Async
3
3
  */
4
4
  import { callInvokable, TimeSpan, } from "../../../utilities/_module-exports.js";
5
- import { PromiseQueue } from "../../../async/utilities/_module.js";
5
+ import { PromiseQueue } from "../../../async/utilities/promise-queue/_module.js";
6
6
  /**
7
7
  * The `bulkhead` middlewares ensures that a given amount of {@link Promiselike | `PromiseLike`} objects run at the same time concurrently and the rest will be queued up.
8
8
  * You can provide {@link BulkheadSettings | `settings.maxCapacity`}
@@ -1 +1 @@
1
- {"version":3,"file":"bulkhead.middleware.js","sourceRoot":"","sources":["../../../../src/async/middlewares/bulkhead/bulkhead.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACH,aAAa,EACb,QAAQ,GAIX,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAwE5D;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,QAAQ,CAKpB,WAAoD,EAAE;IAEtD,MAAM,EACF,cAAc,GAAG,EAAE,EACnB,WAAW,GAAG,IAAI,EAClB,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,EACvC,YAAY,GAAG,GAAG,EAAE,GAAE,CAAC,GAC1B,GAAG,QAAQ,CAAC;IACb,MAAM,YAAY,GAAG,IAAI,YAAY,CAAC;QAClC,WAAW;QACX,cAAc;QACd,QAAQ;KACX,CAAC,CAAC;IACH,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE;QAC7C,OAAO,MAAM,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE;YAC/B,aAAa,CAAC,YAAY,EAAE;gBACxB,IAAI;gBACJ,OAAO;aACV,CAAC,CAAC;YACH,OAAO,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;QACzB,CAAC,EAAE,MAAM,CAAC,CAAC;IACf,CAAC,CAAC;AACN,CAAC"}
1
+ {"version":3,"file":"bulkhead.middleware.js","sourceRoot":"","sources":["../../../../src/async/middlewares/bulkhead/bulkhead.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACH,aAAa,EACb,QAAQ,GAIX,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,YAAY,EAAE,MAAM,4CAA4C,CAAC;AAwE1E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,QAAQ,CAKpB,WAAoD,EAAE;IAEtD,MAAM,EACF,cAAc,GAAG,EAAE,EACnB,WAAW,GAAG,IAAI,EAClB,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,EACvC,YAAY,GAAG,GAAG,EAAE,GAAE,CAAC,GAC1B,GAAG,QAAQ,CAAC;IACb,MAAM,YAAY,GAAG,IAAI,YAAY,CAAC;QAClC,WAAW;QACX,cAAc;QACd,QAAQ;KACX,CAAC,CAAC;IACH,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE;QAC7C,OAAO,MAAM,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE;YAC/B,aAAa,CAAC,YAAY,EAAE;gBACxB,IAAI;gBACJ,OAAO;aACV,CAAC,CAAC;YACH,OAAO,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;QACzB,CAAC,EAAE,MAAM,CAAC,CAAC;IACf,CAAC,CAAC;AACN,CAAC"}
@@ -1 +1,2 @@
1
1
  export * from "../../../async/middlewares/fallback/fallback.middleware.js";
2
+ export * from "../../../async/middlewares/fallback/fallback.types.js";
@@ -1,2 +1,3 @@
1
1
  export * from "../../../async/middlewares/fallback/fallback.middleware.js";
2
+ export * from "../../../async/middlewares/fallback/fallback.types.js";
2
3
  //# sourceMappingURL=_module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../../src/async/middlewares/fallback/_module.ts"],"names":[],"mappings":"AAAA,cAAc,qDAAqD,CAAC"}
1
+ {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../../src/async/middlewares/fallback/_module.ts"],"names":[],"mappings":"AAAA,cAAc,qDAAqD,CAAC;AACpE,cAAc,gDAAgD,CAAC"}
@@ -1,54 +1,9 @@
1
1
  /**
2
2
  * @module Async
3
3
  */
4
- import type { HookContext, AsyncLazyable } from "../../../utilities/_module-exports.js";
5
- import { type AsyncMiddlewareFn, type Invokable } from "../../../utilities/_module-exports.js";
6
- import { type ErrorPolicy } from "../../../async/middlewares/_shared.js";
7
- /**
8
- *
9
- * IMPORT_PATH: `"@daiso-tech/core/async"`
10
- * @group Middlewares
11
- */
12
- export type OnFallbackData<TParameters extends unknown[] = unknown[], TFallbackValue = unknown, TContext extends HookContext = HookContext> = {
13
- error: unknown;
14
- fallbackValue: TFallbackValue;
15
- args: TParameters;
16
- context: TContext;
17
- };
18
- /**
19
- *
20
- * IMPORT_PATH: `"@daiso-tech/core/async"`
21
- * @group Middlewares
22
- */
23
- export type OnFallback<TParameters extends unknown[] = unknown[], TFallbackValue = unknown, TContext extends HookContext = HookContext> = Invokable<[data: OnFallbackData<TParameters, TFallbackValue, TContext>]>;
24
- /**
25
- *
26
- * IMPORT_PATH: `"@daiso-tech/core/async"`
27
- * @group Middlewares
28
- */
29
- export type FallbackCallbacks<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext extends HookContext = HookContext> = {
30
- /**
31
- * Callback {@link Invokable | `Invokable`} that will be called before fallback value is returned.
32
- */
33
- onFallback?: OnFallback<TParameters, TReturn, TContext>;
34
- };
35
- /**
36
- *
37
- * IMPORT_PATH: `"@daiso-tech/core/async"`
38
- * @group Middlewares
39
- */
40
- export type FallbackSettings<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext extends HookContext = HookContext> = FallbackCallbacks<TParameters, TReturn, TContext> & {
41
- fallbackValue: AsyncLazyable<TReturn>;
42
- /**
43
- * You can choose what errors you want to add fallback value. By default fallback value will be added to all errors.
44
- *
45
- * @default
46
- * ```ts
47
- * (_error: unknown) => true
48
- * ```
49
- */
50
- errorPolicy?: ErrorPolicy;
51
- };
4
+ import { type HookContext } from "../../../utilities/_module-exports.js";
5
+ import type { AsyncMiddlewareFn } from "../../../utilities/_module-exports.js";
6
+ import type { FallbackSettings } from "../../../async/middlewares/fallback/fallback.types.js";
52
7
  /**
53
8
  * The `fallback` middleware adds fallback value when an error occurs.
54
9
  *
@@ -74,5 +29,26 @@ export type FallbackSettings<TParameters extends unknown[] = unknown[], TReturn
74
29
  * // Will return null when the fetch method throws an error.
75
30
  * console.log(await fetchData.invoke("URL_ENDPOINT"));
76
31
  * ```
32
+ *
33
+ * The middleware works also when the function returns a {@link Result | `Result`} type.
34
+ * @example
35
+ * ```ts
36
+ * import { fallback } from "@daiso-tech/core/async";
37
+ * import { AsyncHooks, Result, resultFailure, resultSuccess } from "@daiso-tech/core/utilities";
38
+ *
39
+ * const fetchData = new AsyncHooks(async (url: string): Promise<Result> => {
40
+ * const response = await fetch(url);
41
+ * const json = await response.json();
42
+ * if (!response.ok) {
43
+ * return resultFailure(json);
44
+ * }
45
+ * return resultSuccess(json);
46
+ * }, [
47
+ * fallback({ fallbackValue: null })
48
+ * ]);
49
+ *
50
+ * // Will return null when the fetch method throws an error.
51
+ * console.log(await fetchData.invoke("URL_ENDPOINT"));
52
+ * ```
77
53
  */
78
- export declare function fallback<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings: NoInfer<FallbackSettings<TParameters, TReturn, TContext>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;
54
+ export declare function fallback<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings: NoInfer<FallbackSettings<TParameters, TReturn, TContext, TReturn>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * @module Async
3
3
  */
4
- import { callInvokable, resolveAsyncLazyable, } from "../../../utilities/_module-exports.js";
5
- import { callErrorPolicy, } from "../../../async/middlewares/_shared.js";
4
+ import { resolveAsyncLazyable, callInvokable, resultSuccess, callErrorPolicyOnValue, isResultFailure, } from "../../../utilities/_module-exports.js";
5
+ import { callErrorPolicyOnThrow } from "../../../utilities/_module-exports.js";
6
6
  /**
7
7
  * The `fallback` middleware adds fallback value when an error occurs.
8
8
  *
@@ -28,25 +28,63 @@ import { callErrorPolicy, } from "../../../async/middlewares/_shared.js";
28
28
  * // Will return null when the fetch method throws an error.
29
29
  * console.log(await fetchData.invoke("URL_ENDPOINT"));
30
30
  * ```
31
+ *
32
+ * The middleware works also when the function returns a {@link Result | `Result`} type.
33
+ * @example
34
+ * ```ts
35
+ * import { fallback } from "@daiso-tech/core/async";
36
+ * import { AsyncHooks, Result, resultFailure, resultSuccess } from "@daiso-tech/core/utilities";
37
+ *
38
+ * const fetchData = new AsyncHooks(async (url: string): Promise<Result> => {
39
+ * const response = await fetch(url);
40
+ * const json = await response.json();
41
+ * if (!response.ok) {
42
+ * return resultFailure(json);
43
+ * }
44
+ * return resultSuccess(json);
45
+ * }, [
46
+ * fallback({ fallbackValue: null })
47
+ * ]);
48
+ *
49
+ * // Will return null when the fetch method throws an error.
50
+ * console.log(await fetchData.invoke("URL_ENDPOINT"));
51
+ * ```
31
52
  */
32
53
  export function fallback(settings) {
33
- const { fallbackValue, errorPolicy = () => true, onFallback = () => { }, } = settings;
54
+ const { fallbackValue, errorPolicy, onFallback = () => { } } = settings;
34
55
  return async (args, next, { context }) => {
35
56
  try {
36
- return await next(...args);
57
+ const value = await next(...args);
58
+ // Handle fallback value if an Result type is returned
59
+ if (!(await callErrorPolicyOnValue(errorPolicy, value))) {
60
+ return value;
61
+ }
62
+ // This is only needed for type inference
63
+ if (!isResultFailure(value)) {
64
+ return value;
65
+ }
66
+ const resolvedFallbackValue = await resolveAsyncLazyable(fallbackValue);
67
+ callInvokable(onFallback, {
68
+ error: value.error,
69
+ fallbackValue: resolvedFallbackValue,
70
+ args,
71
+ context,
72
+ });
73
+ return resultSuccess(resolvedFallbackValue);
74
+ // Handle fallback value if an error is thrown
37
75
  }
38
76
  catch (error) {
39
- if (await callErrorPolicy(errorPolicy, error)) {
40
- const resolvedFallbackValue = await resolveAsyncLazyable(fallbackValue);
41
- callInvokable(onFallback, {
42
- error,
43
- fallbackValue: resolvedFallbackValue,
44
- args,
45
- context,
46
- });
47
- return resolvedFallbackValue;
77
+ if (!(await callErrorPolicyOnThrow(errorPolicy, error))) {
78
+ throw error;
48
79
  }
49
- throw error;
80
+ const resolvedFallbackValue = await resolveAsyncLazyable(fallbackValue);
81
+ callInvokable(onFallback, {
82
+ error,
83
+ fallbackValue: resolvedFallbackValue,
84
+ args,
85
+ context,
86
+ });
87
+ return resolvedFallbackValue;
50
88
  }
51
89
  };
52
90
  }
@@ -1 +1 @@
1
- {"version":3,"file":"fallback.middleware.js","sourceRoot":"","sources":["../../../../src/async/middlewares/fallback/fallback.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH,OAAO,EACH,aAAa,EACb,oBAAoB,GAGvB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAEH,eAAe,GAClB,MAAM,gCAAgC,CAAC;AAoExC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,QAAQ,CAKpB,QAAmE;IAEnE,MAAM,EACF,aAAa,EACb,WAAW,GAAG,GAAG,EAAE,CAAC,IAAI,EACxB,UAAU,GAAG,GAAG,EAAE,GAAE,CAAC,GACxB,GAAG,QAAQ,CAAC;IACb,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,EAAoB,EAAE;QACvD,IAAI,CAAC;YACD,OAAO,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;QAC/B,CAAC;QAAC,OAAO,KAAc,EAAE,CAAC;YACtB,IAAI,MAAM,eAAe,CAAC,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC;gBAC5C,MAAM,qBAAqB,GACvB,MAAM,oBAAoB,CAAC,aAAa,CAAC,CAAC;gBAC9C,aAAa,CAAC,UAAU,EAAE;oBACtB,KAAK;oBACL,aAAa,EAAE,qBAAqB;oBACpC,IAAI;oBACJ,OAAO;iBACV,CAAC,CAAC;gBACH,OAAO,qBAAqB,CAAC;YACjC,CAAC;YACD,MAAM,KAAK,CAAC;QAChB,CAAC;IACL,CAAC,CAAC;AACN,CAAC"}
1
+ {"version":3,"file":"fallback.middleware.js","sourceRoot":"","sources":["../../../../src/async/middlewares/fallback/fallback.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAIH,oBAAoB,EACpB,aAAa,EACb,aAAa,EACb,sBAAsB,EACtB,eAAe,GAClB,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,UAAU,QAAQ,CAKpB,QAEC;IAED,MAAM,EAAE,aAAa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Lazyable, InferResultSuccess, InferResultError } from "../../../utilities/_module-exports.js";
5
+ import { type Invokable } from "../../../utilities/_module-exports.js";
6
+ import { type ErrorPolicySettings } from "../../../utilities/_module-exports.js";
7
+ /**
8
+ *
9
+ * IMPORT_PATH: `"@daiso-tech/core/async"`
10
+ * @group Middlewares
11
+ */
12
+ export type OnFallbackData<TParameters extends unknown[] = unknown[], TFallbackValue = unknown, TContext extends HookContext = HookContext> = {
13
+ error: unknown;
14
+ fallbackValue: TFallbackValue;
15
+ args: TParameters;
16
+ context: TContext;
17
+ };
18
+ /**
19
+ *
20
+ * IMPORT_PATH: `"@daiso-tech/core/async"`
21
+ * @group Middlewares
22
+ */
23
+ export type OnFallback<TParameters extends unknown[] = unknown[], TFallbackValue = unknown, TContext extends HookContext = HookContext> = Invokable<[data: OnFallbackData<TParameters, TFallbackValue, TContext>]>;
24
+ /**
25
+ *
26
+ * IMPORT_PATH: `"@daiso-tech/core/async"`
27
+ * @group Middlewares
28
+ */
29
+ export type FallbackCallbacks<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext extends HookContext = HookContext> = {
30
+ /**
31
+ * Callback {@link Invokable | `Invokable`} that will be called before fallback value is returned.
32
+ */
33
+ onFallback?: OnFallback<TParameters, TReturn, TContext>;
34
+ };
35
+ /**
36
+ *
37
+ * IMPORT_PATH: `"@daiso-tech/core/async"`
38
+ * @group Middlewares
39
+ */
40
+ export type FallbackSettings<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext extends HookContext = HookContext, TError = unknown> = FallbackCallbacks<TParameters, TReturn, TContext> & ErrorPolicySettings<InferResultError<TError>> & {
41
+ fallbackValue: AsyncLazyable<InferResultSuccess<TReturn>>;
42
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @module Async
3
+ */
4
+ import {} from "../../../utilities/_module-exports.js";
5
+ import {} from "../../../utilities/_module-exports.js";
6
+ //# sourceMappingURL=fallback.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fallback.types.js","sourceRoot":"","sources":["../../../../src/async/middlewares/fallback/fallback.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAQH,OAAO,EAAkB,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAA4B,MAAM,gCAAgC,CAAC"}
@@ -1,2 +1,2 @@
1
- export * from "../../../async/middlewares/hedging/concurrent-hedging/_module.js";
2
- export * from "../../../async/middlewares/hedging/sequential-hedging/_module.js";
1
+ export * from "../../../async/middlewares/hedging/hedging.types.js";
2
+ export * from "../../../async/middlewares/hedging/sequential-hedging.middleware.js";
@@ -1,3 +1,3 @@
1
- export * from "../../../async/middlewares/hedging/concurrent-hedging/_module.js";
2
- export * from "../../../async/middlewares/hedging/sequential-hedging/_module.js";
1
+ export * from "../../../async/middlewares/hedging/hedging.types.js";
2
+ export * from "../../../async/middlewares/hedging/sequential-hedging.middleware.js";
3
3
  //# sourceMappingURL=_module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../../src/async/middlewares/hedging/_module.ts"],"names":[],"mappings":"AAAA,cAAc,2DAA2D,CAAC;AAC1E,cAAc,2DAA2D,CAAC"}
1
+ {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../../src/async/middlewares/hedging/_module.ts"],"names":[],"mappings":"AAAA,cAAc,8CAA8C,CAAC;AAC7D,cAAc,8DAA8D,CAAC"}
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * @module Async
3
3
  */
4
- import type { TimeSpan } from "../../../utilities/_module-exports.js";
5
- import { type HookContext, type Invokable, type OneOrMore, type Promisable } from "../../../utilities/_module-exports.js";
4
+ import { type AsyncMiddleware, type HookContext, type OneOrMore } from "../../../utilities/_module-exports.js";
5
+ import type { ErrorPolicySettings, InferResultError } from "../../../utilities/_module-exports.js";
6
+ import { type Invokable, type Promisable } from "../../../utilities/_module-exports.js";
6
7
  /**
7
8
  *
8
9
  * IMPORT_PATH: `"@daiso-tech/core/async"`
@@ -74,20 +75,47 @@ export type NamedFallback<TParameters extends unknown[] = unknown[], TReturn = u
74
75
  * IMPORT_PATH: `"@daiso-tech/core/async"`
75
76
  * @group Middlewares
76
77
  */
77
- export type HedgingSettings<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext extends HookContext = HookContext> = HedgingCallbacks<TParameters, TContext> & {
78
+ export type HedgingSettings<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext extends HookContext = HookContext> = HedgingCallbacks<TParameters, TContext> & ErrorPolicySettings<InferResultError<TReturn>> & {
78
79
  /**
79
- * The maximum time to wait before automatically aborting the executing primary {@link Invokable | `Invokable`} or fallback {@link Invokable | `Invokable:s`}.
80
+ * The fallback functions that run in case the primary function fails.
80
81
  *
81
82
  * @default
82
83
  * ```ts
83
- * import { TimeSpan } from "@daiso-tech/core/utilities";
84
+ * import { retry } from "@daiso-tech/core/async";
85
+ * import { timeout } from "@daiso-tech/core/async";
84
86
  *
85
- * TimeSpan.fromSeconds(2)
87
+ * [timeout(), retry()]
86
88
  * ```
87
89
  */
88
- waitTime?: TimeSpan;
90
+ fallbacks: OneOrMore<Fallback<TParameters, TReturn> | NamedFallback<TParameters, TReturn>>;
89
91
  /**
90
- * The fallback functions that run in case the primary function fails.
92
+ * Middlewares to apply on all fallback functions and on primary function.
93
+ *
94
+ * You can wrap primary and fallback functions with for example `retry` and `timeout` middlewares.
95
+ * @example
96
+ * ```ts
97
+ * import { retry, timeout, sequentialHedging } from "@daiso-tech/core/async";
98
+ *
99
+ * new AsyncHooks(fn, [
100
+ * sequentialHedging({
101
+ * fallbacks: [
102
+ * fn1,
103
+ * fn2,
104
+ * fn3,
105
+ * ],
106
+ * middlewares: [
107
+ * timeout({
108
+ * waitTime: TimeSpan.fromSeconds(2),
109
+ * }),
110
+ * retry({
111
+ * maxAttempts: 4
112
+ * }),
113
+ * ]
114
+ * })
115
+ * ], {
116
+ * signalBinder,
117
+ * })
118
+ * ```
91
119
  */
92
- fallbacks: OneOrMore<Fallback<TParameters, TReturn> | NamedFallback<TParameters, TReturn>>;
120
+ middlewares?: Array<AsyncMiddleware<TParameters, TReturn>>;
93
121
  };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @module Async
3
+ */
4
+ import {} from "../../../utilities/_module-exports.js";
5
+ import {} from "../../../utilities/_module-exports.js";
6
+ //# sourceMappingURL=hedging.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hedging.types.js","sourceRoot":"","sources":["../../../../src/async/middlewares/hedging/hedging.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAIN,MAAM,gCAAgC,CAAC;AAKxC,OAAO,EAGN,MAAM,gCAAgC,CAAC"}
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * @module Async
3
3
  */
4
- import { type AsyncMiddlewareFn, type HookContext } from "../../../../utilities/_module-exports.js";
5
- import type { HedgingSettings } from "../../../../async/middlewares/hedging/_shared.js";
4
+ import { type AsyncMiddlewareFn, type HookContext } from "../../../utilities/_module-exports.js";
5
+ import { type HedgingSettings } from "../../../async/middlewares/hedging/hedging.types.js";
6
6
  /**
7
7
  * The `sequentialHedging` middleware executes the primary function and all fallback functions sequentially.
8
8
  * It returns the result of the first successful function and automatically cancels all remaining functions.
9
- * If all function fail than error is thrown.
9
+ * If all function fail than last error is thrown.
10
10
  *
11
11
  * IMPORT_PATH: `"@daiso-tech/core/async"`
12
12
  * @group Middlewares
@@ -0,0 +1,133 @@
1
+ /**
2
+ * @module Async
3
+ */
4
+ import { AsyncHooks, callErrorPolicyOnThrow, callErrorPolicyOnValue, callInvokable, isInvokable, optionNone, optionSome, resolveOneOrMore, } from "../../../utilities/_module-exports.js";
5
+ import {} from "../../../async/middlewares/hedging/hedging.types.js";
6
+ /**
7
+ * @internal
8
+ */
9
+ function resolveFunctions(primaryFn, fallbacks, middlewares, signalBinder) {
10
+ const resolvedFallbacks = resolveOneOrMore(fallbacks).map((fallback, index) => {
11
+ if (isInvokable(fallback)) {
12
+ return {
13
+ name: `fallback-${String(index + 1)}`,
14
+ invokable: fallback,
15
+ };
16
+ }
17
+ return fallback;
18
+ });
19
+ return [
20
+ {
21
+ name: "__primary",
22
+ invokable: primaryFn,
23
+ },
24
+ ...resolvedFallbacks,
25
+ ].map((namedFn) => ({
26
+ name: namedFn.name,
27
+ invokable: new AsyncHooks(namedFn.invokable, middlewares, {
28
+ signalBinder,
29
+ }),
30
+ }));
31
+ }
32
+ /**
33
+ * The `sequentialHedging` middleware executes the primary function and all fallback functions sequentially.
34
+ * It returns the result of the first successful function and automatically cancels all remaining functions.
35
+ * If all function fail than last error is thrown.
36
+ *
37
+ * IMPORT_PATH: `"@daiso-tech/core/async"`
38
+ * @group Middlewares
39
+ * @throws {HedgingAsyncError} {@link HedgingAsyncError}
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import { sequentialHedging } from "@daiso-tech/core/async";
44
+ * import { AsyncHooks } from "@daiso-tech/core/utilities";
45
+ *
46
+ * async function fn1(signal?: AbortSignal): Promise<unknown> {
47
+ * const response = await fetch("ENDPOINT-1", { signal });
48
+ * return await response.json();
49
+ * }
50
+ * async function fn2(signal?: AbortSignal): Promise<unknown> {
51
+ * const response = await fetch("ENDPOINT-2", { signal });
52
+ * return await response.json();
53
+ * }
54
+ * async function fn3(signal?: AbortSignal): Promise<unknown> {
55
+ * const response = await fetch("ENDPOINT-3", { signal });
56
+ * return await response.json();
57
+ * }
58
+ * const fetchData = new AsyncHooks(fn1, [
59
+ * sequentialHedging({
60
+ * fallbacks: [
61
+ * fn2,
62
+ * fn3
63
+ * ]
64
+ * })
65
+ * ], {
66
+ * signalBinder: {
67
+ * getSignal: (args) => args[0],
68
+ * forwardSignal: (args, signal) => {
69
+ * args[0] = signal;
70
+ * }
71
+ * }
72
+ * });
73
+ *
74
+ * console.log(await fetchData.invoke());
75
+ * ```
76
+ */
77
+ export function sequentialHedging(settings) {
78
+ const { middlewares = [], fallbacks, errorPolicy, onHedgingAttempt = () => { }, onHedgingError = () => { }, } = settings;
79
+ return async (args, next, { context, signal, signalBinder }) => {
80
+ const funcs = resolveFunctions(next, fallbacks, middlewares, signalBinder);
81
+ let lastError = optionNone();
82
+ for (const { name, invokable: func } of funcs) {
83
+ try {
84
+ if (signal.aborted) {
85
+ break;
86
+ }
87
+ callInvokable(onHedgingAttempt, {
88
+ args,
89
+ context,
90
+ name,
91
+ });
92
+ const value = await callInvokable(func, ...args);
93
+ // Handle hedging if an Result type is returned
94
+ if (!(await callErrorPolicyOnValue(errorPolicy, value))) {
95
+ return value;
96
+ }
97
+ // We can cast type here because callErrorPolicyOnValue ensures the value is a ResultFailure
98
+ const resultFailure = value;
99
+ callInvokable(onHedgingError, {
100
+ args,
101
+ context,
102
+ error: resultFailure.error,
103
+ name,
104
+ });
105
+ lastError = optionSome(resultFailure.error);
106
+ // Handle hedging if an error is thrown
107
+ }
108
+ catch (error) {
109
+ if (signal.aborted) {
110
+ break;
111
+ }
112
+ if (await callErrorPolicyOnThrow(errorPolicy, error)) {
113
+ lastError = optionSome(error);
114
+ }
115
+ else {
116
+ throw error;
117
+ }
118
+ callInvokable(onHedgingError, {
119
+ args,
120
+ context,
121
+ error,
122
+ name,
123
+ });
124
+ lastError = optionSome(error);
125
+ }
126
+ }
127
+ if (lastError.type === "none") {
128
+ throw new Error("!!__MESSAGE__!!");
129
+ }
130
+ throw lastError.value;
131
+ };
132
+ }
133
+ //# sourceMappingURL=sequential-hedging.middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sequential-hedging.middleware.js","sourceRoot":"","sources":["../../../../src/async/middlewares/hedging/sequential-hedging.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACH,UAAU,EACV,sBAAsB,EACtB,sBAAsB,EACtB,aAAa,EACb,WAAW,EACX,UAAU,EACV,UAAU,EACV,gBAAgB,GAQnB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAIN,MAAM,8CAA8C,CAAC;AAEtD;;GAEG;AACH,SAAS,gBAAgB,CACrB,SAAyC,EACzC,SAEC,EACD,WAA6D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,iBAAiB,CAAC,CAAC;QACvC,CAAC;QAED,MAAM,SAAS,CAAC,KAAK,CAAC;IAC1B,CAAC,CAAC;AACN,CAAC"}
@@ -1 +1,2 @@
1
1
  export * from "../../../async/middlewares/observe/observe.middleware.js";
2
+ export * from "../../../async/middlewares/observe/observe.types.js";