@oscarpalmer/atoms 0.201.0 → 0.203.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 (156) hide show
  1. package/dist/array/difference.d.mts +4 -5
  2. package/dist/array/exists.d.mts +5 -6
  3. package/dist/array/filter.d.mts +10 -11
  4. package/dist/array/find.d.mts +10 -11
  5. package/dist/array/first.d.mts +10 -11
  6. package/dist/array/flatten.d.mts +2 -3
  7. package/dist/array/from.d.mts +7 -8
  8. package/dist/array/group-by.d.mts +16 -185
  9. package/dist/array/insert.d.mts +3 -4
  10. package/dist/array/intersection.d.mts +4 -5
  11. package/dist/array/last.d.mts +10 -11
  12. package/dist/array/move.d.mts +9 -10
  13. package/dist/array/partition.d.mts +5 -6
  14. package/dist/array/push.d.mts +2 -3
  15. package/dist/array/reverse.d.mts +2 -3
  16. package/dist/array/select.d.mts +11 -12
  17. package/dist/array/single.d.mts +4 -5
  18. package/dist/array/slice.d.mts +12 -13
  19. package/dist/array/sort.d.mts +31 -32
  20. package/dist/array/sort.mjs +41 -33
  21. package/dist/array/splice.d.mts +5 -6
  22. package/dist/array/swap.d.mts +8 -9
  23. package/dist/array/to-map.d.mts +18 -175
  24. package/dist/array/to-record.d.mts +18 -175
  25. package/dist/array/to-set.d.mts +4 -5
  26. package/dist/array/toggle.d.mts +4 -5
  27. package/dist/array/union.d.mts +4 -5
  28. package/dist/array/unique.d.mts +4 -5
  29. package/dist/array/update.d.mts +4 -5
  30. package/dist/beacon.d.mts +8 -9
  31. package/dist/beacon.mjs +90 -65
  32. package/dist/color/constants.d.mts +14 -13
  33. package/dist/color/constants.mjs +9 -2
  34. package/dist/color/index.d.mts +2 -2
  35. package/dist/color/instance.d.mts +3 -3
  36. package/dist/color/instance.mjs +125 -61
  37. package/dist/color/misc/alpha.d.mts +4 -5
  38. package/dist/color/misc/format.d.mts +6 -7
  39. package/dist/color/misc/format.mjs +15 -12
  40. package/dist/color/misc/get.d.mts +14 -15
  41. package/dist/color/misc/is.d.mts +16 -17
  42. package/dist/color/misc/is.mjs +3 -4
  43. package/dist/color/misc/state.d.mts +8 -10
  44. package/dist/color/misc/state.mjs +22 -20
  45. package/dist/color/models.d.mts +25 -19
  46. package/dist/color/space/hex.d.mts +9 -10
  47. package/dist/color/space/hsl.d.mts +8 -9
  48. package/dist/color/space/hwb.d.mts +9 -10
  49. package/dist/color/space/rgb.d.mts +11 -12
  50. package/dist/function/limit.d.mts +9 -8
  51. package/dist/function/limit.mjs +5 -5
  52. package/dist/function/once.d.mts +26 -6
  53. package/dist/function/once.mjs +135 -76
  54. package/dist/function/retry.d.mts +5 -6
  55. package/dist/function/retry.mjs +3 -3
  56. package/dist/index.d.mts +244 -584
  57. package/dist/index.mjs +1191 -810
  58. package/dist/internal/abort.d.mts +3 -4
  59. package/dist/internal/array/callbacks.d.mts +4 -5
  60. package/dist/internal/array/chunk.d.mts +2 -3
  61. package/dist/internal/array/compact.d.mts +3 -4
  62. package/dist/internal/array/find.d.mts +10 -11
  63. package/dist/internal/array/get.d.mts +7 -8
  64. package/dist/internal/array/group.d.mts +2 -3
  65. package/dist/internal/array/index-of.d.mts +10 -11
  66. package/dist/internal/array/insert.d.mts +5 -6
  67. package/dist/internal/array/match.d.mts +17 -18
  68. package/dist/internal/array/overlap.d.mts +2 -3
  69. package/dist/internal/array/sets.d.mts +5 -6
  70. package/dist/internal/array/shuffle.d.mts +2 -3
  71. package/dist/internal/array/update.d.mts +2 -3
  72. package/dist/internal/defaults.d.mts +3 -4
  73. package/dist/internal/function/assert.d.mts +10 -11
  74. package/dist/internal/function/limit.d.mts +41 -0
  75. package/dist/internal/function/limit.mjs +110 -0
  76. package/dist/internal/function/memoize.d.mts +4 -5
  77. package/dist/internal/function/memoize.mjs +53 -31
  78. package/dist/internal/function/misc.d.mts +2 -3
  79. package/dist/internal/function/work.d.mts +53 -54
  80. package/dist/internal/herald.d.mts +8 -9
  81. package/dist/internal/herald.mjs +38 -26
  82. package/dist/internal/is.d.mts +19 -20
  83. package/dist/internal/math/aggregate.d.mts +14 -15
  84. package/dist/internal/math/misc.d.mts +3 -4
  85. package/dist/internal/number.d.mts +4 -5
  86. package/dist/internal/random.d.mts +3 -4
  87. package/dist/internal/result/misc.d.mts +29 -14
  88. package/dist/internal/result/misc.mjs +4 -0
  89. package/dist/internal/result/models.d.mts +9 -10
  90. package/dist/internal/sized/map.d.mts +2 -3
  91. package/dist/internal/sized/misc.d.mts +2 -3
  92. package/dist/internal/string/case.d.mts +9 -10
  93. package/dist/internal/string/misc.d.mts +8 -9
  94. package/dist/internal/subscription.d.mts +14 -15
  95. package/dist/internal/subscription.mjs +69 -57
  96. package/dist/internal/value/compare.d.mts +17 -9
  97. package/dist/internal/value/compare.mjs +7 -7
  98. package/dist/internal/value/equal.d.mts +34 -15
  99. package/dist/internal/value/equal.mjs +28 -13
  100. package/dist/internal/value/get.d.mts +3 -4
  101. package/dist/internal/value/handlers.d.mts +19 -9
  102. package/dist/internal/value/handlers.mjs +50 -50
  103. package/dist/internal/value/has.d.mts +6 -7
  104. package/dist/internal/value/misc.d.mts +6 -7
  105. package/dist/internal/value/partial.d.mts +3 -4
  106. package/dist/internal/value/set.d.mts +4 -5
  107. package/dist/is.d.mts +13 -13
  108. package/dist/logger.d.mts +5 -4
  109. package/dist/logger.mjs +61 -54
  110. package/dist/math.d.mts +20 -20
  111. package/dist/models.d.mts +58 -46
  112. package/dist/promise/constants.d.mts +14 -15
  113. package/dist/promise/delay.d.mts +3 -4
  114. package/dist/promise/delay.mjs +4 -4
  115. package/dist/promise/helpers.d.mts +7 -8
  116. package/dist/promise/index.d.mts +13 -14
  117. package/dist/promise/misc.d.mts +6 -6
  118. package/dist/promise/models.d.mts +16 -17
  119. package/dist/promise/timed.d.mts +4 -5
  120. package/dist/promise/timed.mjs +5 -5
  121. package/dist/query.d.mts +3 -4
  122. package/dist/queue.d.mts +14 -15
  123. package/dist/queue.mjs +185 -129
  124. package/dist/random.d.mts +8 -8
  125. package/dist/result/index.d.mts +8 -9
  126. package/dist/result/match.d.mts +6 -7
  127. package/dist/result/misc.d.mts +3 -3
  128. package/dist/result/work/flow.d.mts +28 -29
  129. package/dist/result/work/pipe.d.mts +24 -25
  130. package/dist/sized/set.d.mts +2 -3
  131. package/dist/string/fuzzy.d.mts +21 -12
  132. package/dist/string/fuzzy.mjs +77 -49
  133. package/dist/string/index.d.mts +8 -8
  134. package/dist/string/match.d.mts +4 -5
  135. package/dist/string/normalize.d.mts +8 -9
  136. package/dist/string/normalize.mjs +9 -4
  137. package/dist/string/template.d.mts +9 -9
  138. package/dist/string/template.mjs +11 -4
  139. package/dist/value/clone.d.mts +21 -13
  140. package/dist/value/clone.mjs +22 -20
  141. package/dist/value/collection.d.mts +4 -5
  142. package/dist/value/diff.d.mts +5 -6
  143. package/dist/value/freeze.d.mts +18 -19
  144. package/dist/value/freeze.mjs +1 -1
  145. package/dist/value/merge.d.mts +13 -14
  146. package/dist/value/merge.mjs +21 -5
  147. package/dist/value/omit.d.mts +2 -3
  148. package/dist/value/pick.d.mts +2 -3
  149. package/dist/value/shake.d.mts +3 -4
  150. package/dist/value/smush.d.mts +3 -4
  151. package/dist/value/transform.d.mts +15 -10
  152. package/dist/value/transform.mjs +12 -7
  153. package/dist/value/unsmush.d.mts +3 -4
  154. package/package.json +1 -1
  155. package/dist/internal/function/timer.d.mts +0 -10
  156. package/dist/internal/function/timer.mjs +0 -87
@@ -6,7 +6,7 @@ import { PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./constants.mjs";
6
6
  /**
7
7
  * A _Promise_ that can be canceled
8
8
  */
9
- declare class CancelablePromise<Value = void> extends Promise<Value> {
9
+ export declare class CancelablePromise<Value = void> extends Promise<Value> {
10
10
  #private;
11
11
  constructor(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void);
12
12
  /**
@@ -19,7 +19,7 @@ declare class CancelablePromise<Value = void> extends Promise<Value> {
19
19
  /**
20
20
  * A _Promise_ that was fulfilled
21
21
  */
22
- type FulfilledPromise<Value> = {
22
+ export type FulfilledPromise<Value> = {
23
23
  /**
24
24
  * Status of the _Promise_
25
25
  */
@@ -29,18 +29,18 @@ type FulfilledPromise<Value> = {
29
29
  */
30
30
  value: Awaited<Value>;
31
31
  };
32
- type PromiseData = {
32
+ export type PromiseData = {
33
33
  last: number;
34
34
  result: ArrayOrPlainObject;
35
35
  };
36
- type PromiseHandlers = {
36
+ export type PromiseHandlers = {
37
37
  resolve: (value: unknown[]) => void;
38
38
  reject: (reason: unknown) => void;
39
39
  };
40
40
  /**
41
41
  * Options for a _Promise_-handling function
42
42
  */
43
- type PromiseOptions = {
43
+ export type PromiseOptions = {
44
44
  /**
45
45
  * AbortSignal for aborting the _Promise_; when aborted, the _Promise_ will reject with the reason of the signal
46
46
  */
@@ -50,7 +50,7 @@ type PromiseOptions = {
50
50
  */
51
51
  time?: number;
52
52
  };
53
- type PromiseParameters = {
53
+ export type PromiseParameters = {
54
54
  aborter?: Aborter;
55
55
  complete: boolean;
56
56
  data: PromiseData;
@@ -65,18 +65,18 @@ type PromiseParameters = {
65
65
  * - `complete`: wait for all _Promises_ to settle, then return the results, as an array of fulfilled and/or rejected results
66
66
  * - `first`: rejects on the first rejected _Promise_, and returns an array of values
67
67
  */
68
- type PromiseStrategy = 'complete' | 'first';
68
+ export type PromiseStrategy = 'complete' | 'first';
69
69
  /**
70
70
  * An error thrown when a promise times out
71
71
  */
72
- declare class PromiseTimeoutError extends Error {
72
+ export declare class PromiseTimeoutError extends Error {
73
73
  constructor();
74
74
  }
75
- type PromisesItems<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? Promise<Value> : never : Items[ItemsKey] extends Promise<infer Value> ? Promise<Value> : Promise<Items[ItemsKey]>; };
75
+ export type PromisesItems<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? Promise<Value> : never : Items[ItemsKey] extends Promise<infer Value> ? Promise<Value> : Promise<Items[ItemsKey]>; };
76
76
  /**
77
77
  * Options for handling multiple _Promises_
78
78
  */
79
- type PromisesOptions = {
79
+ export type PromisesOptions = {
80
80
  /**
81
81
  * AbortSignal for aborting the _Promises_; when aborted, the _Promises_ will reject with the reason of the signal
82
82
  */
@@ -86,14 +86,14 @@ type PromisesOptions = {
86
86
  */
87
87
  strategy?: PromiseStrategy;
88
88
  };
89
- type PromisesResult<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value> ? Result<Awaited<Value>> : never; };
90
- type PromisesUnwrapped<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? Awaited<Value> : never : Items[ItemsKey] extends Promise<infer Value> ? Awaited<Value> : never; };
91
- type PromisesValue<Value> = FulfilledPromise<Value> | RejectedPromise;
92
- type PromisesValues<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never; };
89
+ export type PromisesResult<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value> ? Result<Awaited<Value>> : never; };
90
+ export type PromisesUnwrapped<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? Awaited<Value> : never : Items[ItemsKey] extends Promise<infer Value> ? Awaited<Value> : never; };
91
+ export type PromisesValue<Value> = FulfilledPromise<Value> | RejectedPromise;
92
+ export type PromisesValues<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never; };
93
93
  /**
94
94
  * A _Promise_ that was rejected
95
95
  */
96
- type RejectedPromise = {
96
+ export type RejectedPromise = {
97
97
  /**
98
98
  * Status of the _Promise_
99
99
  */
@@ -103,5 +103,4 @@ type RejectedPromise = {
103
103
  */
104
104
  reason: unknown;
105
105
  };
106
- //#endregion
107
- export { CancelablePromise, FulfilledPromise, PromiseData, PromiseHandlers, PromiseOptions, PromiseParameters, PromiseStrategy, PromiseTimeoutError, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValue, PromisesValues, RejectedPromise };
106
+ //#endregion
@@ -1,7 +1,7 @@
1
1
  import { RequiredKeys } from "../models.mjs";
2
2
  import { PromiseOptions } from "./models.mjs";
3
3
  //#region src/promise/timed.d.ts
4
- declare function getTimedPromise<Value>(promise: Promise<Value>, time: number, signal?: AbortSignal): Promise<Value>;
4
+ export declare function getTimedPromise<Value>(promise: Promise<Value>, time: number, signal?: AbortSignal): Promise<Value>;
5
5
  /**
6
6
  * Create a _Promise_ that should be settled within a certain amount of time
7
7
  *
@@ -9,7 +9,7 @@ declare function getTimedPromise<Value>(promise: Promise<Value>, time: number, s
9
9
  * @param options Timed options
10
10
  * @returns Timed _Promise_
11
11
  */
12
- declare function timed<Value>(promise: Promise<Value>, options: RequiredKeys<PromiseOptions, 'time'>): Promise<Value>;
12
+ export declare function timed<Value>(promise: Promise<Value>, options: RequiredKeys<PromiseOptions, 'time'>): Promise<Value>;
13
13
  /**
14
14
  * Create a _Promise_ that should be settled within a certain amount of time
15
15
  *
@@ -17,6 +17,5 @@ declare function timed<Value>(promise: Promise<Value>, options: RequiredKeys<Pro
17
17
  * @param time How long to wait for _(in milliseconds; defaults to `0`)_
18
18
  * @returns Timed _Promise_
19
19
  */
20
- declare function timed<Value>(promise: Promise<Value>, time: number): Promise<Value>;
21
- //#endregion
22
- export { getTimedPromise, timed };
20
+ export declare function timed<Value>(promise: Promise<Value>, time: number): Promise<Value>;
21
+ //#endregion
@@ -1,5 +1,5 @@
1
1
  import { createAborter } from "../internal/abort.mjs";
2
- import { TIMER_WAIT, getTimer } from "../internal/function/timer.mjs";
2
+ import { LIMITER_WAIT, getLimiter } from "../internal/function/limit.mjs";
3
3
  import { PROMISE_MESSAGE_EXPECTATION_TIMED } from "./constants.mjs";
4
4
  import { PromiseTimeoutError } from "./models.mjs";
5
5
  import { createPromiseOptions } from "./helpers.mjs";
@@ -7,17 +7,17 @@ import { settlePromise } from "./misc.mjs";
7
7
  //#region src/promise/timed.ts
8
8
  async function getTimedPromise(promise, time, signal) {
9
9
  const aborter = createAborter(signal, () => {
10
- timer.cancel();
10
+ limiter.cancel();
11
11
  rejector(signal?.reason);
12
12
  });
13
- const timer = getTimer(TIMER_WAIT, () => settlePromise(rejector, new PromiseTimeoutError(), aborter), time);
13
+ const limiter = getLimiter(LIMITER_WAIT, () => settlePromise(rejector, new PromiseTimeoutError(), aborter), time);
14
14
  let rejector;
15
15
  return Promise.race([promise, new Promise((_, reject) => {
16
16
  rejector = reject;
17
- timer();
17
+ limiter.run();
18
18
  })]).then((value) => {
19
19
  aborter?.cancel();
20
- timer.cancel();
20
+ limiter.cancel();
21
21
  rejector(void 0);
22
22
  return value;
23
23
  });
package/dist/query.d.mts CHANGED
@@ -6,13 +6,12 @@ import { PlainObject } from "./models.mjs";
6
6
  * @param query Query string to convert
7
7
  * @returns Plain object representation of the query string
8
8
  */
9
- declare function fromQuery(query: string): PlainObject;
9
+ export declare function fromQuery(query: string): PlainObject;
10
10
  /**
11
11
  * Convert a plain _(nested)_ object to a query string
12
12
  *
13
13
  * @param parameters Plain object to convert
14
14
  * @returns Query string representation of the object
15
15
  */
16
- declare function toQuery(parameters: PlainObject): string;
17
- //#endregion
18
- export { fromQuery, toQuery };
16
+ export declare function toQuery(parameters: PlainObject): string;
17
+ //#endregion
package/dist/queue.d.mts CHANGED
@@ -3,7 +3,7 @@ import { GenericAsyncCallback, GenericCallback } from "./models.mjs";
3
3
  /**
4
4
  * A queue that can be used to manage (a)synchronous tasks with a specific key
5
5
  */
6
- type KeyedQueue<Callback extends GenericCallback | GenericAsyncCallback, CallbackParameters extends unknown[] = Parameters<Callback>> = {
6
+ export type KeyedQueue<Callback extends GenericCallback | GenericAsyncCallback, CallbackParameters extends unknown[] = Parameters<Callback>> = {
7
7
  /**
8
8
  * Get keys of all active queues
9
9
  */
@@ -101,7 +101,7 @@ type KeyedQueue<Callback extends GenericCallback | GenericAsyncCallback, Callbac
101
101
  /**
102
102
  * A queue that can be used to manage (a)synchronous tasks
103
103
  */
104
- type Queue<Callback extends GenericCallback | GenericAsyncCallback, CallbackParameters extends unknown[] = Parameters<Callback>> = {
104
+ export type Queue<Callback extends GenericCallback | GenericAsyncCallback, CallbackParameters extends unknown[] = Parameters<Callback>> = {
105
105
  /**
106
106
  * Is the queue active?
107
107
  */
@@ -167,10 +167,10 @@ type Queue<Callback extends GenericCallback | GenericAsyncCallback, CallbackPara
167
167
  /**
168
168
  * An error thrown by the Queue when an operation fails
169
169
  */
170
- declare class QueueError extends Error {
170
+ export declare class QueueError extends Error {
171
171
  constructor(message: string);
172
172
  }
173
- type QueueOptions = {
173
+ export type QueueOptions = {
174
174
  /**
175
175
  * Automatically start processing the queue when the first item is added _(defaults to `true`)_
176
176
  */
@@ -187,7 +187,7 @@ type QueueOptions = {
187
187
  /**
188
188
  * A queued item
189
189
  */
190
- type Queued<Value> = {
190
+ export type Queued<Value> = {
191
191
  /**
192
192
  * ID of the queued item _(can be used to remove it from the queue)_
193
193
  */
@@ -204,15 +204,15 @@ type Tail<Values extends any[]> = Values extends [infer _, ...infer Rest] ? Rest
204
204
  * @param value Value to check
205
205
  * @returns `true` if the value is a keyed queue, otherwise `false`
206
206
  */
207
- declare function isKeyedQueue(value: unknown): value is KeyedQueue<GenericAsyncCallback>;
207
+ export declare function isKeyedQueue(value: unknown): value is KeyedQueue<GenericAsyncCallback>;
208
208
  /**
209
209
  * Is the value a queue?
210
210
  *
211
211
  * @param value Value to check
212
212
  * @returns `true` if the value is a queue, otherwise `false`
213
213
  */
214
- declare function isQueue(value: unknown): value is Queue<GenericCallback | GenericAsyncCallback>;
215
- declare function isQueueInstance<Instance>(name: string, value: unknown): value is Instance;
214
+ export declare function isQueue(value: unknown): value is Queue<GenericCallback | GenericAsyncCallback>;
215
+ export declare function isQueueInstance<Instance>(name: string, value: unknown): value is Instance;
216
216
  /**
217
217
  * Create a keyed queue for an asynchronous callback function, where each key has its own queue
218
218
  *
@@ -221,7 +221,7 @@ declare function isQueueInstance<Instance>(name: string, value: unknown): value
221
221
  * @param callback Callback function for queued items
222
222
  * @param options Queue options
223
223
  */
224
- declare function keyedQueue<Callback extends (key: string, ...parameters: any[]) => Promise<void>>(callback: Callback, options?: QueueOptions): KeyedQueue<Callback, Parameters<Callback>>;
224
+ export declare function keyedQueue<Callback extends (key: string, ...parameters: any[]) => Promise<void>>(callback: Callback, options?: QueueOptions): KeyedQueue<Callback, Parameters<Callback>>;
225
225
  /**
226
226
  * Create a keyed queue for an asynchronous callback function, where each key has its own queue
227
227
  *
@@ -230,7 +230,7 @@ declare function keyedQueue<Callback extends (key: string, ...parameters: any[])
230
230
  * @param callback Callback function for queued items
231
231
  * @param options Queue options
232
232
  */
233
- declare function keyedQueue<Callback extends (key: string, ...parameters: any[]) => void>(callback: Callback, options?: QueueOptions): KeyedQueue<Callback, Parameters<Callback>>;
233
+ export declare function keyedQueue<Callback extends (key: string, ...parameters: any[]) => void>(callback: Callback, options?: QueueOptions): KeyedQueue<Callback, Parameters<Callback>>;
234
234
  /**
235
235
  * Create a queue for an asynchronous callback function
236
236
  *
@@ -238,7 +238,7 @@ declare function keyedQueue<Callback extends (key: string, ...parameters: any[])
238
238
  * @param options Queue options
239
239
  * @returns Queue instance
240
240
  */
241
- declare function queue<Callback extends GenericAsyncCallback>(callback: Callback, options?: QueueOptions): Queue<Callback, Parameters<Callback>>;
241
+ export declare function queue<Callback extends GenericAsyncCallback>(callback: Callback, options?: QueueOptions): Queue<Callback, Parameters<Callback>>;
242
242
  /**
243
243
  * Create a queue for a synchronous callback function
244
244
  *
@@ -246,9 +246,8 @@ declare function queue<Callback extends GenericAsyncCallback>(callback: Callback
246
246
  * @param options Queue options
247
247
  * @returns Queue instance
248
248
  */
249
- declare function queue<Callback extends GenericCallback>(callback: Callback, options?: QueueOptions): Queue<Callback, Parameters<Callback>>;
250
- declare namespace queue {
249
+ export declare function queue<Callback extends GenericCallback>(callback: Callback, options?: QueueOptions): Queue<Callback, Parameters<Callback>>;
250
+ export declare namespace queue {
251
251
  var keyed: typeof keyedQueue;
252
252
  }
253
- //#endregion
254
- export { KeyedQueue, Queue, QueueError, QueueOptions, Queued, isKeyedQueue, isQueue, isQueueInstance, keyedQueue, queue };
253
+ //#endregion
package/dist/queue.mjs CHANGED
@@ -1,6 +1,9 @@
1
1
  import { createAborter } from "./internal/abort.mjs";
2
2
  import { getBooleanOrDefault, getNumberOrDefault } from "./internal/defaults.mjs";
3
3
  //#region src/queue.ts
4
+ const QUEUE_NAME_KEYED = "keyedQueue";
5
+ const QUEUE_NAME_QUEUE = "queue";
6
+ const QUEUE_PROPERTY = "$queue";
4
7
  /**
5
8
  * An error thrown by the Queue when an operation fails
6
9
  */
@@ -10,10 +13,159 @@ var QueueError = class extends Error {
10
13
  this.name = QUEUE_ERROR_NAME;
11
14
  }
12
15
  };
13
- function addToQueue(instance, state, parameters, signal) {
14
- if (instance.full) throw new QueueError(QUEUE_MESSAGE_MAXIMUM);
16
+ function KeyedQueue(callback, options) {
17
+ Object.defineProperty(this, QUEUE_SYMBOL, { value: {
18
+ callback,
19
+ options: createQueueOptions(options),
20
+ queues: /* @__PURE__ */ new Map()
21
+ } });
22
+ }
23
+ Object.defineProperties(KeyedQueue.prototype, {
24
+ [QUEUE_PROPERTY]: { value: QUEUE_NAME_KEYED },
25
+ active: {
26
+ enumerable: true,
27
+ get() {
28
+ return getStatus(this[QUEUE_SYMBOL], QUEUE_STATUS_ACTIVE);
29
+ }
30
+ },
31
+ add: { value: addKeyedQueue },
32
+ autostart: {
33
+ enumerable: true,
34
+ get() {
35
+ return this[QUEUE_SYMBOL].options.autostart;
36
+ }
37
+ },
38
+ clear: { value: clearQueues },
39
+ concurrency: {
40
+ enumerable: true,
41
+ get() {
42
+ return this[QUEUE_SYMBOL].options.concurrency;
43
+ }
44
+ },
45
+ empty: {
46
+ enumerable: true,
47
+ get() {
48
+ return getStatus(this[QUEUE_SYMBOL], QUEUE_STATUS_EMPTY);
49
+ }
50
+ },
51
+ full: {
52
+ enumerable: true,
53
+ get() {
54
+ return getStatus(this[QUEUE_SYMBOL], QUEUE_STATUS_FULL);
55
+ }
56
+ },
57
+ get: { value: getQueue },
58
+ items: {
59
+ enumerable: true,
60
+ get() {
61
+ return getQueueItems(this[QUEUE_SYMBOL]);
62
+ }
63
+ },
64
+ keys: {
65
+ enumerable: true,
66
+ get() {
67
+ return [...this[QUEUE_SYMBOL].queues.keys()];
68
+ }
69
+ },
70
+ maximum: {
71
+ enumerable: true,
72
+ get() {
73
+ return this[QUEUE_SYMBOL].options.maximum;
74
+ }
75
+ },
76
+ pause: { value: pauseQueues },
77
+ paused: {
78
+ enumerable: true,
79
+ get() {
80
+ return getStatus(this[QUEUE_SYMBOL], QUEUE_STATUS_PAUSED);
81
+ }
82
+ },
83
+ queues: {
84
+ enumerable: true,
85
+ get() {
86
+ return this[QUEUE_SYMBOL].queues.size;
87
+ }
88
+ },
89
+ remove: { value: removeQueue },
90
+ resume: { value: resumeQueues }
91
+ });
92
+ function Queue(callback, options, key) {
93
+ Object.defineProperty(this, QUEUE_SYMBOL, { value: {
94
+ callback,
95
+ key,
96
+ options,
97
+ handled: [],
98
+ id: 0,
99
+ items: [],
100
+ paused: !options.autostart,
101
+ runners: 0
102
+ } });
103
+ }
104
+ Object.defineProperties(Queue.prototype, {
105
+ [QUEUE_PROPERTY]: { value: QUEUE_NAME_QUEUE },
106
+ active: {
107
+ enumerable: true,
108
+ get() {
109
+ return this[QUEUE_SYMBOL].runners > 0;
110
+ }
111
+ },
112
+ add: { value: addToQueue },
113
+ autostart: {
114
+ enumerable: true,
115
+ get() {
116
+ return this[QUEUE_SYMBOL].options.autostart;
117
+ }
118
+ },
119
+ clear: { value: clearQueue },
120
+ concurrency: {
121
+ enumerable: true,
122
+ get() {
123
+ return this[QUEUE_SYMBOL].options.concurrency;
124
+ }
125
+ },
126
+ empty: {
127
+ enumerable: true,
128
+ get() {
129
+ return this[QUEUE_SYMBOL].items.length === 0;
130
+ }
131
+ },
132
+ full: {
133
+ enumerable: true,
134
+ get() {
135
+ const state = this[QUEUE_SYMBOL];
136
+ return state.options.maximum > 0 && state.items.length >= state.options.maximum;
137
+ }
138
+ },
139
+ maximum: {
140
+ enumerable: true,
141
+ get() {
142
+ return this[QUEUE_SYMBOL].options.maximum;
143
+ }
144
+ },
145
+ pause: { value: pauseQueue },
146
+ paused: {
147
+ enumerable: true,
148
+ get() {
149
+ return this[QUEUE_SYMBOL].paused;
150
+ }
151
+ },
152
+ remove: { value: removeQueued },
153
+ resume: { value: resumeQueue },
154
+ size: {
155
+ enumerable: true,
156
+ get() {
157
+ return this[QUEUE_SYMBOL].items.length;
158
+ }
159
+ }
160
+ });
161
+ function addKeyedQueue(key, parameters, signal) {
162
+ return getQueue.call(this, key, true).add(parameters, signal);
163
+ }
164
+ function addToQueue(parameters, signal) {
165
+ if (this.full) throw new QueueError(QUEUE_MESSAGE_MAXIMUM);
15
166
  const abortSignal = signal instanceof AbortSignal ? signal : void 0;
16
167
  if (abortSignal?.aborted ?? false) throw new Error(abortSignal?.reason);
168
+ const state = this[QUEUE_SYMBOL];
17
169
  const id = identify(state);
18
170
  let rejector;
19
171
  let resolver;
@@ -37,8 +189,8 @@ function addToQueue(instance, state, parameters, signal) {
37
189
  promise
38
190
  };
39
191
  }
40
- function clearQueue(state) {
41
- const items = state.items.splice(0);
192
+ function clearQueue() {
193
+ const items = this[QUEUE_SYMBOL].items.splice(0);
42
194
  const { length } = items;
43
195
  for (let index = 0; index < length; index += 1) {
44
196
  const { aborter, reject } = items[index];
@@ -46,63 +198,12 @@ function clearQueue(state) {
46
198
  reject(new QueueError(QUEUE_MESSAGE_CLEAR));
47
199
  }
48
200
  }
201
+ function clearQueues(key) {
202
+ handleQueues(this, QUEUE_HANDLE_CLEAR, key);
203
+ }
49
204
  function createQueue(callback, options, key) {
50
205
  if (typeof callback !== "function") throw new TypeError(QUEUE_MESSAGE_CALLBACK);
51
- const state = {
52
- callback,
53
- key,
54
- handled: [],
55
- id: 0,
56
- items: [],
57
- options: createQueueOptions(options),
58
- paused: !getBooleanOrDefault(options?.autostart, true),
59
- runners: 0
60
- };
61
- const instance = {
62
- add: (parameters, signal) => addToQueue(instance, state, parameters, signal),
63
- clear: () => clearQueue(state),
64
- pause: () => {
65
- state.paused = true;
66
- },
67
- remove: (id) => removeQueued(state, id),
68
- resume: () => resumeQueue(state)
69
- };
70
- Object.defineProperties(instance, {
71
- [QUEUE_PROPERTY]: { value: QUEUE_NAME_QUEUE },
72
- active: {
73
- enumerable: true,
74
- get: () => state.runners > 0
75
- },
76
- autostart: {
77
- enumerable: true,
78
- get: () => state.options.autostart
79
- },
80
- concurrency: {
81
- enumerable: true,
82
- get: () => state.options.concurrency
83
- },
84
- empty: {
85
- enumerable: true,
86
- get: () => state.items.length === 0
87
- },
88
- full: {
89
- enumerable: true,
90
- get: () => state.options.maximum > 0 && state.items.length >= state.options.maximum
91
- },
92
- maximum: {
93
- enumerable: true,
94
- get: () => state.options.maximum
95
- },
96
- paused: {
97
- enumerable: true,
98
- get: () => state.paused
99
- },
100
- size: {
101
- enumerable: true,
102
- get: () => state.items.length
103
- }
104
- });
105
- return Object.freeze(instance);
206
+ return new Queue(callback, createQueueOptions(options), key);
106
207
  }
107
208
  function createQueueOptions(input) {
108
209
  const options = typeof input === "object" && input != null ? input : {};
@@ -112,8 +213,9 @@ function createQueueOptions(input) {
112
213
  maximum: getNumberOrDefault(options.maximum, 0)
113
214
  };
114
215
  }
115
- function getQueue(state, key, add) {
216
+ function getQueue(key, add) {
116
217
  if (typeof key !== "string" || key.trim().length === 0) throw new TypeError(QUEUE_MESSAGE_KEY);
218
+ const state = this[QUEUE_SYMBOL];
117
219
  let queue = state.queues.get(key);
118
220
  if (queue == null && add === true) {
119
221
  queue = createQueue(state.callback, state.options, key);
@@ -145,12 +247,12 @@ function handleQueuedResult(item, error, result) {
145
247
  }
146
248
  item.resolve(result);
147
249
  }
148
- function handleQueues(state, type, key) {
250
+ function handleQueues(instance, type, key) {
149
251
  if (typeof key === "string") {
150
- getQueue(state, key)?.[type]();
252
+ getQueue.call(instance, key)?.[type]();
151
253
  return;
152
254
  }
153
- const queues = state.queues.values();
255
+ const queues = instance[QUEUE_SYMBOL].queues.values();
154
256
  for (const queue of queues) queue[type]();
155
257
  }
156
258
  function identify(state) {
@@ -176,78 +278,29 @@ function isQueue(value) {
176
278
  return isQueueInstance(QUEUE_NAME_QUEUE, value);
177
279
  }
178
280
  function isQueueInstance(name, value) {
179
- return typeof value === "object" && value != null && value[QUEUE_PROPERTY] === name;
281
+ return typeof value === "object" && value != null && QUEUE_PROPERTY in value && value[QUEUE_PROPERTY] === name;
180
282
  }
181
283
  function keyedQueue(callback, options) {
182
284
  if (typeof callback !== "function") throw new TypeError(QUEUE_MESSAGE_CALLBACK);
183
- const state = {
184
- callback,
185
- options: createQueueOptions(options),
186
- queues: /* @__PURE__ */ new Map()
187
- };
188
- const instance = {
189
- add: (key, parameters, signal) => getQueue(state, key, true).add(parameters, signal),
190
- clear: (key) => handleQueues(state, QUEUE_HANDLE_CLEAR, key),
191
- get: (key) => getQueue(state, key),
192
- pause: (key) => handleQueues(state, QUEUE_HANDLE_PAUSE, key),
193
- remove: (key, id) => removeQueue(state, key, id),
194
- resume: (key) => handleQueues(state, QUEUE_HANDLE_RESUME, key)
195
- };
196
- Object.defineProperties(instance, {
197
- [QUEUE_PROPERTY]: { value: QUEUE_NAME_KEYED },
198
- active: {
199
- enumerable: true,
200
- get: () => getStatus(state, QUEUE_STATUS_ACTIVE)
201
- },
202
- autostart: {
203
- enumerable: true,
204
- get: () => state.options.autostart
205
- },
206
- concurrency: {
207
- enumerable: true,
208
- get: () => state.options.concurrency
209
- },
210
- empty: {
211
- enumerable: true,
212
- get: () => getStatus(state, QUEUE_STATUS_EMPTY)
213
- },
214
- full: {
215
- enumerable: true,
216
- get: () => getStatus(state, QUEUE_STATUS_FULL)
217
- },
218
- items: {
219
- enumerable: true,
220
- get: () => getQueueItems(state)
221
- },
222
- keys: {
223
- enumerable: true,
224
- get: () => [...state.queues.keys()]
225
- },
226
- maximum: {
227
- enumerable: true,
228
- get: () => state.options.maximum
229
- },
230
- paused: {
231
- enumerable: true,
232
- get: () => getStatus(state, QUEUE_STATUS_PAUSED)
233
- },
234
- queues: {
235
- enumerable: true,
236
- get: () => state.queues.size
237
- }
238
- });
239
- return Object.freeze(instance);
285
+ return new KeyedQueue(callback, createQueueOptions(options));
286
+ }
287
+ function pauseQueue() {
288
+ this[QUEUE_SYMBOL].paused = true;
289
+ }
290
+ function pauseQueues(key) {
291
+ handleQueues(this, QUEUE_HANDLE_PAUSE, key);
240
292
  }
241
293
  function queue(callback, options) {
242
294
  return createQueue(callback, options);
243
295
  }
244
- function removeQueue(state, key, id) {
296
+ function removeQueue(key, id) {
297
+ const state = this[QUEUE_SYMBOL];
245
298
  if (key == null) {
246
- handleQueues(state, QUEUE_HANDLE_CLEAR);
299
+ handleQueues(this, QUEUE_HANDLE_CLEAR);
247
300
  state.queues.clear();
248
301
  return;
249
302
  }
250
- const queue = getQueue(state, key);
303
+ const queue = getQueue.call(this, key);
251
304
  if (queue == null) return;
252
305
  if (typeof id === "number") {
253
306
  queue.remove(id);
@@ -256,7 +309,8 @@ function removeQueue(state, key, id) {
256
309
  queue.clear();
257
310
  state.queues.delete(key);
258
311
  }
259
- function removeQueued(state, id) {
312
+ function removeQueued(id) {
313
+ const state = this[QUEUE_SYMBOL];
260
314
  const index = state.items.findIndex((item) => item.id === id);
261
315
  if (index > -1) {
262
316
  const { aborter, reject } = state.items.splice(index, 1)[0];
@@ -264,7 +318,8 @@ function removeQueued(state, id) {
264
318
  reject(new QueueError(QUEUE_MESSAGE_REMOVE));
265
319
  }
266
320
  }
267
- function resumeQueue(state) {
321
+ function resumeQueue() {
322
+ const state = this[QUEUE_SYMBOL];
268
323
  if (state.paused) {
269
324
  const handled = state.handled.splice(0);
270
325
  const { length } = handled;
@@ -274,6 +329,9 @@ function resumeQueue(state) {
274
329
  const length = Math.min(state.options.concurrency, state.items.length);
275
330
  for (let index = 0; index < length; index += 1) run(state);
276
331
  }
332
+ function resumeQueues(key) {
333
+ handleQueues(this, QUEUE_HANDLE_RESUME, key);
334
+ }
277
335
  async function run(state) {
278
336
  if (state.paused || state.runners >= state.options.concurrency) return;
279
337
  state.runners += 1;
@@ -301,7 +359,6 @@ async function run(state) {
301
359
  state.runners -= 1;
302
360
  }
303
361
  const QUEUE_ERROR_NAME = "QueueError";
304
- const QUEUE_PROPERTY = "$queue";
305
362
  const QUEUE_HANDLE_CLEAR = "clear";
306
363
  const QUEUE_HANDLE_PAUSE = "pause";
307
364
  const QUEUE_HANDLE_RESUME = "resume";
@@ -310,12 +367,11 @@ const QUEUE_MESSAGE_CLEAR = "Queue was cleared";
310
367
  const QUEUE_MESSAGE_KEY = "Key must be a non-empty string";
311
368
  const QUEUE_MESSAGE_MAXIMUM = "Queue has reached its maximum size";
312
369
  const QUEUE_MESSAGE_REMOVE = "Item removed from queue";
313
- const QUEUE_NAME_KEYED = "keyedQueue";
314
- const QUEUE_NAME_QUEUE = "queue";
315
370
  const QUEUE_STATUS_ACTIVE = "active";
316
371
  const QUEUE_STATUS_EMPTY = "empty";
317
372
  const QUEUE_STATUS_FULL = "full";
318
373
  const QUEUE_STATUS_PAUSED = "paused";
374
+ const QUEUE_SYMBOL = Symbol(QUEUE_PROPERTY);
319
375
  queue.keyed = keyedQueue;
320
376
  Object.defineProperty(queue, "keyed", { value: keyedQueue });
321
377
  //#endregion