@daiso-tech/core 0.3.0 → 0.4.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 (177) hide show
  1. package/dist/cjs/_module.js +2 -0
  2. package/dist/cjs/_module.js.map +1 -1
  3. package/dist/cjs/async/_module.js +29 -0
  4. package/dist/cjs/async/_module.js.map +1 -0
  5. package/dist/cjs/async/_shared.js +34 -0
  6. package/dist/cjs/async/_shared.js.map +1 -0
  7. package/dist/cjs/async/abortable/_module.js +18 -0
  8. package/dist/cjs/async/abortable/_module.js.map +1 -0
  9. package/dist/cjs/async/abortable/abortable.js +48 -0
  10. package/dist/cjs/async/abortable/abortable.js.map +1 -0
  11. package/dist/cjs/async/abortable-iterable/_module.js +18 -0
  12. package/dist/cjs/async/abortable-iterable/_module.js.map +1 -0
  13. package/dist/cjs/async/abortable-iterable/abortable-iterable.js +32 -0
  14. package/dist/cjs/async/abortable-iterable/abortable-iterable.js.map +1 -0
  15. package/dist/cjs/async/backof-policies/_module.js +21 -0
  16. package/dist/cjs/async/backof-policies/_module.js.map +1 -0
  17. package/dist/cjs/async/backof-policies/_shared.js +7 -0
  18. package/dist/cjs/async/backof-policies/_shared.js.map +1 -0
  19. package/dist/cjs/async/backof-policies/constant-backoff-policy/_module.js +18 -0
  20. package/dist/cjs/async/backof-policies/constant-backoff-policy/_module.js.map +1 -0
  21. package/dist/cjs/async/backof-policies/constant-backoff-policy/constant-backoff-policy.js +19 -0
  22. package/dist/cjs/async/backof-policies/constant-backoff-policy/constant-backoff-policy.js.map +1 -0
  23. package/dist/cjs/async/backof-policies/exponential-backoff-policy/_module.js +18 -0
  24. package/dist/cjs/async/backof-policies/exponential-backoff-policy/_module.js.map +1 -0
  25. package/dist/cjs/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.js +23 -0
  26. package/dist/cjs/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.js.map +1 -0
  27. package/dist/cjs/async/backof-policies/linear-backoff-policy/_module.js +18 -0
  28. package/dist/cjs/async/backof-policies/linear-backoff-policy/_module.js.map +1 -0
  29. package/dist/cjs/async/backof-policies/linear-backoff-policy/linear-backoff-policy.js +23 -0
  30. package/dist/cjs/async/backof-policies/linear-backoff-policy/linear-backoff-policy.js.map +1 -0
  31. package/dist/cjs/async/backof-policies/polynomial-backoff-policy/_module.js +18 -0
  32. package/dist/cjs/async/backof-policies/polynomial-backoff-policy/_module.js.map +1 -0
  33. package/dist/cjs/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.js +23 -0
  34. package/dist/cjs/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.js.map +1 -0
  35. package/dist/cjs/async/delay/_module.js +18 -0
  36. package/dist/cjs/async/delay/_module.js.map +1 -0
  37. package/dist/cjs/async/delay/delay.js +24 -0
  38. package/dist/cjs/async/delay/delay.js.map +1 -0
  39. package/dist/cjs/async/delay-iterable/_module.js +18 -0
  40. package/dist/cjs/async/delay-iterable/_module.js.map +1 -0
  41. package/dist/cjs/async/delay-iterable/delay-iterable.js +36 -0
  42. package/dist/cjs/async/delay-iterable/delay-iterable.js.map +1 -0
  43. package/dist/cjs/async/lazy-promise/_module.js +18 -0
  44. package/dist/cjs/async/lazy-promise/_module.js.map +1 -0
  45. package/dist/cjs/async/lazy-promise/lazy-promise.js +22 -0
  46. package/dist/cjs/async/lazy-promise/lazy-promise.js.map +1 -0
  47. package/dist/cjs/async/retry/_module.js +18 -0
  48. package/dist/cjs/async/retry/_module.js.map +1 -0
  49. package/dist/cjs/async/retry/retry.js +43 -0
  50. package/dist/cjs/async/retry/retry.js.map +1 -0
  51. package/dist/cjs/async/retry-iterable/_module.js +18 -0
  52. package/dist/cjs/async/retry-iterable/_module.js.map +1 -0
  53. package/dist/cjs/async/retry-iterable/retry-iterable.js +32 -0
  54. package/dist/cjs/async/retry-iterable/retry-iterable.js.map +1 -0
  55. package/dist/cjs/async/timeout/_module.js +18 -0
  56. package/dist/cjs/async/timeout/_module.js.map +1 -0
  57. package/dist/cjs/async/timeout/timeout.js +28 -0
  58. package/dist/cjs/async/timeout/timeout.js.map +1 -0
  59. package/dist/cjs/async/timeout-iterable/_module.js +18 -0
  60. package/dist/cjs/async/timeout-iterable/_module.js.map +1 -0
  61. package/dist/cjs/async/timeout-iterable/timeout-iterable.js +11 -0
  62. package/dist/cjs/async/timeout-iterable/timeout-iterable.js.map +1 -0
  63. package/dist/cjs/storage/storage.js +25 -15
  64. package/dist/cjs/storage/storage.js.map +1 -1
  65. package/dist/cjs/utilities/_module.js +18 -0
  66. package/dist/cjs/utilities/_module.js.map +1 -0
  67. package/dist/cjs/utilities/time-span/_module.js +18 -0
  68. package/dist/cjs/utilities/time-span/_module.js.map +1 -0
  69. package/dist/cjs/utilities/time-span/time-span.js +103 -0
  70. package/dist/cjs/utilities/time-span/time-span.js.map +1 -0
  71. package/dist/esm/_module.js +2 -0
  72. package/dist/esm/_module.js.map +1 -1
  73. package/dist/esm/async/_module.js +11 -0
  74. package/dist/esm/async/_module.js.map +1 -0
  75. package/dist/esm/async/_shared.js +27 -0
  76. package/dist/esm/async/_shared.js.map +1 -0
  77. package/dist/esm/async/abortable/_module.js +2 -0
  78. package/dist/esm/async/abortable/_module.js.map +1 -0
  79. package/dist/esm/async/abortable/abortable.js +45 -0
  80. package/dist/esm/async/abortable/abortable.js.map +1 -0
  81. package/dist/esm/async/abortable-iterable/_module.js +2 -0
  82. package/dist/esm/async/abortable-iterable/_module.js.map +1 -0
  83. package/dist/esm/async/abortable-iterable/abortable-iterable.js +29 -0
  84. package/dist/esm/async/abortable-iterable/abortable-iterable.js.map +1 -0
  85. package/dist/esm/async/backof-policies/_module.js +5 -0
  86. package/dist/esm/async/backof-policies/_module.js.map +1 -0
  87. package/dist/esm/async/backof-policies/_shared.js +4 -0
  88. package/dist/esm/async/backof-policies/_shared.js.map +1 -0
  89. package/dist/esm/async/backof-policies/constant-backoff-policy/_module.js +2 -0
  90. package/dist/esm/async/backof-policies/constant-backoff-policy/_module.js.map +1 -0
  91. package/dist/esm/async/backof-policies/constant-backoff-policy/constant-backoff-policy.js +16 -0
  92. package/dist/esm/async/backof-policies/constant-backoff-policy/constant-backoff-policy.js.map +1 -0
  93. package/dist/esm/async/backof-policies/exponential-backoff-policy/_module.js +2 -0
  94. package/dist/esm/async/backof-policies/exponential-backoff-policy/_module.js.map +1 -0
  95. package/dist/esm/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.js +20 -0
  96. package/dist/esm/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.js.map +1 -0
  97. package/dist/esm/async/backof-policies/linear-backoff-policy/_module.js +2 -0
  98. package/dist/esm/async/backof-policies/linear-backoff-policy/_module.js.map +1 -0
  99. package/dist/esm/async/backof-policies/linear-backoff-policy/linear-backoff-policy.js +20 -0
  100. package/dist/esm/async/backof-policies/linear-backoff-policy/linear-backoff-policy.js.map +1 -0
  101. package/dist/esm/async/backof-policies/polynomial-backoff-policy/_module.js +2 -0
  102. package/dist/esm/async/backof-policies/polynomial-backoff-policy/_module.js.map +1 -0
  103. package/dist/esm/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.js +20 -0
  104. package/dist/esm/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.js.map +1 -0
  105. package/dist/esm/async/delay/_module.js +2 -0
  106. package/dist/esm/async/delay/_module.js.map +1 -0
  107. package/dist/esm/async/delay/delay.js +21 -0
  108. package/dist/esm/async/delay/delay.js.map +1 -0
  109. package/dist/esm/async/delay-iterable/_module.js +2 -0
  110. package/dist/esm/async/delay-iterable/_module.js.map +1 -0
  111. package/dist/esm/async/delay-iterable/delay-iterable.js +33 -0
  112. package/dist/esm/async/delay-iterable/delay-iterable.js.map +1 -0
  113. package/dist/esm/async/lazy-promise/_module.js +2 -0
  114. package/dist/esm/async/lazy-promise/_module.js.map +1 -0
  115. package/dist/esm/async/lazy-promise/lazy-promise.js +17 -0
  116. package/dist/esm/async/lazy-promise/lazy-promise.js.map +1 -0
  117. package/dist/esm/async/retry/_module.js +2 -0
  118. package/dist/esm/async/retry/_module.js.map +1 -0
  119. package/dist/esm/async/retry/retry.js +40 -0
  120. package/dist/esm/async/retry/retry.js.map +1 -0
  121. package/dist/esm/async/retry-iterable/_module.js +2 -0
  122. package/dist/esm/async/retry-iterable/_module.js.map +1 -0
  123. package/dist/esm/async/retry-iterable/retry-iterable.js +29 -0
  124. package/dist/esm/async/retry-iterable/retry-iterable.js.map +1 -0
  125. package/dist/esm/async/timeout/_module.js +2 -0
  126. package/dist/esm/async/timeout/_module.js.map +1 -0
  127. package/dist/esm/async/timeout/timeout.js +25 -0
  128. package/dist/esm/async/timeout/timeout.js.map +1 -0
  129. package/dist/esm/async/timeout-iterable/_module.js +2 -0
  130. package/dist/esm/async/timeout-iterable/_module.js.map +1 -0
  131. package/dist/esm/async/timeout-iterable/timeout-iterable.js +8 -0
  132. package/dist/esm/async/timeout-iterable/timeout-iterable.js.map +1 -0
  133. package/dist/esm/storage/storage.js +25 -15
  134. package/dist/esm/storage/storage.js.map +1 -1
  135. package/dist/esm/utilities/_module.js +2 -0
  136. package/dist/esm/utilities/_module.js.map +1 -0
  137. package/dist/esm/utilities/time-span/_module.js +2 -0
  138. package/dist/esm/utilities/time-span/_module.js.map +1 -0
  139. package/dist/esm/utilities/time-span/time-span.js +99 -0
  140. package/dist/esm/utilities/time-span/time-span.js.map +1 -0
  141. package/dist/types/_module.d.ts +2 -0
  142. package/dist/types/async/_module.d.ts +10 -0
  143. package/dist/types/async/_shared.d.ts +35 -0
  144. package/dist/types/async/abortable/_module.d.ts +1 -0
  145. package/dist/types/async/abortable/abortable.d.ts +39 -0
  146. package/dist/types/async/abortable-iterable/_module.d.ts +1 -0
  147. package/dist/types/async/abortable-iterable/abortable-iterable.d.ts +36 -0
  148. package/dist/types/async/backof-policies/_module.d.ts +5 -0
  149. package/dist/types/async/backof-policies/_shared.d.ts +12 -0
  150. package/dist/types/async/backof-policies/constant-backoff-policy/_module.d.ts +1 -0
  151. package/dist/types/async/backof-policies/constant-backoff-policy/constant-backoff-policy.d.ts +28 -0
  152. package/dist/types/async/backof-policies/exponential-backoff-policy/_module.d.ts +1 -0
  153. package/dist/types/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.d.ts +36 -0
  154. package/dist/types/async/backof-policies/linear-backoff-policy/_module.d.ts +1 -0
  155. package/dist/types/async/backof-policies/linear-backoff-policy/linear-backoff-policy.d.ts +32 -0
  156. package/dist/types/async/backof-policies/polynomial-backoff-policy/_module.d.ts +1 -0
  157. package/dist/types/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.d.ts +36 -0
  158. package/dist/types/async/delay/_module.d.ts +1 -0
  159. package/dist/types/async/delay/delay.d.ts +38 -0
  160. package/dist/types/async/delay-iterable/_module.d.ts +1 -0
  161. package/dist/types/async/delay-iterable/delay-iterable.d.ts +57 -0
  162. package/dist/types/async/lazy-promise/_module.d.ts +1 -0
  163. package/dist/types/async/lazy-promise/lazy-promise.d.ts +28 -0
  164. package/dist/types/async/retry/_module.d.ts +1 -0
  165. package/dist/types/async/retry/retry.d.ts +86 -0
  166. package/dist/types/async/retry-iterable/_module.d.ts +1 -0
  167. package/dist/types/async/retry-iterable/retry-iterable.d.ts +86 -0
  168. package/dist/types/async/timeout/_module.d.ts +1 -0
  169. package/dist/types/async/timeout/timeout.d.ts +40 -0
  170. package/dist/types/async/timeout-iterable/_module.d.ts +1 -0
  171. package/dist/types/async/timeout-iterable/timeout-iterable.d.ts +64 -0
  172. package/dist/types/contracts/storage/storage.contract.d.ts +2 -1
  173. package/dist/types/storage/storage.d.ts +5 -3
  174. package/dist/types/utilities/_module.d.ts +1 -0
  175. package/dist/types/utilities/time-span/_module.d.ts +1 -0
  176. package/dist/types/utilities/time-span/time-span.d.ts +50 -0
  177. package/package.json +1 -1
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @module Async
3
+ */
4
+ import type { TimeSpan } from "../../utilities/_module";
5
+ /**
6
+ * The <i>delayIterable</i> function adds delay to each iteration of <i>{@link AsyncIterable}</i> and the delay can be aborted by providing <i>abortSignal</i>.
7
+ * @group AsyncIterable utilities
8
+ * @throws {AsyncError} {@link AsyncError}
9
+ * @throws {AbortAsyncError} {@link AbortAsyncError}
10
+ * @example
11
+ * ```ts
12
+ * import { delayIterable, TimeSpan } from "@daiso-tech/core";
13
+ *
14
+ * const iterable: AsyncIterable<void> = {
15
+ * async *[Symbol.asyncIterator]() {
16
+ * yield 1;
17
+ * yield 2;
18
+ * yield 3;
19
+ * yield 4;
20
+ * yield 5;
21
+ * }
22
+ * }
23
+ *
24
+ * (async () => {
25
+ * for await (const item of delayIterable(iterable, TimeSpan.fromSeconds(1))) {
26
+ * console.log(item);
27
+ * }
28
+ * })();
29
+ * ```
30
+ * @example with abortSignal
31
+ * ```ts
32
+ * import { delayIterable, TimeSpan } from "@daiso-tech/core";
33
+ *
34
+ * const abortController = new AbortController();
35
+ * setTimeout(() => {
36
+ * abortController.abort("My abort error");
37
+ * }, TimeSpan.fromSeconds(3).toMilliseconds());
38
+ *
39
+ * const iterable: AsyncIterable<void> = {
40
+ * async *[Symbol.asyncIterator]() {
41
+ * yield 1;
42
+ * yield 2;
43
+ * yield 3;
44
+ * yield 4;
45
+ * yield 5;
46
+ * }
47
+ * }
48
+ *
49
+ * (async () => {
50
+ * // Will throw AbortAsyncError
51
+ * for await (const item of delayIterable(iterable, TimeSpan.fromSeconds(1), abortController.signal)) {
52
+ * console.log(item);
53
+ * }
54
+ * })();
55
+ * ```
56
+ */
57
+ export declare function delayIterable<TValue>(iterable: AsyncIterable<TValue>, time: TimeSpan, abortSignal?: AbortSignal): AsyncIterable<TValue>;
@@ -0,0 +1 @@
1
+ export * from "../../async/lazy-promise/lazy-promise";
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @module Async
3
+ */
4
+ /**
5
+ * The <i>LazyPromise</i> class is used for creating lazy <i>{@link PromiseLike}<i> object that will only execute when awaited or when then method is called.
6
+ * @group Promise utilities
7
+ * @example
8
+ * ```ts
9
+ * (async () => {
10
+ * const promise = new LazyPromise(async () => {
11
+ * console.log("I am lazy");
12
+ * });
13
+ * // "I am lazy" will only logged when awaited or then method i called.
14
+ * await promise;
15
+ * })();
16
+ * ```
17
+ */
18
+ export declare class LazyPromise<TValue> implements PromiseLike<TValue> {
19
+ private readonly asyncFn;
20
+ private promise;
21
+ constructor(asyncFn: () => PromiseLike<TValue>);
22
+ then<TResult1 = TValue, TResult2 = never>(onfulfilled?: ((value: TValue) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
23
+ }
24
+ /**
25
+ * The <i>deferLazyPromise</i> function will execute <i>{@link LazyPromise}</i> without awaiting it.
26
+ * @group Promise utilities
27
+ */
28
+ export declare function deferLazyPromise<TValue>(promise: LazyPromise<TValue>): void;
@@ -0,0 +1 @@
1
+ export * from "../../async/retry/retry";
@@ -0,0 +1,86 @@
1
+ /**
2
+ * @module Async
3
+ */
4
+ import type { BackoffPolicy } from "../../async/backof-policies/_module";
5
+ import { LazyPromise } from "../../async/lazy-promise/_module";
6
+ import { TimeSpan } from "../../utilities/_module";
7
+ export type RetryPolicy = (error: unknown) => boolean;
8
+ /**
9
+ * @group Promise utilities
10
+ */
11
+ export type RetrySettings = {
12
+ /**
13
+ * @default {4}
14
+ */
15
+ maxAttempts?: number;
16
+ backoffPolicy?: BackoffPolicy;
17
+ retryPolicy?: RetryPolicy;
18
+ /**
19
+ * @default 60_000 milliseconds
20
+ */
21
+ retryTimeout?: TimeSpan;
22
+ abortSignal?: AbortSignal;
23
+ };
24
+ /**
25
+ * The <i>retry</i> function will retry a async function if it throws an error until given <i>settings.maxAttempts</i>.
26
+ * You can add timeout for each retry by passing <i>settings.retryTimeout</i> and <i>settings.abortSignal</i> for aborting it on your own.
27
+ * You can also customize the retry policy by passing <i>settings.retryPolicy</i> and custom backof policy by passing <i>settings.backoffPolicy</i>.
28
+ * @group Promise utilities
29
+ * @throws {AsyncError} {@link AsyncError}
30
+ * @throws {AbortAsyncError} {@link AbortAsyncError}
31
+ * @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
32
+ * @throws {RetryAsyncError} {@link RetryAsyncError}
33
+ * @example
34
+ * ```ts
35
+ * import { retry, TimeSpan, exponentialBackoffPolicy } from "@daiso-tech/core";
36
+ *
37
+ * (async () => {
38
+ * // Will throw RetryAsyncError after failing 4:th attempt
39
+ * await retry(async () => {
40
+ * throw new Error("My error mesasge");
41
+ * }, {
42
+ * // You can choose the max retry attempts
43
+ * maxAttempts: 4,
44
+ * // You can provide a custom backoff policy
45
+ * backoffPolicy: exponentialBackoffPolicy(),
46
+ * // You can choose what error to retry, this function will retry all errors thrown
47
+ * retryPolicy: () => true,
48
+ * // You can provide a timeout for each retry
49
+ * retryTimeout: TimeSpan.fromMilliseconds(60_000),
50
+ * });
51
+ * })();
52
+ * ```
53
+ * @example with abortSignal
54
+ * ```ts
55
+ * import { retry, TimeSpan } from "@daiso-tech/core";
56
+ *
57
+ * (async () => {
58
+ * const abortController = new AbortController();
59
+ * abortController.abort("My abort message");
60
+ *
61
+ * // Will be aborted by throwing AbortAsyncError
62
+ * await retry(async () => {
63
+ * throw new Error("My error message");
64
+ * }, {
65
+ * abortSignal: abortController.signal
66
+ * });
67
+ * })();
68
+ * ```
69
+ * @example with custom retry policy
70
+ * ```ts
71
+ * import { retry, TimeSpan } from "@daiso-tech/core";
72
+ *
73
+ * class ErrorA extends Error {};
74
+ * class ErrorB extends Error {};
75
+ *
76
+ * (async () => {
77
+ * // Will not retry and ErrorB will be thrown
78
+ * await retry(async () => {
79
+ * throw new ErrorB("My error message");
80
+ * }, {
81
+ * retryPolicy: error => error instanceof ErrorA
82
+ * });
83
+ * })();
84
+ * ```
85
+ */
86
+ export declare function retry<TValue = void>(asyncFn: () => PromiseLike<TValue>, settings?: RetrySettings): LazyPromise<TValue>;
@@ -0,0 +1 @@
1
+ export * from "../../async/retry-iterable/retry-iterable";
@@ -0,0 +1,86 @@
1
+ import type { RetrySettings } from "../../async/retry/_module";
2
+ /**
3
+ * The <i>retryIterable</i> function will retry each iteration of {@link AsyncIterable} if it throws an error until given <i>settings.maxAttempts</i>.
4
+ * You can add timeout for each retry by passing <i>settings.retryTimeout</i> and <i>settings.abortSignal</i> for aborting it on your own.
5
+ * You can also customize the retry policy by passing <i>settings.retryPolicy</i> and custom backof policy by passing <i>settings.backoffPolicy</i>.
6
+ * @group AsyncIterable utilities
7
+ * @throws {AsyncError} {@link AsyncError}
8
+ * @throws {AbortAsyncError} {@link AbortAsyncError}
9
+ * @throws {RetryAsyncError} {@link RetryAsyncError}
10
+ * @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
11
+ * @example
12
+ * ```ts
13
+ * import { retryIterable, TimeSpan, exponentialBackoffPolicy } from "@daiso-tech/core";
14
+ *
15
+ * const iterable: AsyncIterable<void> = {
16
+ * async *[Symbol.asyncIterator]() {
17
+ * yield 1;
18
+ * throw new Error("My error mesasge");
19
+ * }
20
+ * }
21
+ *
22
+ * (async () => {
23
+ * // Will throw RetryAsyncError after failing 4:th attempt
24
+ * for await (const item of retryIterable(iterable, {
25
+ * // You can choose the max retry attempts
26
+ * maxAttempts: 4,
27
+ * // You can provide a custom backoff policy
28
+ * backoffPolicy: exponentialBackoffPolicy(),
29
+ * // You can choose what error to retry, this function will retry all errors thrown
30
+ * retryPolicy: () => true,
31
+ * // You can provide a timeout for each retry
32
+ * retryTimeout: TimeSpan.fromMilliseconds(60_000),
33
+ * })) {
34
+ * console.log(item)
35
+ * }
36
+ * })();
37
+ * ```
38
+ * @example with abortSignal
39
+ * ```ts
40
+ * import { retryIterable, TimeSpan } from "@daiso-tech/core";
41
+ *
42
+ * const iterable: AsyncIterable<void> = {
43
+ * async *[Symbol.asyncIterator]() {
44
+ * yield 1;
45
+ * throw new Error("My error mesasge");
46
+ * }
47
+ * }
48
+ *
49
+ * (async () => {
50
+ * const abortController = new AbortController();
51
+ * abortController.abort("My abort message");
52
+ *
53
+ * // Will be aborted by throwing AbortAsyncError
54
+ * for await (const item of retry(iterable, {
55
+ * abortSignal: abortController.signal
56
+ * })) {
57
+ * console.log(item)
58
+ * }
59
+ * })();
60
+ * ```
61
+ * @example with abortSignal
62
+ * ```ts
63
+ * import { retryIterable, TimeSpan } from "@daiso-tech/core";
64
+ *
65
+ * class ErrorA extends Error {};
66
+ * class ErrorB extends Error {};
67
+ *
68
+ * const iterable: AsyncIterable<void> = {
69
+ * async *[Symbol.asyncIterator]() {
70
+ * yield 1;
71
+ * throw new ErrorB("My error mesasge");
72
+ * }
73
+ * }
74
+ *
75
+ *
76
+ * (async () => {
77
+ * // Will not retry and ErrorB will be thrown
78
+ * for await (const item of retry(iterable, {
79
+ * retryPolicy: error => error instanceof ErrorA
80
+ * })) {
81
+ * console.log(item)
82
+ * }
83
+ * })();
84
+ * ```
85
+ */
86
+ export declare function retryIterable<TValue>(iterable: AsyncIterable<TValue>, settings?: RetrySettings): AsyncIterable<TValue>;
@@ -0,0 +1 @@
1
+ export * from "../../async/timeout/timeout";
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @module Async
3
+ */
4
+ import { LazyPromise } from "../../async/lazy-promise/_module";
5
+ import type { TimeSpan } from "../../utilities/_module";
6
+ /**
7
+ * The <i>timeout</i> function makes a <i>{@link PromiseLike}</i> object abort after a given <i>time</i> and you can also abort it on your own by passing <i>abortSignal</i>.
8
+ * @group Promise utilities
9
+ * @throws {AsyncError} {@link AsyncError}
10
+ * @throws {AbortAsyncError} {@link AbortAsyncError}
11
+ * @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
12
+ * @example
13
+ * ```ts
14
+ * import { timeout, delay, TimeSpan } from "@daiso-tech/core";
15
+ *
16
+ * (async () => {
17
+ * // Will throw an AbortAsyncError
18
+ * await timeout(async () => {
19
+ * await delay(TimeSpan.fromSeconds(10));
20
+ * }, TimeSpan.fromSeconds(5));
21
+ * })();
22
+ * ```
23
+ * @example with abortSignal
24
+ * ```ts
25
+ * import { timeout, delay, TimeSpan } from "@daiso-tech/core";
26
+ *
27
+ * const abortController = new AbortController();
28
+ * setTimeout(() => {
29
+ * abortController.abort("My abort error");
30
+ * }, TimeSpan.fromSeconds(1));
31
+ *
32
+ * (async () => {
33
+ * // Will abort after 1 second by throwing AbortAsyncError
34
+ * await timeout(async () => {
35
+ * await delay(TimeSpan.fromSeconds(10));
36
+ * }, TimeSpan.fromSeconds(5), abortController.signal);
37
+ * })();
38
+ * ```
39
+ */
40
+ export declare function timeout<TValue>(asyncFn: () => PromiseLike<TValue>, time: TimeSpan, abortSignal?: AbortSignal): LazyPromise<TValue>;
@@ -0,0 +1 @@
1
+ export * from "../../async/timeout-iterable/timeout-iterable";
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @module Async
3
+ */
4
+ import type { TimeSpan } from "../../utilities/_module";
5
+ /**
6
+ * The <i>timeoutIterable</i> function makes a <i>{@link AsyncIterable}</i> abort after a given <i>time</i> and you can also abort it on your own by passing <i>abortSignal</i>.
7
+ * @group AsyncIterable utilities
8
+ * @throws {AsyncError} {@link AsyncError}
9
+ * @throws {AbortAsyncError} {@link AbortAsyncError}
10
+ * @example
11
+ * ```ts
12
+ * import { timeoutIterable, delayIterable, TimeSpan } from "@daiso-tech/core";
13
+ *
14
+ * const iterable: AsyncIterable<void> = delayIterable({
15
+ * async *[Symbol.asyncIterator]() {
16
+ * yield 1;
17
+ * yield 2;
18
+ * yield 3;
19
+ * yield 4;
20
+ * yield 5;
21
+ * yield 6;
22
+ * yield 7;
23
+ * yield 8;
24
+ * }
25
+ * }, TimeSpan.fromSeconds(1));
26
+ *
27
+ * (async () => {
28
+ * // Will throw an AbortAsyncError
29
+ * for (const item of timeoutIterable(iterable, TimeSpan.fromSeconds(5))) {
30
+ * console.log(item);
31
+ * }
32
+ * })();
33
+ * ```
34
+ * @example with abortSignal
35
+ * ```ts
36
+ * import { timeoutIterable, delay, TimeSpan } from "@daiso-tech/core";
37
+ *
38
+ * const iterable: AsyncIterable<void> = delayIterable({
39
+ * async *[Symbol.asyncIterator]() {
40
+ * yield 1;
41
+ * yield 2;
42
+ * yield 3;
43
+ * yield 4;
44
+ * yield 5;
45
+ * yield 6;
46
+ * yield 7;
47
+ * yield 8;
48
+ * }
49
+ * }, TimeSpan.fromSeconds(1));
50
+ *
51
+ * const abortController = new AbortController();
52
+ * setTimeout(() => {
53
+ * abortController.abort("My abort message");
54
+ * }, TimeSpan.fromSeconds(3));
55
+ *
56
+ * (async () => {
57
+ * // Will abort after 3 second by throwing AbortAsyncError
58
+ * for (const item of timeoutIterable(iterable, TimeSpan.fromSeconds(5), abortController.signal)) {
59
+ * console.log(item);
60
+ * }
61
+ * })();
62
+ * ```
63
+ */
64
+ export declare function timeoutIterable<TValue>(iterable: AsyncIterable<TValue>, time: TimeSpan, abortSignal?: AbortSignal): AsyncIterable<TValue>;
@@ -9,7 +9,8 @@ export type StorageValue<T> = Exclude<T, AnyFunction | undefined | null>;
9
9
  * @throws {TypeStorageError} {@link TypeStorageError}
10
10
  * @group Contracts
11
11
  */
12
- export type IStorage<TType = unknown> = AsyncDisposable & {
12
+ export type IStorage<TType = unknown> = {
13
+ namespace<TNamespaceType extends TType>(name: string): IStorage<TNamespaceType>;
13
14
  /**
14
15
  * Returns true when key is found otherwise false will be returned.
15
16
  * @throws {StorageError} {@link StorageError}
@@ -7,10 +7,12 @@ import { type AsyncLazyable, type GetOrAddValue } from "../_shared/types";
7
7
  export type StorageSettings = {
8
8
  namespace?: string;
9
9
  };
10
- export declare class Storage<TType> implements IStorage<TType> {
10
+ export declare class Storage<TType = unknown> implements IStorage<TType> {
11
11
  private readonly storageAdapter;
12
- constructor(storageAdapter: IStorageAdapter<TType>, { namespace }?: StorageSettings);
13
- [Symbol.asyncDispose](): Promise<void>;
12
+ private readonly namespaceStorageAdapter;
13
+ private readonly settings;
14
+ constructor(storageAdapter: IStorageAdapter<TType>, settings?: StorageSettings);
15
+ namespace<TNamespaceType extends TType>(name: string): IStorage<TNamespaceType>;
14
16
  exists(key: string): Promise<boolean>;
15
17
  existsMany<TKeys extends string>(keys: TKeys[]): Promise<Record<TKeys, boolean>>;
16
18
  missing(key: string): Promise<boolean>;
@@ -0,0 +1 @@
1
+ export * from "../utilities/time-span/_module";
@@ -0,0 +1 @@
1
+ export * from "../../utilities/time-span/time-span";
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @module Utilities
3
+ */
4
+ /**
5
+ * The <i>TimeSpan</i> class is used for representing time interval that is the difference between two times measured in a number of days, hours, minutes, and seconds.
6
+ * <i>TimeSpan</i> cannot be negative.
7
+ */
8
+ export declare class TimeSpan {
9
+ private readonly milliseconds;
10
+ private static secondInMilliseconds;
11
+ private static minuteInMilliseconds;
12
+ private static hourInMilliseconds;
13
+ private static dayInMilliseconds;
14
+ private constructor();
15
+ static fromMilliseconds(milliseconds: number): TimeSpan;
16
+ static fromSeconds(seconds: number): TimeSpan;
17
+ static fromMinutes(minutes: number): TimeSpan;
18
+ static fromHours(hours: number): TimeSpan;
19
+ static fromDays(days: number): TimeSpan;
20
+ static fromTimeSpan(timeSpan: TimeSpan): TimeSpan;
21
+ static fromDateRange(from: Date, to: Date): TimeSpan;
22
+ addMilliseconds(milliseconds: number): TimeSpan;
23
+ addSeconds(seconds: number): TimeSpan;
24
+ addMinutes(minutes: number): TimeSpan;
25
+ addHours(hours: number): TimeSpan;
26
+ addDays(days: number): TimeSpan;
27
+ addTimeSpan(timeSpan: TimeSpan): TimeSpan;
28
+ subtractMilliseconds(milliseconds: number): TimeSpan;
29
+ subtractSeconds(seconds: number): TimeSpan;
30
+ subtractMinutes(minutes: number): TimeSpan;
31
+ subtractHours(hours: number): TimeSpan;
32
+ subtractDays(days: number): TimeSpan;
33
+ subtractTimeSpan(timeSpan: TimeSpan): TimeSpan;
34
+ multiply(value: number): TimeSpan;
35
+ divide(value: number): TimeSpan;
36
+ toMilliseconds(): number;
37
+ toSeconds(): number;
38
+ toMinutes(): number;
39
+ toHours(): number;
40
+ toDays(): number;
41
+ toUTCTimestamp(): number;
42
+ /**
43
+ * Will return endDate relative to a given <i>startDate</i> argument.
44
+ */
45
+ toEndDate(startDate: Date): Date;
46
+ /**
47
+ * Will return startDate relative to a given <i>endDate</i> argument.
48
+ */
49
+ toStartDate(endDate: Date): Date;
50
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@daiso-tech/core",
3
3
  "private": false,
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "author": "Yousif Abdulkarim",
6
6
  "license": "Apache-2.0",
7
7
  "main": "./dist/cjs/_module.js",