@daiso-tech/core 0.37.3 → 0.39.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 (249) hide show
  1. package/README.md +5 -1
  2. package/dist/async/_module-exports.d.ts +1 -1
  3. package/dist/async/_module-exports.js +1 -1
  4. package/dist/async/_module-exports.js.map +1 -1
  5. package/dist/async/async.errors.d.ts +0 -20
  6. package/dist/async/async.errors.js +0 -16
  7. package/dist/async/async.errors.js.map +1 -1
  8. package/dist/async/backof-policies/constant-backoff-policy/constant-backoff-policy.d.ts +1 -1
  9. package/dist/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.d.ts +2 -2
  10. package/dist/async/backof-policies/linear-backoff-policy/linear-backoff-policy.d.ts +1 -1
  11. package/dist/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.d.ts +2 -2
  12. package/dist/async/middlewares/_module.d.ts +0 -1
  13. package/dist/async/middlewares/_module.js +0 -1
  14. package/dist/async/middlewares/_module.js.map +1 -1
  15. package/dist/async/middlewares/bulkhead/bulkhead.middleware.d.ts +2 -2
  16. package/dist/async/middlewares/bulkhead/bulkhead.middleware.js +1 -1
  17. package/dist/async/middlewares/bulkhead/bulkhead.middleware.js.map +1 -1
  18. package/dist/async/middlewares/fallback/_module.d.ts +1 -0
  19. package/dist/async/middlewares/fallback/_module.js +1 -0
  20. package/dist/async/middlewares/fallback/_module.js.map +1 -1
  21. package/dist/async/middlewares/fallback/fallback.middleware.d.ts +25 -49
  22. package/dist/async/middlewares/fallback/fallback.middleware.js +52 -14
  23. package/dist/async/middlewares/fallback/fallback.middleware.js.map +1 -1
  24. package/dist/async/middlewares/fallback/fallback.types.d.ts +42 -0
  25. package/dist/async/middlewares/fallback/fallback.types.js +6 -0
  26. package/dist/async/middlewares/fallback/fallback.types.js.map +1 -0
  27. package/dist/async/middlewares/hedging/_module.d.ts +2 -2
  28. package/dist/async/middlewares/hedging/_module.js +2 -2
  29. package/dist/async/middlewares/hedging/_module.js.map +1 -1
  30. package/dist/async/middlewares/hedging/{_shared.d.ts → hedging.types.d.ts} +37 -9
  31. package/dist/async/middlewares/hedging/hedging.types.js +6 -0
  32. package/dist/async/middlewares/hedging/hedging.types.js.map +1 -0
  33. package/dist/async/middlewares/hedging/{sequential-hedging/sequential-hedging.middleware.d.ts → sequential-hedging.middleware.d.ts} +3 -3
  34. package/dist/async/middlewares/hedging/sequential-hedging.middleware.js +133 -0
  35. package/dist/async/middlewares/hedging/sequential-hedging.middleware.js.map +1 -0
  36. package/dist/async/middlewares/observe/_module.d.ts +1 -0
  37. package/dist/async/middlewares/observe/_module.js +1 -0
  38. package/dist/async/middlewares/observe/_module.js.map +1 -1
  39. package/dist/async/middlewares/observe/observe.middleware.d.ts +37 -87
  40. package/dist/async/middlewares/observe/observe.middleware.js +53 -2
  41. package/dist/async/middlewares/observe/observe.middleware.js.map +1 -1
  42. package/dist/async/middlewares/observe/observe.types.d.ts +91 -0
  43. package/dist/async/middlewares/observe/observe.types.js +6 -0
  44. package/dist/async/middlewares/observe/observe.types.js.map +1 -0
  45. package/dist/async/middlewares/retry/_module.d.ts +1 -0
  46. package/dist/async/middlewares/retry/_module.js +1 -0
  47. package/dist/async/middlewares/retry/_module.js.map +1 -1
  48. package/dist/async/middlewares/retry/retry.middleware.d.ts +36 -86
  49. package/dist/async/middlewares/retry/retry.middleware.js +70 -22
  50. package/dist/async/middlewares/retry/retry.middleware.js.map +1 -1
  51. package/dist/async/middlewares/retry/retry.types.d.ts +77 -0
  52. package/dist/async/middlewares/retry/retry.types.js +7 -0
  53. package/dist/async/middlewares/retry/retry.types.js.map +1 -0
  54. package/dist/async/middlewares/timeout/_module.d.ts +1 -0
  55. package/dist/async/middlewares/timeout/_module.js +1 -0
  56. package/dist/async/middlewares/timeout/_module.js.map +1 -1
  57. package/dist/async/middlewares/timeout/timeout.middleware.d.ts +1 -47
  58. package/dist/async/middlewares/timeout/timeout.middleware.js +1 -1
  59. package/dist/async/middlewares/timeout/timeout.middleware.js.map +1 -1
  60. package/dist/async/middlewares/timeout/timeout.type.d.ts +51 -0
  61. package/dist/async/middlewares/timeout/timeout.type.js +6 -0
  62. package/dist/async/middlewares/timeout/timeout.type.js.map +1 -0
  63. package/dist/async/utilities/promise-queue/promise-queue.js +19 -8
  64. package/dist/async/utilities/promise-queue/promise-queue.js.map +1 -1
  65. package/dist/cache/contracts/cache.contract.d.ts +7 -7
  66. package/dist/cache/contracts/cache.errors.d.ts +3 -26
  67. package/dist/cache/contracts/cache.errors.js +0 -47
  68. package/dist/cache/contracts/cache.errors.js.map +1 -1
  69. package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.d.ts +6 -5
  70. package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js +17 -13
  71. package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js.map +1 -1
  72. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.d.ts +15 -3
  73. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js +25 -9
  74. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js.map +1 -1
  75. package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.d.ts +1 -1
  76. package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js +1 -1
  77. package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js +2 -1
  78. package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js.map +1 -1
  79. package/dist/cache/implementations/derivables/cache/cache.d.ts +2 -2
  80. package/dist/cache/implementations/derivables/cache/cache.js +1 -1
  81. package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
  82. package/dist/collection/contracts/async-collection.contract.d.ts +0 -1
  83. package/dist/collection/contracts/async-collection.contract.js +3 -1
  84. package/dist/collection/contracts/async-collection.contract.js.map +1 -1
  85. package/dist/collection/contracts/collection.contract.d.ts +0 -1
  86. package/dist/collection/contracts/collection.contract.js +3 -1
  87. package/dist/collection/contracts/collection.contract.js.map +1 -1
  88. package/dist/collection/contracts/collection.errors.d.ts +1 -26
  89. package/dist/collection/contracts/collection.errors.js +0 -55
  90. package/dist/collection/contracts/collection.errors.js.map +1 -1
  91. package/dist/collection/implementations/async-iterable-collection/_shared/async-split-iterable.js +1 -1
  92. package/dist/collection/implementations/async-iterable-collection/_shared/async-split-iterable.js.map +1 -1
  93. package/dist/collection/implementations/async-iterable-collection/_shared/async-zip-iterable.js +1 -1
  94. package/dist/collection/implementations/async-iterable-collection/_shared/async-zip-iterable.js.map +1 -1
  95. package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js +9 -6
  96. package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
  97. package/dist/collection/implementations/iterable-collection/_shared/split-iterable.js +1 -1
  98. package/dist/collection/implementations/iterable-collection/_shared/split-iterable.js.map +1 -1
  99. package/dist/collection/implementations/iterable-collection/_shared/zip-iterable.js +1 -1
  100. package/dist/collection/implementations/iterable-collection/_shared/zip-iterable.js.map +1 -1
  101. package/dist/collection/implementations/iterable-collection/iterable-collection.js +9 -6
  102. package/dist/collection/implementations/iterable-collection/iterable-collection.js.map +1 -1
  103. package/dist/collection/implementations/list-collection/list-collection.js +12 -9
  104. package/dist/collection/implementations/list-collection/list-collection.js.map +1 -1
  105. package/dist/event-bus/contracts/_module-exports.d.ts +0 -1
  106. package/dist/event-bus/contracts/_module-exports.js +0 -1
  107. package/dist/event-bus/contracts/_module-exports.js.map +1 -1
  108. package/dist/event-bus/contracts/event-bus.contract.d.ts +0 -6
  109. package/dist/event-bus/contracts/event-bus.contract.js +1 -7
  110. package/dist/event-bus/contracts/event-bus.contract.js.map +1 -1
  111. package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.d.ts +2 -2
  112. package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js +3 -2
  113. package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js.map +1 -1
  114. package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +1 -1
  115. package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +6 -11
  116. package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
  117. package/dist/lock/contracts/database-lock-adapter.contract.d.ts +7 -5
  118. package/dist/lock/contracts/lock-adapter.contract.d.ts +5 -3
  119. package/dist/lock/contracts/lock.contract.d.ts +25 -41
  120. package/dist/lock/contracts/lock.errors.d.ts +5 -56
  121. package/dist/lock/contracts/lock.errors.js +1 -123
  122. package/dist/lock/contracts/lock.errors.js.map +1 -1
  123. package/dist/lock/contracts/lock.events.d.ts +3 -3
  124. package/dist/lock/contracts/lock.events.js +1 -1
  125. package/dist/lock/contracts/lock.events.js.map +1 -1
  126. package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.d.ts +16 -4
  127. package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js +11 -8
  128. package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
  129. package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.d.ts +17 -6
  130. package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +9 -16
  131. package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
  132. package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.d.ts +1 -1
  133. package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +3 -3
  134. package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
  135. package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.d.ts +1 -1
  136. package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js +3 -2
  137. package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js.map +1 -1
  138. package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +3 -3
  139. package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +2 -3
  140. package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
  141. package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js +4 -3
  142. package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js.map +1 -1
  143. package/dist/lock/implementations/derivables/lock-provider/lock-state.d.ts +8 -11
  144. package/dist/lock/implementations/derivables/lock-provider/lock-state.js +9 -26
  145. package/dist/lock/implementations/derivables/lock-provider/lock-state.js.map +1 -1
  146. package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +11 -14
  147. package/dist/lock/implementations/derivables/lock-provider/lock.js +50 -26
  148. package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
  149. package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +2 -2
  150. package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
  151. package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +43 -47
  152. package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
  153. package/dist/serde/implementations/derivables/serde.d.ts +22 -22
  154. package/dist/utilities/_module-exports.d.ts +1 -1
  155. package/dist/utilities/_module-exports.js +1 -1
  156. package/dist/utilities/_module-exports.js.map +1 -1
  157. package/dist/utilities/classes/hooks/async-hooks.d.ts +6 -4
  158. package/dist/utilities/classes/hooks/async-hooks.js +3 -1
  159. package/dist/utilities/classes/hooks/async-hooks.js.map +1 -1
  160. package/dist/utilities/classes/hooks/hooks.d.ts +1 -1
  161. package/dist/utilities/classes/hooks/hooks.js +1 -1
  162. package/dist/utilities/classes/hooks/hooks.js.map +1 -1
  163. package/dist/utilities/classes/kysely-table-name-transformer-plugin/kysely-table-name-transformer-plugin.js +2 -1
  164. package/dist/utilities/classes/kysely-table-name-transformer-plugin/kysely-table-name-transformer-plugin.js.map +1 -1
  165. package/dist/utilities/classes/namespace/namespace.d.ts +5 -5
  166. package/dist/utilities/classes/namespace/namespace.js +2 -2
  167. package/dist/utilities/classes/namespace/namespace.js.map +1 -1
  168. package/dist/utilities/classes/time-span/time-span.d.ts +10 -2
  169. package/dist/utilities/classes/time-span/time-span.js +10 -2
  170. package/dist/utilities/classes/time-span/time-span.js.map +1 -1
  171. package/dist/utilities/errors.d.ts +8 -0
  172. package/dist/utilities/errors.js +11 -0
  173. package/dist/utilities/errors.js.map +1 -1
  174. package/dist/utilities/functions/{_module-exports.d.ts → _module.d.ts} +6 -1
  175. package/dist/utilities/functions/{_module-exports.js → _module.js} +7 -2
  176. package/dist/utilities/functions/_module.js.map +1 -0
  177. package/dist/utilities/functions/error-policy.d.ts +54 -0
  178. package/dist/utilities/functions/error-policy.js +63 -0
  179. package/dist/utilities/functions/error-policy.js.map +1 -0
  180. package/dist/utilities/functions/invokable.d.ts +57 -1
  181. package/dist/utilities/functions/invokable.js +3 -1
  182. package/dist/utilities/functions/invokable.js.map +1 -1
  183. package/dist/utilities/functions/is-class.d.ts +9 -0
  184. package/dist/utilities/functions/is-class.js +15 -0
  185. package/dist/utilities/functions/is-class.js.map +1 -0
  186. package/dist/utilities/functions/is-positive-nbr.d.ts +8 -0
  187. package/dist/utilities/functions/is-positive-nbr.js +19 -0
  188. package/dist/utilities/functions/is-positive-nbr.js.map +1 -0
  189. package/dist/utilities/functions/is-standard-schema.d.ts +2 -0
  190. package/dist/utilities/functions/is-standard-schema.js +5 -0
  191. package/dist/utilities/functions/is-standard-schema.js.map +1 -0
  192. package/dist/utilities/functions/lazy.d.ts +27 -1
  193. package/dist/utilities/functions/lazy.js.map +1 -1
  194. package/dist/utilities/functions/option.d.ts +32 -0
  195. package/dist/utilities/functions/option.js +25 -0
  196. package/dist/utilities/functions/option.js.map +1 -0
  197. package/dist/utilities/functions/resolve-one-or-more.d.ts +5 -1
  198. package/dist/utilities/functions/resolve-one-or-more.js.map +1 -1
  199. package/dist/utilities/functions/result.d.ts +81 -1
  200. package/dist/utilities/functions/result.js +46 -2
  201. package/dist/utilities/functions/result.js.map +1 -1
  202. package/dist/utilities/types/_module.d.ts +1 -7
  203. package/dist/utilities/types/_module.js +1 -7
  204. package/dist/utilities/types/_module.js.map +1 -1
  205. package/dist/utilities/types/any-class.type.d.ts +3 -0
  206. package/dist/utilities/types/any-class.type.js +2 -0
  207. package/dist/utilities/types/any-class.type.js.map +1 -0
  208. package/package.json +1 -1
  209. package/dist/async/middlewares/_shared.d.ts +0 -15
  210. package/dist/async/middlewares/_shared.js +0 -15
  211. package/dist/async/middlewares/_shared.js.map +0 -1
  212. package/dist/async/middlewares/hedging/_shared.js +0 -5
  213. package/dist/async/middlewares/hedging/_shared.js.map +0 -1
  214. package/dist/async/middlewares/hedging/concurrent-hedging/_module.d.ts +0 -1
  215. package/dist/async/middlewares/hedging/concurrent-hedging/_module.js +0 -2
  216. package/dist/async/middlewares/hedging/concurrent-hedging/_module.js.map +0 -1
  217. package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.d.ts +0 -51
  218. package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.js +0 -138
  219. package/dist/async/middlewares/hedging/concurrent-hedging/concurrent-hedging.middleware.js.map +0 -1
  220. package/dist/async/middlewares/hedging/sequential-hedging/_module.d.ts +0 -1
  221. package/dist/async/middlewares/hedging/sequential-hedging/_module.js +0 -2
  222. package/dist/async/middlewares/hedging/sequential-hedging/_module.js.map +0 -1
  223. package/dist/async/middlewares/hedging/sequential-hedging/sequential-hedging.middleware.js +0 -104
  224. package/dist/async/middlewares/hedging/sequential-hedging/sequential-hedging.middleware.js.map +0 -1
  225. package/dist/event-bus/contracts/event-bus.errors.d.ts +0 -72
  226. package/dist/event-bus/contracts/event-bus.errors.js +0 -110
  227. package/dist/event-bus/contracts/event-bus.errors.js.map +0 -1
  228. package/dist/utilities/functions/_module-exports.js.map +0 -1
  229. package/dist/utilities/types/at-least-one.type.d.ts +0 -8
  230. package/dist/utilities/types/at-least-one.type.js +0 -5
  231. package/dist/utilities/types/at-least-one.type.js.map +0 -1
  232. package/dist/utilities/types/factory.type.d.ts +0 -44
  233. package/dist/utilities/types/factory.type.js +0 -5
  234. package/dist/utilities/types/factory.type.js.map +0 -1
  235. package/dist/utilities/types/invokable.type.d.ts +0 -20
  236. package/dist/utilities/types/invokable.type.js +0 -5
  237. package/dist/utilities/types/invokable.type.js.map +0 -1
  238. package/dist/utilities/types/items.type.d.ts +0 -8
  239. package/dist/utilities/types/items.type.js +0 -5
  240. package/dist/utilities/types/items.type.js.map +0 -1
  241. package/dist/utilities/types/lazy.type.d.ts +0 -31
  242. package/dist/utilities/types/lazy.type.js +0 -5
  243. package/dist/utilities/types/lazy.type.js.map +0 -1
  244. package/dist/utilities/types/one-or-more.type.d.ts +0 -8
  245. package/dist/utilities/types/one-or-more.type.js +0 -5
  246. package/dist/utilities/types/one-or-more.type.js.map +0 -1
  247. package/dist/utilities/types/result.type.d.ts +0 -18
  248. package/dist/utilities/types/result.type.js +0 -5
  249. package/dist/utilities/types/result.type.js.map +0 -1
@@ -1,4 +1,84 @@
1
- import type { ResultFailure, ResultSuccess } from "../../utilities/types/_module.js";
1
+ /**
2
+ * @module Utilities
3
+ */
4
+ export declare const RESULT: {
5
+ readonly FAILURE: "failure";
6
+ readonly SUCCESS: "success";
7
+ };
8
+ /**
9
+ *
10
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
11
+ */
12
+ export type ResultFailure<TError = unknown> = {
13
+ type: (typeof RESULT)["FAILURE"];
14
+ error: TError;
15
+ };
16
+ /**
17
+ *
18
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
19
+ */
20
+ export type ResultSuccess<TValue = unknown> = {
21
+ type: (typeof RESULT)["SUCCESS"];
22
+ value: TValue;
23
+ };
24
+ /**
25
+ * The `Result` type represents either success or failure.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { Result, resultFailure, resultSuccess, RESULT } from "@daiso-tech/core/utilities";
30
+ *
31
+ * function random(): Result<string, Error> {
32
+ * const nbr = Math.round(Math.random() * 100);
33
+ * if (nbr > 50) {
34
+ * // The resultFailure function return a failed result
35
+ * return resultFailure(new Error("Unexpected error occured"));
36
+ * }
37
+ * // The resultSuccess function return a success result
38
+ * return resultSuccess("Function succeded");
39
+ * }
40
+ *
41
+ * const result = random();
42
+ *
43
+ * // Checking for failure
44
+ * if (result.type === RESULT.FAILURE) {
45
+ * console.log("Error occured:", result.error);
46
+ * }
47
+ *
48
+ * // Checking for success
49
+ * if (result.type === RESULT.SUCCESS) {
50
+ * console.log("Result was successful:", result.value);
51
+ * }
52
+ * ```
53
+ *
54
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
55
+ */
56
+ export type Result<TValue = unknown, TError = unknown> = ResultFailure<TError> | ResultSuccess<TValue>;
57
+ /**
58
+ *
59
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
60
+ */
61
+ export type InferResultError<TValue> = TValue extends Result ? Extract<TValue, ResultFailure>["error"] : unknown;
62
+ /**
63
+ *
64
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
65
+ */
66
+ export type InferResultSuccess<TValue> = TValue extends Result ? Extract<TValue, ResultSuccess>["value"] : TValue;
67
+ /**
68
+ *
69
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
70
+ */
71
+ export declare function isResultFailure<TError = unknown>(value: unknown): value is ResultFailure<TError>;
72
+ /**
73
+ *
74
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
75
+ */
76
+ export declare function isResultSuccess<TSuccess = unknown>(value: unknown): value is ResultSuccess<TSuccess>;
77
+ /**
78
+ *
79
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
80
+ */
81
+ export declare function isResult<TValue = unknown, TError = unknown>(value: unknown): value is Result<TValue, TError>;
2
82
  /**
3
83
  *
4
84
  * IMPORT_PATH: `"@daiso-tech/core/utilities"`
@@ -1,15 +1,59 @@
1
+ /**
2
+ * @module Utilities
3
+ */
4
+ export const RESULT = {
5
+ FAILURE: "failure",
6
+ SUCCESS: "success",
7
+ };
8
+ /**
9
+ *
10
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
11
+ */
12
+ export function isResultFailure(value) {
13
+ return (typeof value === "object" &&
14
+ value !== null &&
15
+ "type" in value &&
16
+ typeof value.type === "string" &&
17
+ value.type === RESULT.FAILURE &&
18
+ "error" in value);
19
+ }
20
+ /**
21
+ *
22
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
23
+ */
24
+ export function isResultSuccess(value) {
25
+ return (typeof value === "object" &&
26
+ value !== null &&
27
+ "type" in value &&
28
+ typeof value.type === "string" &&
29
+ value.type === RESULT.SUCCESS &&
30
+ "value" in value);
31
+ }
32
+ /**
33
+ *
34
+ * IMPORT_PATH: `"@daiso-tech/core/utilities"`
35
+ */
36
+ export function isResult(value) {
37
+ return isResultFailure(value) || isResultSuccess(value);
38
+ }
1
39
  /**
2
40
  *
3
41
  * IMPORT_PATH: `"@daiso-tech/core/utilities"`
4
42
  */
5
43
  export function resultFailure(error) {
6
- return [null, error];
44
+ return {
45
+ type: RESULT.FAILURE,
46
+ error,
47
+ };
7
48
  }
8
49
  /**
9
50
  *
10
51
  * IMPORT_PATH: `"@daiso-tech/core/utilities"`
11
52
  */
12
53
  export function resultSuccess(value) {
13
- return [value, null];
54
+ return {
55
+ type: RESULT.SUCCESS,
56
+ value,
57
+ };
14
58
  }
15
59
  //# sourceMappingURL=result.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"result.js","sourceRoot":"","sources":["../../../src/utilities/functions/result.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,MAAM,UAAU,aAAa,CACzB,KAAa;IAEb,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACzB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAS,KAAa;IAC/C,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AACzB,CAAC"}
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../../src/utilities/functions/result.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,MAAM,GAAG;IAClB,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;CACZ,CAAC;AAwEX;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC3B,KAAc;IAEd,OAAO,CACH,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;QAC9B,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC,OAAO;QAC7B,OAAO,IAAI,KAAK,CACnB,CAAC;AACN,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC3B,KAAc;IAEd,OAAO,CACH,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;QAC9B,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC,OAAO;QAC7B,OAAO,IAAI,KAAK,CACnB,CAAC;AACN,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,QAAQ,CACpB,KAAc;IAEd,OAAO,eAAe,CAAC,KAAK,CAAC,IAAI,eAAe,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CACzB,KAAa;IAEb,OAAO;QACH,IAAI,EAAE,MAAM,CAAC,OAAO;QACpB,KAAK;KACR,CAAC;AACN,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAS,KAAa;IAC/C,OAAO;QACH,IAAI,EAAE,MAAM,CAAC,OAAO;QACpB,KAAK;KACR,CAAC;AACN,CAAC"}
@@ -1,10 +1,4 @@
1
+ export * from "../../utilities/types/any-class.type.js";
1
2
  export * from "../../utilities/types/async-iterable-value.type.js";
2
- export * from "../../utilities/types/at-least-one.type.js";
3
- export * from "../../utilities/types/factory.type.js";
4
3
  export * from "../../utilities/types/none-func.type.js";
5
- export * from "../../utilities/types/invokable.type.js";
6
- export * from "../../utilities/types/items.type.js";
7
- export * from "../../utilities/types/lazy.type.js";
8
- export * from "../../utilities/types/one-or-more.type.js";
9
4
  export * from "../../utilities/types/promiseable.type.js";
10
- export * from "../../utilities/types/result.type.js";
@@ -1,11 +1,5 @@
1
+ export * from "../../utilities/types/any-class.type.js";
1
2
  export * from "../../utilities/types/async-iterable-value.type.js";
2
- export * from "../../utilities/types/at-least-one.type.js";
3
- export * from "../../utilities/types/factory.type.js";
4
3
  export * from "../../utilities/types/none-func.type.js";
5
- export * from "../../utilities/types/invokable.type.js";
6
- export * from "../../utilities/types/items.type.js";
7
- export * from "../../utilities/types/lazy.type.js";
8
- export * from "../../utilities/types/one-or-more.type.js";
9
4
  export * from "../../utilities/types/promiseable.type.js";
10
- export * from "../../utilities/types/result.type.js";
11
5
  //# sourceMappingURL=_module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../src/utilities/types/_module.ts"],"names":[],"mappings":"AAAA,cAAc,gDAAgD,CAAC;AAC/D,cAAc,wCAAwC,CAAC;AACvD,cAAc,mCAAmC,CAAC;AAClD,cAAc,qCAAqC,CAAC;AACpD,cAAc,qCAAqC,CAAC;AACpD,cAAc,iCAAiC,CAAC;AAChD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uCAAuC,CAAC;AACtD,cAAc,uCAAuC,CAAC;AACtD,cAAc,kCAAkC,CAAC"}
1
+ {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../src/utilities/types/_module.ts"],"names":[],"mappings":"AAAA,cAAc,qCAAqC,CAAC;AACpD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,qCAAqC,CAAC;AACpD,cAAc,uCAAuC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export type AnyClass = {
2
+ new (...args: any[]): any;
3
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=any-class.type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"any-class.type.js","sourceRoot":"","sources":["../../../src/utilities/types/any-class.type.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@daiso-tech/core",
3
3
  "private": false,
4
- "version": "0.37.3",
4
+ "version": "0.39.0",
5
5
  "author": "Yousif Abdulkarim",
6
6
  "license": "Apache-2.0",
7
7
  "type": "module",
@@ -1,15 +0,0 @@
1
- /**
2
- * @module Async
3
- */
4
- import { type Invokable } from "../../utilities/_module-exports.js";
5
- import type { StandardSchemaV1 } from "@standard-schema/spec";
6
- /**
7
- *
8
- * IMPORT_PATH: `"@daiso-tech/core/async"`
9
- * @group Middlewares
10
- */
11
- export type ErrorPolicy<TInput = unknown, TOutput = unknown> = Invokable<[error: unknown], boolean> | StandardSchemaV1<TInput, TOutput>;
12
- /**
13
- * @internal
14
- */
15
- export declare function callErrorPolicy<TInput = unknown, TOutput = unknown>(errorPolicy: ErrorPolicy<TInput, TOutput>, value: unknown): Promise<boolean>;
@@ -1,15 +0,0 @@
1
- /**
2
- * @module Async
3
- */
4
- import { callInvokable, isInvokable, } from "../../utilities/_module-exports.js";
5
- /**
6
- * @internal
7
- */
8
- export async function callErrorPolicy(errorPolicy, value) {
9
- if (isInvokable(errorPolicy)) {
10
- return callInvokable(errorPolicy, value);
11
- }
12
- const result = await errorPolicy["~standard"].validate(value);
13
- return result.issues === undefined;
14
- }
15
- //# sourceMappingURL=_shared.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_shared.js","sourceRoot":"","sources":["../../../src/async/middlewares/_shared.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EACH,aAAa,EACb,WAAW,GAEd,MAAM,gCAAgC,CAAC;AAYxC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACjC,WAAyC,EACzC,KAAc;IAEd,IAAI,WAAW,CAAC,WAAW,CAAC,EAAE,CAAC;QAC3B,OAAO,aAAa,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IAC7C,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC9D,OAAO,MAAM,CAAC,MAAM,KAAK,SAAS,CAAC;AACvC,CAAC"}
@@ -1,5 +0,0 @@
1
- /**
2
- * @module Async
3
- */
4
- import {} from "../../../utilities/_module-exports.js";
5
- //# sourceMappingURL=_shared.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_shared.js","sourceRoot":"","sources":["../../../../src/async/middlewares/hedging/_shared.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAKN,MAAM,gCAAgC,CAAC"}
@@ -1 +0,0 @@
1
- export * from "../../../../async/middlewares/hedging/concurrent-hedging/_module.js";
@@ -1,2 +0,0 @@
1
- export * from "../../../../async/middlewares/hedging/concurrent-hedging/_module.js";
2
- //# sourceMappingURL=_module.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_module.js","sourceRoot":"","sources":["../../../../../src/async/middlewares/hedging/concurrent-hedging/_module.ts"],"names":[],"mappings":"AAAA,cAAc,2DAA2D,CAAC"}
@@ -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,8BAA8B,CAAC;AAE5E;;GAEG;AACH,MAAM,aAAc,SAAQ,KAAK;IAC7B,YAAY,OAAe,EAAE,KAAe;QACxC,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC,IAAI,CAAC;IACnC,CAAC;CACJ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,UAAU,iBAAiB,CAK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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,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,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,72 +0,0 @@
1
- /**
2
- * @module EventBus
3
- */
4
- import type { ISerderRegister, ISerializable } from "../../serde/contracts/_module-exports.js";
5
- import { type ISerializedError, type OneOrMore } from "../../utilities/_module-exports.js";
6
- /**
7
- *
8
- * IMPORT_PATH: `"@daiso-tech/core/event-bus/contracts"`
9
- * @group Errors
10
- */
11
- export declare class EventBusError extends Error implements ISerializable<ISerializedError> {
12
- static deserialize(deserializedValue: ISerializedError): EventBusError;
13
- constructor(message: string, cause?: unknown);
14
- serialize(): ISerializedError;
15
- }
16
- /**
17
- *
18
- * IMPORT_PATH: `"@daiso-tech/core/event-bus/contracts"`
19
- * @group Errors
20
- */
21
- export declare class UnexpectedEventBusError extends EventBusError {
22
- static deserialize(deserializedValue: ISerializedError): EventBusError;
23
- constructor(message: string, cause?: unknown);
24
- }
25
- /**
26
- * The error is thrown when an unexpected error occurs while removing a listener.
27
- *
28
- * IMPORT_PATH: `"@daiso-tech/core/event-bus/contracts"`
29
- * @group Errors
30
- */
31
- export declare class UnableToRemoveListenerEventBusError extends UnexpectedEventBusError {
32
- static deserialize(deserializedValue: ISerializedError): EventBusError;
33
- constructor(message: string, cause?: unknown);
34
- }
35
- /**
36
- * The error is thrown when an unexpected error occurs while adding a listener.
37
- *
38
- * IMPORT_PATH: `"@daiso-tech/core/event-bus/contracts"`
39
- * @group Errors
40
- */
41
- export declare class UnableToAddListenerEventBusError extends UnexpectedEventBusError {
42
- static deserialize(deserializedValue: ISerializedError): EventBusError;
43
- constructor(message: string, cause?: unknown);
44
- }
45
- /**
46
- * The error is thrown when an unexpected error occurs while dispatching an event.
47
- *
48
- * IMPORT_PATH: `"@daiso-tech/core/event-bus/contracts"`
49
- * @group Errors
50
- */
51
- export declare class UnableToDispatchEventBusError extends UnexpectedEventBusError {
52
- constructor(message: string, cause?: unknown);
53
- }
54
- /**
55
- *
56
- * IMPORT_PATH: `"@daiso-tech/core/event-bus/contracts"`
57
- * @group Errors
58
- */
59
- export declare const EVENT_BUS_ERRORS: {
60
- Base: typeof EventBusError;
61
- Unexpected: typeof UnexpectedEventBusError;
62
- UnableToRemoveListener: typeof UnableToRemoveListenerEventBusError;
63
- UnableToAddListener: typeof UnableToAddListenerEventBusError;
64
- UnableToDispatch: typeof UnableToDispatchEventBusError;
65
- };
66
- /**
67
- * The `registerEventBusErrorsToSerde` function registers all {@link IEventBus | `IEventBus`} related errors with `ISerderRegister`, ensuring they will properly be serialized and deserialized.
68
- *
69
- * IMPORT_PATH: `"@daiso-tech/core/event-bus/contracts"`
70
- * @group Errors
71
- */
72
- export declare function registerEventBusErrorsToSerde(serde: OneOrMore<ISerderRegister>): void;