@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,51 +0,0 @@
1
- /**
2
- * @module Async
3
- */
4
- import { type AsyncMiddlewareFn, type HookContext } from "../../../../utilities/_module-exports.js";
5
- import type { HedgingSettings } from "../../../../async/middlewares/hedging/_shared.js";
6
- /**
7
- * The `concurrentHedging` middleware executes the primary function and all fallback functions concurrently.
8
- * It returns the result of the first successful function and automatically aborts all remaining functions.
9
- * If all function fail than error is thrown.
10
- *
11
- * IMPORT_PATH: `"@daiso-tech/core/async"`
12
- * @group Middlewares
13
- * @throws {HedgingAsyncError} {@link HedgingAsyncError}
14
- *
15
- * @example
16
- * ```ts
17
- * import { concurrentHedging } from "@daiso-tech/core/async";
18
- * import { AsyncHooks } from "@daiso-tech/core/utilities";
19
- *
20
- * async function fn1(signal?: AbortSignal): Promise<unknown> {
21
- * const response = await fetch("ENDPOINT-1", { signal });
22
- * return await response.json();
23
- * }
24
- * async function fn2(signal?: AbortSignal): Promise<unknown> {
25
- * const response = await fetch("ENDPOINT-2", { signal });
26
- * return await response.json();
27
- * }
28
- * async function fn3(signal?: AbortSignal): Promise<unknown> {
29
- * const response = await fetch("ENDPOINT-3", { signal });
30
- * return await response.json();
31
- * }
32
- * const fetchData = new AsyncHooks(fn1, [
33
- * concurrentHedging({
34
- * fallbacks: [
35
- * fn2,
36
- * fn3
37
- * ]
38
- * })
39
- * ], {
40
- * signalBinder: {
41
- * getSignal: (args) => args[0],
42
- * forwardSignal: (args, signal) => {
43
- * args[0] = signal;
44
- * }
45
- * }
46
- * });
47
- *
48
- * console.log(await fetchData.invoke());
49
- * ```
50
- */
51
- export declare function concurrentHedging<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings: NoInfer<HedgingSettings<TParameters, TReturn, TContext>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;
@@ -1,138 +0,0 @@
1
- /**
2
- * @module Async
3
- */
4
- import { callInvokable, isInvokable, resolveOneOrMore, TimeSpan, } from "../../../../utilities/_module-exports.js";
5
- import { HedgingAsyncError } from "../../../../async/async.errors.js";
6
- import { timeoutAndFail, abortAndFail } from "../../../../async/utilities/_module.js";
7
- /**
8
- * @internal
9
- */
10
- class ResolvedError extends Error {
11
- constructor(message, cause) {
12
- super(message, { cause });
13
- this.name = ResolvedError.name;
14
- }
15
- }
16
- /**
17
- * The `concurrentHedging` middleware executes the primary function and all fallback functions concurrently.
18
- * It returns the result of the first successful function and automatically aborts all remaining functions.
19
- * If all function fail than error is thrown.
20
- *
21
- * IMPORT_PATH: `"@daiso-tech/core/async"`
22
- * @group Middlewares
23
- * @throws {HedgingAsyncError} {@link HedgingAsyncError}
24
- *
25
- * @example
26
- * ```ts
27
- * import { concurrentHedging } from "@daiso-tech/core/async";
28
- * import { AsyncHooks } from "@daiso-tech/core/utilities";
29
- *
30
- * async function fn1(signal?: AbortSignal): Promise<unknown> {
31
- * const response = await fetch("ENDPOINT-1", { signal });
32
- * return await response.json();
33
- * }
34
- * async function fn2(signal?: AbortSignal): Promise<unknown> {
35
- * const response = await fetch("ENDPOINT-2", { signal });
36
- * return await response.json();
37
- * }
38
- * async function fn3(signal?: AbortSignal): Promise<unknown> {
39
- * const response = await fetch("ENDPOINT-3", { signal });
40
- * return await response.json();
41
- * }
42
- * const fetchData = new AsyncHooks(fn1, [
43
- * concurrentHedging({
44
- * fallbacks: [
45
- * fn2,
46
- * fn3
47
- * ]
48
- * })
49
- * ], {
50
- * signalBinder: {
51
- * getSignal: (args) => args[0],
52
- * forwardSignal: (args, signal) => {
53
- * args[0] = signal;
54
- * }
55
- * }
56
- * });
57
- *
58
- * console.log(await fetchData.invoke());
59
- * ```
60
- */
61
- export function concurrentHedging(settings) {
62
- const { waitTime = TimeSpan.fromSeconds(2), fallbacks, onHedgingAttempt = () => { }, onHedgingError = () => { }, } = settings;
63
- const resolvedFallbacks = resolveOneOrMore(fallbacks).map((fallback, index) => {
64
- if (isInvokable(fallback)) {
65
- return {
66
- name: `fallback-${String(index + 1)}`,
67
- invokable: fallback,
68
- };
69
- }
70
- return fallback;
71
- });
72
- return async (args, next, { context, abort, signal }) => {
73
- function step1({ name, invokable }) {
74
- return {
75
- name,
76
- promise: timeoutAndFail(abortAndFail((async () => callInvokable(invokable, ...args))(), signal), waitTime, (error) => {
77
- abort(error);
78
- }, signal),
79
- };
80
- }
81
- async function step2({ name, promise }) {
82
- try {
83
- const value = await promise;
84
- // We abort all other promises when on promise succeds.
85
- abort(new ResolvedError("Already resolved"));
86
- return {
87
- type: "success",
88
- value,
89
- name,
90
- };
91
- }
92
- catch (error) {
93
- return {
94
- type: "failure",
95
- error,
96
- name,
97
- };
98
- }
99
- }
100
- const funcs = [
101
- {
102
- name: "__initial",
103
- invokable: next,
104
- },
105
- ...resolvedFallbacks,
106
- ];
107
- const promises = funcs.map(step1).map(step2);
108
- const errors = [];
109
- const promiseResults = await Promise.all(promises);
110
- for (const promiseResult of promiseResults) {
111
- callInvokable(onHedgingAttempt, {
112
- args,
113
- context,
114
- name: promiseResult.name,
115
- });
116
- // We return the first fulfilled value
117
- if (promiseResult.type === "success") {
118
- return promiseResult.value;
119
- }
120
- else if (!(promiseResult.error instanceof ResolvedError)) {
121
- callInvokable(onHedgingError, {
122
- args,
123
- context,
124
- error: promiseResult.error,
125
- name: promiseResult.name,
126
- });
127
- errors.push(promiseResult.error);
128
- }
129
- }
130
- // If all promiseResults are rejected we will throw an error
131
- const funcNames = funcs
132
- .slice(1)
133
- .map(({ name }) => `"${name}"`)
134
- .join(", ");
135
- throw new HedgingAsyncError(`The original function and fallback functions failed: ${funcNames}`, errors);
136
- };
137
- }
138
- //# sourceMappingURL=concurrent-hedging.middleware.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"concurrent-hedging.middleware.js","sourceRoot":"","sources":["../../../../../src/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACH,aAAa,EACb,WAAW,EACX,gBAAgB,EAChB,QAAQ,GAGX,MAAM,gCAAgC,CAAC;AAMxC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,8BAA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EAAE,aAAa,CAAC,KAAK;oBAC1B,IAAI,EAAE,aAAa,CAAC,IAAI;iBAC3B,CAAC,CAAC;gBACH,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YACrC,CAAC;QACL,CAAC;QAED,4DAA4D;QAC5D,MAAM,SAAS,GAAG,KAAK;aAClB,KAAK,CAAC,CAAC,CAAC;aACR,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,IAAI,GAAG,CAAC;aAC9B,IAAI,CAAC,IAAI,CAAC,CAAC;QAChB,MAAM,IAAI,iBAAiB,CACvB,wDAAwD,SAAS,EAAE,EACnE,MAAM,CACT,CAAC;IACN,CAAC,CAAC;AACN,CAAC"}
@@ -1 +0,0 @@
1
- export * from "../../../../async/middlewares/hedging/sequential-hedging/_module.js";
@@ -1,2 +0,0 @@
1
- export * from "../../../../async/middlewares/hedging/sequential-hedging/_module.js";
2
- //# sourceMappingURL=_module.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../../../src/async/middlewares/hedging/sequential-hedging/_module.ts"],"names":[],"mappings":"AAAA,cAAc,2DAA2D,CAAC"}
@@ -1,104 +0,0 @@
1
- /**
2
- * @module Async
3
- */
4
- import { callInvokable, isInvokable, resolveOneOrMore, TimeSpan, } from "../../../../utilities/_module-exports.js";
5
- import { HedgingAsyncError } from "../../../../async/async.errors.js";
6
- import { timeoutAndFail } from "../../../../async/utilities/_module.js";
7
- /**
8
- * The `sequentialHedging` middleware executes the primary function and all fallback functions sequentially.
9
- * It returns the result of the first successful function and automatically cancels all remaining functions.
10
- * If all function fail than error is thrown.
11
- *
12
- * IMPORT_PATH: `"@daiso-tech/core/async"`
13
- * @group Middlewares
14
- * @throws {HedgingAsyncError} {@link HedgingAsyncError}
15
- *
16
- * @example
17
- * ```ts
18
- * import { sequentialHedging } from "@daiso-tech/core/async";
19
- * import { AsyncHooks } from "@daiso-tech/core/utilities";
20
- *
21
- * async function fn1(signal?: AbortSignal): Promise<unknown> {
22
- * const response = await fetch("ENDPOINT-1", { signal });
23
- * return await response.json();
24
- * }
25
- * async function fn2(signal?: AbortSignal): Promise<unknown> {
26
- * const response = await fetch("ENDPOINT-2", { signal });
27
- * return await response.json();
28
- * }
29
- * async function fn3(signal?: AbortSignal): Promise<unknown> {
30
- * const response = await fetch("ENDPOINT-3", { signal });
31
- * return await response.json();
32
- * }
33
- * const fetchData = new AsyncHooks(fn1, [
34
- * sequentialHedging({
35
- * fallbacks: [
36
- * fn2,
37
- * fn3
38
- * ]
39
- * })
40
- * ], {
41
- * signalBinder: {
42
- * getSignal: (args) => args[0],
43
- * forwardSignal: (args, signal) => {
44
- * args[0] = signal;
45
- * }
46
- * }
47
- * });
48
- *
49
- * console.log(await fetchData.invoke());
50
- * ```
51
- */
52
- export function sequentialHedging(settings) {
53
- const { waitTime = TimeSpan.fromSeconds(2), fallbacks, onHedgingAttempt = () => { }, onHedgingError = () => { }, } = settings;
54
- const resolvedFallbacks = resolveOneOrMore(fallbacks).map((fallback, index) => {
55
- if (isInvokable(fallback)) {
56
- return {
57
- name: `fallback-${String(index + 1)}`,
58
- invokable: fallback,
59
- };
60
- }
61
- return fallback;
62
- });
63
- return async (args, next, { context, signal, abort }) => {
64
- const errors = [];
65
- const funcs = [
66
- {
67
- name: "__initial",
68
- invokable: next,
69
- },
70
- ...resolvedFallbacks,
71
- ];
72
- for (const { name, invokable: func } of funcs) {
73
- try {
74
- callInvokable(onHedgingAttempt, {
75
- args,
76
- context,
77
- name,
78
- });
79
- return await timeoutAndFail((async () => callInvokable(func, ...args))(), waitTime, (error) => {
80
- abort(error);
81
- }, signal);
82
- }
83
- catch (error) {
84
- if (signal.aborted) {
85
- break;
86
- }
87
- callInvokable(onHedgingError, {
88
- args,
89
- context,
90
- error,
91
- name,
92
- });
93
- errors.push(error);
94
- }
95
- }
96
- // If all promiseResults are rejected we will throw an error
97
- const funcNames = funcs
98
- .slice(1)
99
- .map(({ name }) => `"${name}"`)
100
- .join(", ");
101
- throw new HedgingAsyncError(`The original function and fallback functions failed: ${funcNames}`, errors);
102
- };
103
- }
104
- //# sourceMappingURL=sequential-hedging.middleware.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sequential-hedging.middleware.js","sourceRoot":"","sources":["../../../../../src/async/middlewares/hedging/sequential-hedging/sequential-hedging.middleware.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACH,aAAa,EACb,WAAW,EACX,gBAAgB,EAChB,QAAQ,GAGX,MAAM,gCAAgC,CAAC;AAKxC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,UAAU,iBAAiB,CAK7B,QAAkE;IAElE,MAAM,EACF,QAAQ,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,EAClC,SAAS,EACT,gBAAgB,GAAG,GAAG,EAAE,GAAE,CAAC,EAC3B,cAAc,GAAG,GAAG,EAAE,GAAE,CAAC,GAC5B,GAAG,QAAQ,CAAC;IAEb,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAC,GAAG,CAEvD,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE;QAClB,IAAI,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC;YACxB,OAAO;gBACH,IAAI,EAAE,YAAY,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE;gBACrC,SAAS,EAAE,QAAQ;aACtB,CAAC;QACN,CAAC;QACD,OAAO,QAAQ,CAAC;IACpB,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE;QACpD,MAAM,MAAM,GAAc,EAAE,CAAC;QAC7B,MAAM,KAAK,GAAG;YACV;gBACI,IAAI,EAAE,WAAW;gBACjB,SAAS,EAAE,IAAI;aAClB;YACD,GAAG,iBAAiB;SACvB,CAAC;QACF,KAAK,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,KAAK,EAAE,CAAC;YAC5C,IAAI,CAAC;gBACD,aAAa,CAAC,gBAAgB,EAAE;oBAC5B,IAAI;oBACJ,OAAO;oBACP,IAAI;iBACP,CAAC,CAAC;gBACH,OAAO,MAAM,cAAc,CACvB,CAAC,KAAK,IAAI,EAAE,CAAC,aAAa,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE,EAC5C,QAAQ,EACR,CAAC,KAAc,EAAE,EAAE;oBACf,KAAK,CAAC,KAAK,CAAC,CAAC;gBACjB,CAAC,EACD,MAAM,CACT,CAAC;YACN,CAAC;YAAC,OAAO,KAAc,EAAE,CAAC;gBACtB,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;oBACjB,MAAM;gBACV,CAAC;gBACD,aAAa,CAAC,cAAc,EAAE;oBAC1B,IAAI;oBACJ,OAAO;oBACP,KAAK;oBACL,IAAI;iBACP,CAAC,CAAC;gBACH,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;QACL,CAAC;QAED,4DAA4D;QAC5D,MAAM,SAAS,GAAG,KAAK;aAClB,KAAK,CAAC,CAAC,CAAC;aACR,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,IAAI,GAAG,CAAC;aAC9B,IAAI,CAAC,IAAI,CAAC,CAAC;QAChB,MAAM,IAAI,iBAAiB,CACvB,wDAAwD,SAAS,EAAE,EACnE,MAAM,CACT,CAAC;IACN,CAAC,CAAC;AACN,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"_module-exports.js","sourceRoot":"","sources":["../../../src/utilities/functions/_module-exports.ts"],"names":[],"mappings":"AAAA,cAAc,+CAA+C,CAAC;AAC9D,cAAc,oCAAoC,CAAC;AACnD,cAAc,sCAAsC,CAAC;AACrD,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,8CAA8C,CAAC;AAC7D,cAAc,iCAAiC,CAAC;AAChD,cAAc,mCAAmC,CAAC"}
@@ -1,8 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- /**
5
- *
6
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
7
- */
8
- export type AtLeastOne<TItem> = [TItem, ...TItem[]];
@@ -1,5 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- export {};
5
- //# sourceMappingURL=at-least-one.type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"at-least-one.type.js","sourceRoot":"","sources":["../../../src/utilities/types/at-least-one.type.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -1,44 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- import type { NoneFunc } from "../../utilities/types/none-func.type.js";
5
- import type { Promisable } from "../../utilities/types/promiseable.type.js";
6
- import type { IInvokableObject, InvokableFn } from "../../utilities/types/invokable.type.js";
7
- /**
8
- *
9
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
10
- */
11
- export type FactoryFn<TInput, TOutput> = InvokableFn<[
12
- value: TInput
13
- ], NoneFunc<TOutput>>;
14
- /**
15
- *
16
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
17
- */
18
- export type IFactoryObject<TInput, TOutput> = IInvokableObject<[
19
- value: TInput
20
- ], NoneFunc<TOutput>>;
21
- /**
22
- *
23
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
24
- */
25
- export type Factory<TInput, TOutput> = FactoryFn<TInput, TOutput> | IFactoryObject<TInput, TOutput>;
26
- /**
27
- *
28
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
29
- */
30
- export type AsyncFactoryFn<TInput, TOutput> = InvokableFn<[
31
- value: TInput
32
- ], Promisable<NoneFunc<TOutput>>>;
33
- /**
34
- *
35
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
36
- */
37
- export type IAsyncFactoryObject<TInput, TOutput> = IInvokableObject<[
38
- value: TInput
39
- ], Promisable<NoneFunc<TOutput>>>;
40
- /**
41
- *
42
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
43
- */
44
- export type AsyncFactory<TInput, TOutput> = AsyncFactoryFn<TInput, TOutput> | IAsyncFactoryObject<TInput, TOutput>;
@@ -1,5 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- export {};
5
- //# sourceMappingURL=factory.type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"factory.type.js","sourceRoot":"","sources":["../../../src/utilities/types/factory.type.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -1,20 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- /**
5
- *
6
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
7
- */
8
- export type InvokableFn<TArgs extends unknown[] = unknown[], TReturn = unknown> = (...args: TArgs) => TReturn;
9
- /**
10
- *
11
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
12
- */
13
- export type IInvokableObject<TArgs extends unknown[] = unknown[], TReturn = unknown> = {
14
- invoke(...args: TArgs): TReturn;
15
- };
16
- /**
17
- *
18
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
19
- */
20
- export type Invokable<TArgs extends unknown[] = unknown[], TReturn = unknown> = InvokableFn<TArgs, TReturn> | IInvokableObject<TArgs, TReturn>;
@@ -1,5 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- export {};
5
- //# sourceMappingURL=invokable.type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"invokable.type.js","sourceRoot":"","sources":["../../../src/utilities/types/invokable.type.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -1,8 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- /**
5
- *
6
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
7
- */
8
- export type Items<TArray extends any[]> = TArray[number];
@@ -1,5 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- export {};
5
- //# sourceMappingURL=items.type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"items.type.js","sourceRoot":"","sources":["../../../src/utilities/types/items.type.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -1,31 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- import type { LazyPromise } from "../../async/_module-exports.js";
5
- import type { Promisable } from "../../utilities/types/promiseable.type.js";
6
- import type { Invokable } from "../../utilities/types/invokable.type.js";
7
- /**
8
- *
9
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
10
- */
11
- export type Lazy<TValue> = Invokable<[], TValue>;
12
- /**
13
- *
14
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
15
- */
16
- export type Lazyable<TValue> = TValue | Lazy<TValue>;
17
- /**
18
- *
19
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
20
- */
21
- export type AsyncLazy_<TValue> = Invokable<[], Promisable<TValue>>;
22
- /**
23
- *
24
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
25
- */
26
- export type AsyncLazy<TValue> = AsyncLazy_<TValue> | LazyPromise<TValue>;
27
- /**
28
- *
29
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
30
- */
31
- export type AsyncLazyable<TValue> = TValue | AsyncLazy<TValue>;
@@ -1,5 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- export {};
5
- //# sourceMappingURL=lazy.type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"lazy.type.js","sourceRoot":"","sources":["../../../src/utilities/types/lazy.type.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -1,8 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- /**
5
- *
6
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
7
- */
8
- export type OneOrMore<TItem> = TItem | Iterable<TItem>;
@@ -1,5 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- export {};
5
- //# sourceMappingURL=one-or-more.type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"one-or-more.type.js","sourceRoot":"","sources":["../../../src/utilities/types/one-or-more.type.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -1,18 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- /**
5
- *
6
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
7
- */
8
- export type ResultFailure<TError = unknown> = [value: null, errror: TError];
9
- /**
10
- *
11
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
12
- */
13
- export type ResultSuccess<TValue> = [value: TValue, error: null];
14
- /**
15
- *
16
- * IMPORT_PATH: `"@daiso-tech/core/utilities"`
17
- */
18
- export type Result<TValue, TError = unknown> = ResultFailure<TError> | ResultSuccess<TValue>;
@@ -1,5 +0,0 @@
1
- /**
2
- * @module Utilities
3
- */
4
- export {};
5
- //# sourceMappingURL=result.type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"result.type.js","sourceRoot":"","sources":["../../../src/utilities/types/result.type.ts"],"names":[],"mappings":"AAAA;;GAEG"}