@oscarpalmer/atoms 0.183.0 → 0.184.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.
package/dist/index.mjs CHANGED
@@ -2946,6 +2946,8 @@ function getNormalizeOptions(input) {
2946
2946
  }
2947
2947
  /**
2948
2948
  * Initialize a string normalizer
2949
+ *
2950
+ * Available as `initializeNormalizer` and `normalize.initialize`
2949
2951
  * @param options Normalization options
2950
2952
  * @returns Normalizer function
2951
2953
  */
@@ -3313,6 +3315,42 @@ const DIFF_FULL = "full";
3313
3315
  const DIFF_NONE = "none";
3314
3316
  const DIFF_PARTIAL = "partial";
3315
3317
  //#endregion
3318
+ //#region src/value/freeze.ts
3319
+ function freeze(value) {
3320
+ return freezeValue(value, /* @__PURE__ */ new WeakSet());
3321
+ }
3322
+ function freezeArray(array, references) {
3323
+ references.add(array);
3324
+ const { length } = array;
3325
+ for (let index = 0; index < length; index += 1) {
3326
+ const value = array[index];
3327
+ if (!references.has(value)) array[index] = freezeValue(array[index], references);
3328
+ }
3329
+ return Object.freeze(array);
3330
+ }
3331
+ function freezeFunction(fn, references) {
3332
+ references.add(fn);
3333
+ return Object.freeze(fn);
3334
+ }
3335
+ function freezeObject(object, references) {
3336
+ references.add(object);
3337
+ const keys = Object.keys(object);
3338
+ const { length } = keys;
3339
+ for (let index = 0; index < length; index += 1) {
3340
+ const key = keys[index];
3341
+ if (!references.has(object[key])) object[key] = freezeValue(object[key], references);
3342
+ }
3343
+ return Object.freeze(object);
3344
+ }
3345
+ function freezeValue(value, references) {
3346
+ switch (true) {
3347
+ case typeof value === "function": return freezeFunction(value, references);
3348
+ case Array.isArray(value): return freezeArray(value, references);
3349
+ case isPlainObject(value): return freezeObject(value, references);
3350
+ default: return value;
3351
+ }
3352
+ }
3353
+ //#endregion
3316
3354
  //#region src/internal/value/partial.ts
3317
3355
  function partial(value, providedKeys, omit) {
3318
3356
  if (typeof value !== "object" || value === null) return {};
@@ -3351,6 +3389,11 @@ function pick(value, keys) {
3351
3389
  }
3352
3390
  //#endregion
3353
3391
  //#region src/value/shake.ts
3392
+ /**
3393
+ * Shake an object, removing all keys with `undefined` values
3394
+ * @param value Object to shake
3395
+ * @returns Shaken object
3396
+ */
3354
3397
  function shake(value) {
3355
3398
  const shaken = {};
3356
3399
  if (isNonPlainObject(value)) return shaken;
@@ -3434,6 +3477,19 @@ function unsmush(value) {
3434
3477
  }
3435
3478
  //#endregion
3436
3479
  //#region src/value/merge.ts
3480
+ /**
3481
+ * Assign values from multiple arrays or objects to the first one
3482
+ * @param to Value to assign to
3483
+ * @param from Values to assign
3484
+ * @param options Assigning options
3485
+ * @returns Assigned value
3486
+ */
3487
+ function assign(to, from, options) {
3488
+ const actual = getMergeOptions(options);
3489
+ actual.assignValues = true;
3490
+ return mergeValues([to, ...from], actual, true);
3491
+ }
3492
+ assign.initialize = initializeAssigner;
3437
3493
  function getMergeOptions(options) {
3438
3494
  const actual = {
3439
3495
  assignValues: false,
@@ -3456,6 +3512,18 @@ function handleMerge(values, options) {
3456
3512
  return !Array.isArray(values) || values.length === 0 ? {} : mergeValues(values, options, true);
3457
3513
  }
3458
3514
  /**
3515
+ * Create an assigner with predefined options
3516
+ *
3517
+ * Available as `initializeAssigner` and `assign.initialize`
3518
+ * @param options Assigning options
3519
+ * @returns Assigner function
3520
+ */
3521
+ function initializeAssigner(options) {
3522
+ const actual = getMergeOptions(options);
3523
+ actual.assignValues = true;
3524
+ return ((to, from) => mergeValues([to, ...from], actual, true));
3525
+ }
3526
+ /**
3459
3527
  * Create a merger with predefined options
3460
3528
  *
3461
3529
  * Available as `initializeMerger` and `merge.initialize`
@@ -3469,6 +3537,7 @@ function initializeMerger(options) {
3469
3537
  function merge(values, options) {
3470
3538
  return handleMerge(values, getMergeOptions(options));
3471
3539
  }
3540
+ merge.assign = assign;
3472
3541
  merge.initialize = initializeMerger;
3473
3542
  function mergeObjects(values, options, prefix) {
3474
3543
  const { length } = values;
@@ -3495,6 +3564,42 @@ function mergeValues(values, options, validate, prefix) {
3495
3564
  return actual.length > 1 ? mergeObjects(actual, options, prefix) : actual[0] ?? {};
3496
3565
  }
3497
3566
  //#endregion
3567
+ //#region src/value/transform.ts
3568
+ function getTransformer(input) {
3569
+ if (typeof input === "function") return input;
3570
+ if (isNonPlainObject(input)) return;
3571
+ const keys = Object.keys(input);
3572
+ const { length } = keys;
3573
+ const transformer = {};
3574
+ for (let index = 0; index < length; index += 1) {
3575
+ const key = keys[index];
3576
+ const value = input[key];
3577
+ if (typeof value === "function") transformer[key] = value;
3578
+ }
3579
+ if (Object.keys(transformer).length > 0) return transformer;
3580
+ }
3581
+ function initializeTransformer(transform) {
3582
+ const transformer = getTransformer(transform);
3583
+ return (value) => transformValue(value, transformer);
3584
+ }
3585
+ function transform(value, transform) {
3586
+ return transformValue(value, getTransformer(transform));
3587
+ }
3588
+ transform.initialize = initializeTransformer;
3589
+ function transformValue(value, transformer) {
3590
+ if (isNonPlainObject(value)) return {};
3591
+ if (transformer == null) return value;
3592
+ const keys = Object.keys(value);
3593
+ const { length } = keys;
3594
+ for (let index = 0; index < length; index += 1) {
3595
+ const key = keys[index];
3596
+ const val = value[key];
3597
+ if (typeof transformer === "function") value[key] = transformer(key, val);
3598
+ else value[key] = transformer[key]?.(val) ?? val;
3599
+ }
3600
+ return value;
3601
+ }
3602
+ //#endregion
3498
3603
  //#region src/beacon.ts
3499
3604
  var Beacon = class {
3500
3605
  #options;
@@ -4837,11 +4942,161 @@ const TYPES = new Set([
4837
4942
  ]);
4838
4943
  //#endregion
4839
4944
  //#region src/queue.ts
4945
+ var KeyedQueue = class {
4946
+ #callback;
4947
+ #options;
4948
+ #queues = /* @__PURE__ */ new Map();
4949
+ /**
4950
+ * Is any queue active?
4951
+ */
4952
+ get active() {
4953
+ return this.#getStatus(STATUS_ACTIVE);
4954
+ }
4955
+ /**
4956
+ * Does the queue automatically start when the first item is added?
4957
+ */
4958
+ get autostart() {
4959
+ return this.#options.autostart;
4960
+ }
4961
+ /**
4962
+ * Maximum number of runners to process the queue concurrently
4963
+ */
4964
+ get concurrency() {
4965
+ return this.#options.concurrency;
4966
+ }
4967
+ /**
4968
+ * Are all queues empty?
4969
+ */
4970
+ get empty() {
4971
+ return this.#getStatus(STATUS_EMPTY);
4972
+ }
4973
+ /**
4974
+ * Are all queues full?
4975
+ */
4976
+ get full() {
4977
+ return this.#getStatus(STATUS_FULL);
4978
+ }
4979
+ /**
4980
+ * Number of items in all queues
4981
+ */
4982
+ get items() {
4983
+ const size = {};
4984
+ const queues = this.#queues.entries();
4985
+ for (const [key, queue] of queues) size[key] = queue.size;
4986
+ return size;
4987
+ }
4988
+ /**
4989
+ * Keys of all queues
4990
+ */
4991
+ get keys() {
4992
+ return [...this.#queues.keys()];
4993
+ }
4994
+ /**
4995
+ * Maximum number of items allowed in the queue
4996
+ */
4997
+ get maximum() {
4998
+ return this.#options.maximum;
4999
+ }
5000
+ /**
5001
+ * Are all queues paused?
5002
+ */
5003
+ get paused() {
5004
+ return this.#getStatus(STATUS_PAUSED);
5005
+ }
5006
+ /**
5007
+ * Number of queues
5008
+ */
5009
+ get queues() {
5010
+ return this.#queues.size;
5011
+ }
5012
+ constructor(callback, options) {
5013
+ this.#callback = callback;
5014
+ this.#options = options;
5015
+ }
5016
+ /**
5017
+ * Queue an item for a specific key
5018
+ * @param key Key to queue the item for
5019
+ * @param parameters Parameters to use when item runs
5020
+ * @param signal Optional signal to abort the item
5021
+ * @returns Queued item
5022
+ */
5023
+ add(key, parameters, signal) {
5024
+ return this.#getQueue(key, true).add(parameters, signal);
5025
+ }
5026
+ /**
5027
+ * Clear all items for a specific key _(or all items for all keys, if no key is provided)_
5028
+ * @param key Optional key to clear the queue for
5029
+ */
5030
+ clear(key) {
5031
+ this.#handleQueues(HANDLE_CLEAR, key);
5032
+ }
5033
+ /**
5034
+ * Get the queue for a specific key
5035
+ * @param key Key to get the queue for
5036
+ * @returns Queue for the key, or `undefined` if it doesn't exist
5037
+ */
5038
+ get(key) {
5039
+ return this.#getQueue(key);
5040
+ }
5041
+ /**
5042
+ * Pause the queue for a specific key _(or all queues, if no key is provided)_
5043
+ * @param key Optional key to pause the queue for
5044
+ */
5045
+ pause(key) {
5046
+ this.#handleQueues(HANDLE_PAUSE, key);
5047
+ }
5048
+ remove(key, id) {
5049
+ if (key == null) {
5050
+ this.#handleQueues(HANDLE_CLEAR);
5051
+ this.#queues.clear();
5052
+ return;
5053
+ }
5054
+ const queue = this.#getQueue(key);
5055
+ if (queue == null) return;
5056
+ if (typeof id === "number") {
5057
+ queue.remove(id);
5058
+ return;
5059
+ }
5060
+ queue.clear();
5061
+ this.#queues.delete(key);
5062
+ }
5063
+ /**
5064
+ * Resume the queue for a specific key _(or all queues, if no key is provided)_
5065
+ * @param key Optional key to resume the queue for
5066
+ */
5067
+ resume(key) {
5068
+ this.#handleQueues(HANDLE_RESUME, key);
5069
+ }
5070
+ #getQueue(key, add) {
5071
+ if (typeof key !== "string" || key.trim().length === 0) throw new TypeError(MESSAGE_KEY);
5072
+ let queue = this.#queues.get(key);
5073
+ if (queue == null && add === true) {
5074
+ queue = new Queue(this.#callback, this.#options, key);
5075
+ this.#queues.set(key, queue);
5076
+ }
5077
+ return queue;
5078
+ }
5079
+ #getStatus(status) {
5080
+ const queues = this.#queues.entries();
5081
+ const result = [];
5082
+ for (const [key, queue] of queues) if (queue[status]) result.push(key);
5083
+ return result;
5084
+ }
5085
+ #handleQueues(type, key) {
5086
+ if (typeof key === "string") {
5087
+ this.#getQueue(key)?.[type]();
5088
+ return;
5089
+ }
5090
+ const queues = this.#queues.values();
5091
+ for (const queue of queues) queue[type]();
5092
+ }
5093
+ };
4840
5094
  var Queue = class {
4841
5095
  #callback;
4842
5096
  #handled = [];
4843
5097
  #id = 0;
4844
5098
  #items = [];
5099
+ #key;
4845
5100
  #options;
4846
5101
  #paused;
4847
5102
  #runners = 0;
@@ -4893,8 +5148,9 @@ var Queue = class {
4893
5148
  get size() {
4894
5149
  return this.#items.length;
4895
5150
  }
4896
- constructor(callback, options) {
5151
+ constructor(callback, options, key) {
4897
5152
  this.#callback = callback;
5153
+ this.#key = key;
4898
5154
  this.#options = options;
4899
5155
  this.#paused = !options.autostart;
4900
5156
  }
@@ -4922,6 +5178,7 @@ var Queue = class {
4922
5178
  parameters,
4923
5179
  promise,
4924
5180
  abort: aborter,
5181
+ key: this.#key,
4925
5182
  reject: rejector,
4926
5183
  resolve: resolver,
4927
5184
  signal: abortSignal
@@ -4990,7 +5247,10 @@ var Queue = class {
4990
5247
  let error = false;
4991
5248
  let result;
4992
5249
  try {
4993
- if (!(item.signal?.aborted ?? false)) result = await this.#callback(...item.parameters);
5250
+ if (!(item.signal?.aborted ?? false)) {
5251
+ const parameters = item.key == null ? item.parameters : [item.key, ...item.parameters];
5252
+ result = await this.#callback(...parameters);
5253
+ }
4994
5254
  } catch (thrown) {
4995
5255
  error = true;
4996
5256
  result = thrown;
@@ -5037,17 +5297,30 @@ function handleResult$1(item, error, result, finished) {
5037
5297
  value: result
5038
5298
  });
5039
5299
  }
5300
+ function keyedQueue(callback, options) {
5301
+ if (typeof callback !== "function") throw new TypeError(MESSAGE_CALLBACK);
5302
+ return new KeyedQueue(callback, getOptions(options));
5303
+ }
5040
5304
  function queue(callback, options) {
5041
5305
  if (typeof callback !== "function") throw new TypeError(MESSAGE_CALLBACK);
5042
5306
  return new Queue(callback, getOptions(options));
5043
5307
  }
5308
+ queue.keyed = keyedQueue;
5044
5309
  const ERROR_NAME = "QueueError";
5045
5310
  const EVENT_NAME = "abort";
5046
5311
  const EVENT_OPTIONS = { once: true };
5312
+ const HANDLE_CLEAR = "clear";
5313
+ const HANDLE_PAUSE = "pause";
5314
+ const HANDLE_RESUME = "resume";
5047
5315
  const MESSAGE_CALLBACK = "A Queue requires a callback function";
5048
5316
  const MESSAGE_CLEAR = "Queue was cleared";
5317
+ const MESSAGE_KEY = "Key must be a non-empty string";
5049
5318
  const MESSAGE_MAXIMUM = "Queue has reached its maximum size";
5050
5319
  const MESSAGE_REMOVE = "Item removed from queue";
5320
+ const STATUS_ACTIVE = "active";
5321
+ const STATUS_EMPTY = "empty";
5322
+ const STATUS_FULL = "full";
5323
+ const STATUS_PAUSED = "paused";
5051
5324
  //#endregion
5052
5325
  //#region src/random.ts
5053
5326
  /**
@@ -5249,4 +5522,4 @@ var SizedSet = class extends Set {
5249
5522
  }
5250
5523
  };
5251
5524
  //#endregion
5252
- export { CancelablePromise, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError, QueueError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatten, floor, flow, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayPosition, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isSorted, isTypedArray, join, kalas, kebabCase, last, lastIndexOf, lastOrDefault, logger, lowerCase, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
5525
+ export { CancelablePromise, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayPosition, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isSorted, isTypedArray, join, kalas, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, logger, lowerCase, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, transform, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
package/dist/queue.d.mts CHANGED
@@ -1,6 +1,96 @@
1
1
  import { GenericAsyncCallback, GenericCallback } from "./models.mjs";
2
2
 
3
3
  //#region src/queue.d.ts
4
+ declare class KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, CallbackResult> {
5
+ #private;
6
+ /**
7
+ * Is any queue active?
8
+ */
9
+ get active(): string[];
10
+ /**
11
+ * Does the queue automatically start when the first item is added?
12
+ */
13
+ get autostart(): boolean;
14
+ /**
15
+ * Maximum number of runners to process the queue concurrently
16
+ */
17
+ get concurrency(): number;
18
+ /**
19
+ * Are all queues empty?
20
+ */
21
+ get empty(): string[];
22
+ /**
23
+ * Are all queues full?
24
+ */
25
+ get full(): string[];
26
+ /**
27
+ * Number of items in all queues
28
+ */
29
+ get items(): Record<string, number>;
30
+ /**
31
+ * Keys of all queues
32
+ */
33
+ get keys(): string[];
34
+ /**
35
+ * Maximum number of items allowed in the queue
36
+ */
37
+ get maximum(): number;
38
+ /**
39
+ * Are all queues paused?
40
+ */
41
+ get paused(): string[];
42
+ /**
43
+ * Number of queues
44
+ */
45
+ get queues(): number;
46
+ constructor(callback: GenericAsyncCallback, options: Required<QueueOptions>);
47
+ /**
48
+ * Queue an item for a specific key
49
+ * @param key Key to queue the item for
50
+ * @param parameters Parameters to use when item runs
51
+ * @param signal Optional signal to abort the item
52
+ * @returns Queued item
53
+ */
54
+ add(key: string, parameters: Tail<CallbackParameters>, signal?: AbortSignal): Queued<CallbackResult>;
55
+ /**
56
+ * Clear all items for a specific key _(or all items for all keys, if no key is provided)_
57
+ * @param key Optional key to clear the queue for
58
+ */
59
+ clear(key?: string): void;
60
+ /**
61
+ * Get the queue for a specific key
62
+ * @param key Key to get the queue for
63
+ * @returns Queue for the key, or `undefined` if it doesn't exist
64
+ */
65
+ get(key: string): Queue<Tail<CallbackParameters>, CallbackResult> | undefined;
66
+ /**
67
+ * Pause the queue for a specific key _(or all queues, if no key is provided)_
68
+ * @param key Optional key to pause the queue for
69
+ */
70
+ pause(key?: string): void;
71
+ /**
72
+ * Remove a specific item for a specific key
73
+ * @param key Key to remove the item for
74
+ * @param id ID of the item to remove
75
+ */
76
+ remove(key: string, id: number): void;
77
+ /**
78
+ * Remove a queue and its items for a specific key
79
+ *
80
+ * _(To remove all items for a specific key, use `clear()` instead)_
81
+ * @param key Key to remove the queue for
82
+ */
83
+ remove(key: string): void;
84
+ /**
85
+ * Remove all queues and their items
86
+ */
87
+ remove(): void;
88
+ /**
89
+ * Resume the queue for a specific key _(or all queues, if no key is provided)_
90
+ * @param key Optional key to resume the queue for
91
+ */
92
+ resume(key?: string): void;
93
+ }
4
94
  declare class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, CallbackResult> {
5
95
  #private;
6
96
  /**
@@ -35,7 +125,7 @@ declare class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>,
35
125
  * Number of items in the queue
36
126
  */
37
127
  get size(): number;
38
- constructor(callback: GenericAsyncCallback, options: Required<QueueOptions>);
128
+ constructor(callback: GenericAsyncCallback, options: Required<QueueOptions>, key?: string);
39
129
  /**
40
130
  * Add an item to the queue
41
131
  * @param parameters Parameters to use when item runs
@@ -101,13 +191,15 @@ type QueuedResult<Value> = {
101
191
  */
102
192
  value: Value;
103
193
  };
194
+ type Tail<Values extends any[]> = Values extends [infer _, ...infer Rest] ? Rest : never;
195
+ declare function keyedQueue<Callback extends GenericAsyncCallback>(callback: Callback, options?: QueueOptions): KeyedQueue<Parameters<Callback>, Awaited<ReturnType<Callback>>>;
104
196
  /**
105
197
  * Create a queue for an asynchronous callback function
106
198
  * @param callback Callback function for queued items
107
199
  * @param options Queue options
108
200
  * @returns Queue instance
109
201
  */
110
- declare function queue<Callback extends GenericAsyncCallback>(callback: Callback, options?: QueueOptions): Queue<Parameters<Callback>, Awaited<ReturnType<Callback>>>;
202
+ declare function queue<Callback extends (key: string, ...parameters: any[]) => Promise<void>>(callback: Callback, options?: QueueOptions): Queue<Parameters<Callback>, Awaited<ReturnType<Callback>>>;
111
203
  /**
112
204
  * Create a queue for an asynchronous callback function
113
205
  * @param callback Callback function for queued items
@@ -115,5 +207,8 @@ declare function queue<Callback extends GenericAsyncCallback>(callback: Callback
115
207
  * @returns Queue instance
116
208
  */
117
209
  declare function queue<Callback extends GenericCallback>(callback: Callback, options?: QueueOptions): Queue<Parameters<Callback>, ReturnType<Callback>>;
210
+ declare namespace queue {
211
+ var keyed: typeof keyedQueue;
212
+ }
118
213
  //#endregion
119
- export { type Queue, QueueError, type QueueOptions, type Queued, type QueuedResult, queue };
214
+ export { type KeyedQueue, type Queue, type QueueError, type QueueOptions, type Queued, type QueuedResult, keyedQueue, queue };