@oscarpalmer/atoms 0.192.0 → 0.193.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.d.mts +25 -23
- package/dist/index.mjs +30 -31
- package/dist/queue.d.mts +25 -23
- package/dist/queue.mjs +27 -28
- package/package.json +1 -1
- package/src/queue.ts +92 -92
package/dist/index.d.mts
CHANGED
|
@@ -7636,7 +7636,7 @@ declare function toQuery(parameters: PlainObject): string;
|
|
|
7636
7636
|
/**
|
|
7637
7637
|
* A queue that can be used to manage (a)synchronous tasks with a specific key
|
|
7638
7638
|
*/
|
|
7639
|
-
type KeyedQueue<CallbackParameters extends Parameters<
|
|
7639
|
+
type KeyedQueue<Callback extends GenericCallback | GenericAsyncCallback, CallbackParameters extends unknown[] = Parameters<Callback>> = {
|
|
7640
7640
|
/**
|
|
7641
7641
|
* Get keys of all active queues
|
|
7642
7642
|
*/
|
|
@@ -7685,7 +7685,7 @@ type KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, Cal
|
|
|
7685
7685
|
* @param signal Optional signal to abort the item
|
|
7686
7686
|
* @returns Queued item
|
|
7687
7687
|
*/
|
|
7688
|
-
add(key: string, parameters: Tail<CallbackParameters>, signal?: AbortSignal): Queued<
|
|
7688
|
+
add(key: string, parameters: Tail<CallbackParameters>, signal?: AbortSignal): Queued<ReturnType<Callback>>;
|
|
7689
7689
|
/**
|
|
7690
7690
|
* Clear all items for a specific key _(or all items for all keys, if no key is provided)_
|
|
7691
7691
|
*
|
|
@@ -7698,7 +7698,7 @@ type KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, Cal
|
|
|
7698
7698
|
* @param key Key to get the queue for
|
|
7699
7699
|
* @returns Queue for the key, or `undefined` if it doesn't exist
|
|
7700
7700
|
*/
|
|
7701
|
-
get(key: string): Queue<Tail<CallbackParameters
|
|
7701
|
+
get(key: string): Queue<Callback, Tail<CallbackParameters>> | undefined;
|
|
7702
7702
|
/**
|
|
7703
7703
|
* Pause the queue for a specific key _(or all queues, if no key is provided)_
|
|
7704
7704
|
*
|
|
@@ -7734,7 +7734,7 @@ type KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, Cal
|
|
|
7734
7734
|
/**
|
|
7735
7735
|
* A queue that can be used to manage (a)synchronous tasks
|
|
7736
7736
|
*/
|
|
7737
|
-
type Queue<CallbackParameters extends Parameters<
|
|
7737
|
+
type Queue<Callback extends GenericCallback | GenericAsyncCallback, CallbackParameters extends unknown[] = Parameters<Callback>> = {
|
|
7738
7738
|
/**
|
|
7739
7739
|
* Is the queue active?
|
|
7740
7740
|
*/
|
|
@@ -7774,7 +7774,7 @@ type Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, Callback
|
|
|
7774
7774
|
* @param signal Optional signal to abort the item
|
|
7775
7775
|
* @returns Queued item
|
|
7776
7776
|
*/
|
|
7777
|
-
add(parameters: CallbackParameters, signal?: AbortSignal): Queued<
|
|
7777
|
+
add(parameters: CallbackParameters, signal?: AbortSignal): Queued<ReturnType<Callback>>;
|
|
7778
7778
|
/**
|
|
7779
7779
|
* Remove and reject all items in the queue
|
|
7780
7780
|
*/
|
|
@@ -7828,17 +7828,7 @@ type Queued<Value> = {
|
|
|
7828
7828
|
/**
|
|
7829
7829
|
* Queued promise
|
|
7830
7830
|
*/
|
|
7831
|
-
readonly promise: Promise<
|
|
7832
|
-
};
|
|
7833
|
-
type QueuedResult<Value> = {
|
|
7834
|
-
/**
|
|
7835
|
-
* Has the queue finished processing all items?
|
|
7836
|
-
*/
|
|
7837
|
-
finished: boolean;
|
|
7838
|
-
/**
|
|
7839
|
-
* Result for the queued promise
|
|
7840
|
-
*/
|
|
7841
|
-
value: Value extends Promise<infer Result> ? Result : Value;
|
|
7831
|
+
readonly promise: Promise<Value extends Promise<infer Result> ? Result : Value>;
|
|
7842
7832
|
};
|
|
7843
7833
|
type Tail<Values extends any[]> = Values extends [infer _, ...infer Rest] ? Rest : never;
|
|
7844
7834
|
/**
|
|
@@ -7847,17 +7837,29 @@ type Tail<Values extends any[]> = Values extends [infer _, ...infer Rest] ? Rest
|
|
|
7847
7837
|
* @param value Value to check
|
|
7848
7838
|
* @returns `true` if the value is a keyed queue, otherwise `false`
|
|
7849
7839
|
*/
|
|
7850
|
-
declare function isKeyedQueue(value: unknown): value is KeyedQueue<
|
|
7840
|
+
declare function isKeyedQueue(value: unknown): value is KeyedQueue<GenericAsyncCallback>;
|
|
7851
7841
|
/**
|
|
7852
7842
|
* Is the value a queue?
|
|
7853
7843
|
*
|
|
7854
7844
|
* @param value Value to check
|
|
7855
7845
|
* @returns `true` if the value is a queue, otherwise `false`
|
|
7856
7846
|
*/
|
|
7857
|
-
declare function isQueue(value: unknown): value is Queue<
|
|
7847
|
+
declare function isQueue(value: unknown): value is Queue<GenericCallback | GenericAsyncCallback>;
|
|
7858
7848
|
declare function isQueueInstance<Instance>(name: string, value: unknown): value is Instance;
|
|
7859
|
-
|
|
7860
|
-
|
|
7849
|
+
/**
|
|
7850
|
+
* Create a keyed queue for an asynchronous callback function, where each key has its own queue
|
|
7851
|
+
*
|
|
7852
|
+
* @param callback Callback function for queued items
|
|
7853
|
+
* @param options Queue options
|
|
7854
|
+
*/
|
|
7855
|
+
declare function keyedQueue<Callback extends (key: string, ...parameters: any[]) => Promise<void>>(callback: Callback, options?: QueueOptions): KeyedQueue<Callback, Parameters<Callback>>;
|
|
7856
|
+
/**
|
|
7857
|
+
* Create a keyed queue for an asynchronous callback function, where each key has its own queue
|
|
7858
|
+
*
|
|
7859
|
+
* @param callback Callback function for queued items
|
|
7860
|
+
* @param options Queue options
|
|
7861
|
+
*/
|
|
7862
|
+
declare function keyedQueue<Callback extends (key: string, ...parameters: any[]) => void>(callback: Callback, options?: QueueOptions): KeyedQueue<Callback, Parameters<Callback>>;
|
|
7861
7863
|
/**
|
|
7862
7864
|
* Create a queue for an asynchronous callback function
|
|
7863
7865
|
*
|
|
@@ -7865,7 +7867,7 @@ declare function keyedQueue<Callback extends (key: string, ...parameters: any[])
|
|
|
7865
7867
|
* @param options Queue options
|
|
7866
7868
|
* @returns Queue instance
|
|
7867
7869
|
*/
|
|
7868
|
-
declare function queue<Callback extends GenericAsyncCallback>(callback: Callback, options?: QueueOptions): Queue<
|
|
7870
|
+
declare function queue<Callback extends GenericAsyncCallback>(callback: Callback, options?: QueueOptions): Queue<Callback, Parameters<Callback>>;
|
|
7869
7871
|
/**
|
|
7870
7872
|
* Create a queue for a synchronous callback function
|
|
7871
7873
|
*
|
|
@@ -7873,7 +7875,7 @@ declare function queue<Callback extends GenericAsyncCallback>(callback: Callback
|
|
|
7873
7875
|
* @param options Queue options
|
|
7874
7876
|
* @returns Queue instance
|
|
7875
7877
|
*/
|
|
7876
|
-
declare function queue<Callback extends GenericCallback>(callback: Callback, options?: QueueOptions): Queue<
|
|
7878
|
+
declare function queue<Callback extends GenericCallback>(callback: Callback, options?: QueueOptions): Queue<Callback, Parameters<Callback>>;
|
|
7877
7879
|
declare namespace queue {
|
|
7878
7880
|
var keyed: typeof keyedQueue;
|
|
7879
7881
|
}
|
|
@@ -8508,4 +8510,4 @@ declare class SizedSet<Value = unknown> extends Set<Value> {
|
|
|
8508
8510
|
get(value: Value, update?: boolean): Value | undefined;
|
|
8509
8511
|
}
|
|
8510
8512
|
//#endregion
|
|
8511
|
-
export { AnyResult, ArrayComparison, ArrayComparisonSorter, ArrayKeySorter, ArrayOrPlainObject, ArrayValueSorter, AssertProperty, Asserter, AssignOptions, Assigner, AsyncCancelableCallback, AttemptFlow, AttemptFlowPromise, Beacon, BeaconOptions, BuiltIns, CancelableCallback, CancelablePromise, type Color, type ColorType, type ColorTypeExtended, type ColorValue, type ColorValueExtended, Constructor, DiffOptions, DiffResult, DiffValue, EqualOptions, Err, EventPosition, Events, ExtendedErr, ExtendedResult, Flow, FlowPromise, Frozen, FulfilledPromise, Fuzzy, FuzzyConfiguration, FuzzyOptions, FuzzyResult, FuzzySearchOptions, GenericAsyncCallback, GenericCallback, type HSLAColor, type HSLColor, type HWBAColor, type HWBColor, Herald, Key$1 as Key, KeyedQueue, KeyedValue, Logger, type Memoized, type MemoizedOptions, MergeOptions, Merger, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NormalizeOptions, Normalizer, NumericalKeys, NumericalValues, Observable, Ok, OnceAsyncCallback, OnceCallback, 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, PlainObject, Primitive, PromiseData, PromiseHandlers, PromiseOptions, PromiseParameters, PromiseStrategy, PromiseTimeoutError, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValue, PromisesValues, Queue, QueueError, QueueOptions, Queued,
|
|
8513
|
+
export { AnyResult, ArrayComparison, ArrayComparisonSorter, ArrayKeySorter, ArrayOrPlainObject, ArrayValueSorter, AssertProperty, Asserter, AssignOptions, Assigner, AsyncCancelableCallback, AttemptFlow, AttemptFlowPromise, Beacon, BeaconOptions, BuiltIns, CancelableCallback, CancelablePromise, type Color, type ColorType, type ColorTypeExtended, type ColorValue, type ColorValueExtended, Constructor, DiffOptions, DiffResult, DiffValue, EqualOptions, Err, EventPosition, Events, ExtendedErr, ExtendedResult, Flow, FlowPromise, Frozen, FulfilledPromise, Fuzzy, FuzzyConfiguration, FuzzyOptions, FuzzyResult, FuzzySearchOptions, GenericAsyncCallback, GenericCallback, type HSLAColor, type HSLColor, type HWBAColor, type HWBColor, Herald, Key$1 as Key, KeyedQueue, KeyedValue, Logger, type Memoized, type MemoizedOptions, MergeOptions, Merger, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NormalizeOptions, Normalizer, NumericalKeys, NumericalValues, Observable, Ok, OnceAsyncCallback, OnceCallback, 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, PlainObject, Primitive, PromiseData, PromiseHandlers, PromiseOptions, PromiseParameters, PromiseStrategy, PromiseTimeoutError, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValue, PromisesValues, Queue, QueueError, QueueOptions, Queued, type RGBAColor, type RGBColor, RejectedPromise, RequiredKeys, Result, ResultMatch, RetryError, RetryOptions, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, type Shaken, Simplify, SizedMap, SizedSet, type Smushed, SortDirection, Sorter, type Subscription, TemplateOptions, type TimedLogger, ToString, Transformer, TypedArray, UnionToIntersection, type Unsmushed, Unsubscriber, UnwrapValue, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, 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, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isBeacon, isColor, isConstructor, isEmpty, isError, isEvents, isFrozen, isFulfilled, isHerald, isHexColor, isHslColor, isHslaColor, isHwbColor, isHwbaColor, isInstanceOf, isKey, isKeyedQueue, isLogger, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isObservable, isOk, isPlainObject, isPrimitive, isQueue, isQueueInstance, isRejected, isResult, isRgbColor, isRgbaColor, isSorted, isSubscription, isTimedLogger, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, 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, rgbToHwb, rgbToHwba, 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/index.mjs
CHANGED
|
@@ -2816,7 +2816,7 @@ function getHandler(input) {
|
|
|
2816
2816
|
throw new TypeError(MESSAGE_HANDLER);
|
|
2817
2817
|
}
|
|
2818
2818
|
}
|
|
2819
|
-
function getItems(items) {
|
|
2819
|
+
function getItems$1(items) {
|
|
2820
2820
|
return items.sort((first, second) => first.haystack.localeCompare(second.haystack)).map(({ item }) => item);
|
|
2821
2821
|
}
|
|
2822
2822
|
function getFuzzyOptions(input, state) {
|
|
@@ -2949,10 +2949,10 @@ function search(items, strings, input, options) {
|
|
|
2949
2949
|
for (let index = 0; index < length; index += 1) {
|
|
2950
2950
|
const key = keys[index];
|
|
2951
2951
|
if (maxScore - key > options.tolerance) break;
|
|
2952
|
-
similar.push(...getItems(scored[key]));
|
|
2952
|
+
similar.push(...getItems$1(scored[key]));
|
|
2953
2953
|
}
|
|
2954
2954
|
}
|
|
2955
|
-
result.exact = getItems(options.limit == null ? exact : exact.slice(0, options.limit));
|
|
2955
|
+
result.exact = getItems$1(options.limit == null ? exact : exact.slice(0, options.limit));
|
|
2956
2956
|
if (options.limit == null) result.similar = similar;
|
|
2957
2957
|
else result.similar = similar.slice(0, options.limit - result.exact.length);
|
|
2958
2958
|
return result;
|
|
@@ -5877,6 +5877,12 @@ function createQueue(callback, options, key) {
|
|
|
5877
5877
|
function getBooleanOrDefault(value, defaultValue) {
|
|
5878
5878
|
return typeof value === "boolean" ? value : defaultValue;
|
|
5879
5879
|
}
|
|
5880
|
+
function getItems(state) {
|
|
5881
|
+
const size = {};
|
|
5882
|
+
const queues = state.queues.entries();
|
|
5883
|
+
for (const [key, queue] of queues) size[key] = queue.size;
|
|
5884
|
+
return size;
|
|
5885
|
+
}
|
|
5880
5886
|
function getOptions(input) {
|
|
5881
5887
|
const options = typeof input === "object" && input != null ? input : {};
|
|
5882
5888
|
return {
|
|
@@ -5900,7 +5906,7 @@ function getStatus(state, status) {
|
|
|
5900
5906
|
for (const [key, queue] of queues) if (queue[status]) result.push(key);
|
|
5901
5907
|
return result;
|
|
5902
5908
|
}
|
|
5903
|
-
function handleQueuedResult(item, error, result
|
|
5909
|
+
function handleQueuedResult(item, error, result) {
|
|
5904
5910
|
item.signal?.removeEventListener(EVENT_NAME, item.abort);
|
|
5905
5911
|
if (item.signal?.aborted ?? false) {
|
|
5906
5912
|
item.reject();
|
|
@@ -5910,10 +5916,7 @@ function handleQueuedResult(item, error, result, finished) {
|
|
|
5910
5916
|
item.reject(result);
|
|
5911
5917
|
return;
|
|
5912
5918
|
}
|
|
5913
|
-
item.resolve(
|
|
5914
|
-
finished,
|
|
5915
|
-
value: result
|
|
5916
|
-
});
|
|
5919
|
+
item.resolve(result);
|
|
5917
5920
|
}
|
|
5918
5921
|
function handleQueues(state, type, key) {
|
|
5919
5922
|
if (typeof key === "string") {
|
|
@@ -5964,21 +5967,7 @@ function keyedQueue(callback, options) {
|
|
|
5964
5967
|
pause: (key) => {
|
|
5965
5968
|
handleQueues(state, HANDLE_PAUSE, key);
|
|
5966
5969
|
},
|
|
5967
|
-
remove: (key, id) =>
|
|
5968
|
-
if (key == null) {
|
|
5969
|
-
handleQueues(state, HANDLE_CLEAR);
|
|
5970
|
-
state.queues.clear();
|
|
5971
|
-
return;
|
|
5972
|
-
}
|
|
5973
|
-
const queue = getQueue(state, key);
|
|
5974
|
-
if (queue == null) return;
|
|
5975
|
-
if (typeof id === "number") {
|
|
5976
|
-
queue.remove(id);
|
|
5977
|
-
return;
|
|
5978
|
-
}
|
|
5979
|
-
queue.clear();
|
|
5980
|
-
state.queues.delete(key);
|
|
5981
|
-
},
|
|
5970
|
+
remove: (key, id) => removeQueue(state, key, id),
|
|
5982
5971
|
resume: (key) => {
|
|
5983
5972
|
handleQueues(state, HANDLE_RESUME, key);
|
|
5984
5973
|
}
|
|
@@ -6010,12 +5999,7 @@ function keyedQueue(callback, options) {
|
|
|
6010
5999
|
},
|
|
6011
6000
|
items: {
|
|
6012
6001
|
enumerable: true,
|
|
6013
|
-
get: () =>
|
|
6014
|
-
const size = {};
|
|
6015
|
-
const queues = state.queues.entries();
|
|
6016
|
-
for (const [key, queue] of queues) size[key] = queue.size;
|
|
6017
|
-
return size;
|
|
6018
|
-
}
|
|
6002
|
+
get: () => getItems(state)
|
|
6019
6003
|
},
|
|
6020
6004
|
keys: {
|
|
6021
6005
|
enumerable: true,
|
|
@@ -6040,6 +6024,21 @@ function queue(callback, options) {
|
|
|
6040
6024
|
return createQueue(callback, options);
|
|
6041
6025
|
}
|
|
6042
6026
|
queue.keyed = keyedQueue;
|
|
6027
|
+
function removeQueue(state, key, id) {
|
|
6028
|
+
if (key == null) {
|
|
6029
|
+
handleQueues(state, HANDLE_CLEAR);
|
|
6030
|
+
state.queues.clear();
|
|
6031
|
+
return;
|
|
6032
|
+
}
|
|
6033
|
+
const queue = getQueue(state, key);
|
|
6034
|
+
if (queue == null) return;
|
|
6035
|
+
if (typeof id === "number") {
|
|
6036
|
+
queue.remove(id);
|
|
6037
|
+
return;
|
|
6038
|
+
}
|
|
6039
|
+
queue.clear();
|
|
6040
|
+
state.queues.delete(key);
|
|
6041
|
+
}
|
|
6043
6042
|
async function run(state) {
|
|
6044
6043
|
if (state.paused || state.runners >= state.options.concurrency) return;
|
|
6045
6044
|
state.runners += 1;
|
|
@@ -6059,11 +6058,11 @@ async function run(state) {
|
|
|
6059
6058
|
if (state.paused) {
|
|
6060
6059
|
const paused = item;
|
|
6061
6060
|
state.handled.push(() => {
|
|
6062
|
-
handleQueuedResult(paused, error, result
|
|
6061
|
+
handleQueuedResult(paused, error, result);
|
|
6063
6062
|
});
|
|
6064
6063
|
break;
|
|
6065
6064
|
}
|
|
6066
|
-
handleQueuedResult(item, error, result
|
|
6065
|
+
handleQueuedResult(item, error, result);
|
|
6067
6066
|
item = state.items.shift();
|
|
6068
6067
|
}
|
|
6069
6068
|
state.runners -= 1;
|
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<CallbackParameters extends Parameters<
|
|
6
|
+
type KeyedQueue<Callback extends GenericCallback | GenericAsyncCallback, CallbackParameters extends unknown[] = Parameters<Callback>> = {
|
|
7
7
|
/**
|
|
8
8
|
* Get keys of all active queues
|
|
9
9
|
*/
|
|
@@ -52,7 +52,7 @@ type KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, Cal
|
|
|
52
52
|
* @param signal Optional signal to abort the item
|
|
53
53
|
* @returns Queued item
|
|
54
54
|
*/
|
|
55
|
-
add(key: string, parameters: Tail<CallbackParameters>, signal?: AbortSignal): Queued<
|
|
55
|
+
add(key: string, parameters: Tail<CallbackParameters>, signal?: AbortSignal): Queued<ReturnType<Callback>>;
|
|
56
56
|
/**
|
|
57
57
|
* Clear all items for a specific key _(or all items for all keys, if no key is provided)_
|
|
58
58
|
*
|
|
@@ -65,7 +65,7 @@ type KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, Cal
|
|
|
65
65
|
* @param key Key to get the queue for
|
|
66
66
|
* @returns Queue for the key, or `undefined` if it doesn't exist
|
|
67
67
|
*/
|
|
68
|
-
get(key: string): Queue<Tail<CallbackParameters
|
|
68
|
+
get(key: string): Queue<Callback, Tail<CallbackParameters>> | undefined;
|
|
69
69
|
/**
|
|
70
70
|
* Pause the queue for a specific key _(or all queues, if no key is provided)_
|
|
71
71
|
*
|
|
@@ -101,7 +101,7 @@ type KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, Cal
|
|
|
101
101
|
/**
|
|
102
102
|
* A queue that can be used to manage (a)synchronous tasks
|
|
103
103
|
*/
|
|
104
|
-
type Queue<CallbackParameters extends Parameters<
|
|
104
|
+
type Queue<Callback extends GenericCallback | GenericAsyncCallback, CallbackParameters extends unknown[] = Parameters<Callback>> = {
|
|
105
105
|
/**
|
|
106
106
|
* Is the queue active?
|
|
107
107
|
*/
|
|
@@ -141,7 +141,7 @@ type Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, Callback
|
|
|
141
141
|
* @param signal Optional signal to abort the item
|
|
142
142
|
* @returns Queued item
|
|
143
143
|
*/
|
|
144
|
-
add(parameters: CallbackParameters, signal?: AbortSignal): Queued<
|
|
144
|
+
add(parameters: CallbackParameters, signal?: AbortSignal): Queued<ReturnType<Callback>>;
|
|
145
145
|
/**
|
|
146
146
|
* Remove and reject all items in the queue
|
|
147
147
|
*/
|
|
@@ -195,17 +195,7 @@ type Queued<Value> = {
|
|
|
195
195
|
/**
|
|
196
196
|
* Queued promise
|
|
197
197
|
*/
|
|
198
|
-
readonly promise: Promise<
|
|
199
|
-
};
|
|
200
|
-
type QueuedResult<Value> = {
|
|
201
|
-
/**
|
|
202
|
-
* Has the queue finished processing all items?
|
|
203
|
-
*/
|
|
204
|
-
finished: boolean;
|
|
205
|
-
/**
|
|
206
|
-
* Result for the queued promise
|
|
207
|
-
*/
|
|
208
|
-
value: Value extends Promise<infer Result> ? Result : Value;
|
|
198
|
+
readonly promise: Promise<Value extends Promise<infer Result> ? Result : Value>;
|
|
209
199
|
};
|
|
210
200
|
type Tail<Values extends any[]> = Values extends [infer _, ...infer Rest] ? Rest : never;
|
|
211
201
|
/**
|
|
@@ -214,17 +204,29 @@ type Tail<Values extends any[]> = Values extends [infer _, ...infer Rest] ? Rest
|
|
|
214
204
|
* @param value Value to check
|
|
215
205
|
* @returns `true` if the value is a keyed queue, otherwise `false`
|
|
216
206
|
*/
|
|
217
|
-
declare function isKeyedQueue(value: unknown): value is KeyedQueue<
|
|
207
|
+
declare function isKeyedQueue(value: unknown): value is KeyedQueue<GenericAsyncCallback>;
|
|
218
208
|
/**
|
|
219
209
|
* Is the value a queue?
|
|
220
210
|
*
|
|
221
211
|
* @param value Value to check
|
|
222
212
|
* @returns `true` if the value is a queue, otherwise `false`
|
|
223
213
|
*/
|
|
224
|
-
declare function isQueue(value: unknown): value is Queue<
|
|
214
|
+
declare function isQueue(value: unknown): value is Queue<GenericCallback | GenericAsyncCallback>;
|
|
225
215
|
declare function isQueueInstance<Instance>(name: string, value: unknown): value is Instance;
|
|
226
|
-
|
|
227
|
-
|
|
216
|
+
/**
|
|
217
|
+
* Create a keyed queue for an asynchronous callback function, where each key has its own queue
|
|
218
|
+
*
|
|
219
|
+
* @param callback Callback function for queued items
|
|
220
|
+
* @param options Queue options
|
|
221
|
+
*/
|
|
222
|
+
declare function keyedQueue<Callback extends (key: string, ...parameters: any[]) => Promise<void>>(callback: Callback, options?: QueueOptions): KeyedQueue<Callback, Parameters<Callback>>;
|
|
223
|
+
/**
|
|
224
|
+
* Create a keyed queue for an asynchronous callback function, where each key has its own queue
|
|
225
|
+
*
|
|
226
|
+
* @param callback Callback function for queued items
|
|
227
|
+
* @param options Queue options
|
|
228
|
+
*/
|
|
229
|
+
declare function keyedQueue<Callback extends (key: string, ...parameters: any[]) => void>(callback: Callback, options?: QueueOptions): KeyedQueue<Callback, Parameters<Callback>>;
|
|
228
230
|
/**
|
|
229
231
|
* Create a queue for an asynchronous callback function
|
|
230
232
|
*
|
|
@@ -232,7 +234,7 @@ declare function keyedQueue<Callback extends (key: string, ...parameters: any[])
|
|
|
232
234
|
* @param options Queue options
|
|
233
235
|
* @returns Queue instance
|
|
234
236
|
*/
|
|
235
|
-
declare function queue<Callback extends GenericAsyncCallback>(callback: Callback, options?: QueueOptions): Queue<
|
|
237
|
+
declare function queue<Callback extends GenericAsyncCallback>(callback: Callback, options?: QueueOptions): Queue<Callback, Parameters<Callback>>;
|
|
236
238
|
/**
|
|
237
239
|
* Create a queue for a synchronous callback function
|
|
238
240
|
*
|
|
@@ -240,9 +242,9 @@ declare function queue<Callback extends GenericAsyncCallback>(callback: Callback
|
|
|
240
242
|
* @param options Queue options
|
|
241
243
|
* @returns Queue instance
|
|
242
244
|
*/
|
|
243
|
-
declare function queue<Callback extends GenericCallback>(callback: Callback, options?: QueueOptions): Queue<
|
|
245
|
+
declare function queue<Callback extends GenericCallback>(callback: Callback, options?: QueueOptions): Queue<Callback, Parameters<Callback>>;
|
|
244
246
|
declare namespace queue {
|
|
245
247
|
var keyed: typeof keyedQueue;
|
|
246
248
|
}
|
|
247
249
|
//#endregion
|
|
248
|
-
export { KeyedQueue, Queue, QueueError, QueueOptions, Queued,
|
|
250
|
+
export { KeyedQueue, Queue, QueueError, QueueOptions, Queued, isKeyedQueue, isQueue, isQueueInstance, keyedQueue, queue };
|
package/dist/queue.mjs
CHANGED
|
@@ -125,6 +125,12 @@ function createQueue(callback, options, key) {
|
|
|
125
125
|
function getBooleanOrDefault(value, defaultValue) {
|
|
126
126
|
return typeof value === "boolean" ? value : defaultValue;
|
|
127
127
|
}
|
|
128
|
+
function getItems(state) {
|
|
129
|
+
const size = {};
|
|
130
|
+
const queues = state.queues.entries();
|
|
131
|
+
for (const [key, queue] of queues) size[key] = queue.size;
|
|
132
|
+
return size;
|
|
133
|
+
}
|
|
128
134
|
function getOptions(input) {
|
|
129
135
|
const options = typeof input === "object" && input != null ? input : {};
|
|
130
136
|
return {
|
|
@@ -148,7 +154,7 @@ function getStatus(state, status) {
|
|
|
148
154
|
for (const [key, queue] of queues) if (queue[status]) result.push(key);
|
|
149
155
|
return result;
|
|
150
156
|
}
|
|
151
|
-
function handleQueuedResult(item, error, result
|
|
157
|
+
function handleQueuedResult(item, error, result) {
|
|
152
158
|
item.signal?.removeEventListener(EVENT_NAME, item.abort);
|
|
153
159
|
if (item.signal?.aborted ?? false) {
|
|
154
160
|
item.reject();
|
|
@@ -158,10 +164,7 @@ function handleQueuedResult(item, error, result, finished) {
|
|
|
158
164
|
item.reject(result);
|
|
159
165
|
return;
|
|
160
166
|
}
|
|
161
|
-
item.resolve(
|
|
162
|
-
finished,
|
|
163
|
-
value: result
|
|
164
|
-
});
|
|
167
|
+
item.resolve(result);
|
|
165
168
|
}
|
|
166
169
|
function handleQueues(state, type, key) {
|
|
167
170
|
if (typeof key === "string") {
|
|
@@ -212,21 +215,7 @@ function keyedQueue(callback, options) {
|
|
|
212
215
|
pause: (key) => {
|
|
213
216
|
handleQueues(state, HANDLE_PAUSE, key);
|
|
214
217
|
},
|
|
215
|
-
remove: (key, id) =>
|
|
216
|
-
if (key == null) {
|
|
217
|
-
handleQueues(state, HANDLE_CLEAR);
|
|
218
|
-
state.queues.clear();
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
const queue = getQueue(state, key);
|
|
222
|
-
if (queue == null) return;
|
|
223
|
-
if (typeof id === "number") {
|
|
224
|
-
queue.remove(id);
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
queue.clear();
|
|
228
|
-
state.queues.delete(key);
|
|
229
|
-
},
|
|
218
|
+
remove: (key, id) => removeQueue(state, key, id),
|
|
230
219
|
resume: (key) => {
|
|
231
220
|
handleQueues(state, HANDLE_RESUME, key);
|
|
232
221
|
}
|
|
@@ -258,12 +247,7 @@ function keyedQueue(callback, options) {
|
|
|
258
247
|
},
|
|
259
248
|
items: {
|
|
260
249
|
enumerable: true,
|
|
261
|
-
get: () =>
|
|
262
|
-
const size = {};
|
|
263
|
-
const queues = state.queues.entries();
|
|
264
|
-
for (const [key, queue] of queues) size[key] = queue.size;
|
|
265
|
-
return size;
|
|
266
|
-
}
|
|
250
|
+
get: () => getItems(state)
|
|
267
251
|
},
|
|
268
252
|
keys: {
|
|
269
253
|
enumerable: true,
|
|
@@ -288,6 +272,21 @@ function queue(callback, options) {
|
|
|
288
272
|
return createQueue(callback, options);
|
|
289
273
|
}
|
|
290
274
|
queue.keyed = keyedQueue;
|
|
275
|
+
function removeQueue(state, key, id) {
|
|
276
|
+
if (key == null) {
|
|
277
|
+
handleQueues(state, HANDLE_CLEAR);
|
|
278
|
+
state.queues.clear();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const queue = getQueue(state, key);
|
|
282
|
+
if (queue == null) return;
|
|
283
|
+
if (typeof id === "number") {
|
|
284
|
+
queue.remove(id);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
queue.clear();
|
|
288
|
+
state.queues.delete(key);
|
|
289
|
+
}
|
|
291
290
|
async function run(state) {
|
|
292
291
|
if (state.paused || state.runners >= state.options.concurrency) return;
|
|
293
292
|
state.runners += 1;
|
|
@@ -307,11 +306,11 @@ async function run(state) {
|
|
|
307
306
|
if (state.paused) {
|
|
308
307
|
const paused = item;
|
|
309
308
|
state.handled.push(() => {
|
|
310
|
-
handleQueuedResult(paused, error, result
|
|
309
|
+
handleQueuedResult(paused, error, result);
|
|
311
310
|
});
|
|
312
311
|
break;
|
|
313
312
|
}
|
|
314
|
-
handleQueuedResult(item, error, result
|
|
313
|
+
handleQueuedResult(item, error, result);
|
|
315
314
|
item = state.items.shift();
|
|
316
315
|
}
|
|
317
316
|
state.runners -= 1;
|
package/package.json
CHANGED
package/src/queue.ts
CHANGED
|
@@ -9,8 +9,8 @@ type HandleType = 'clear' | 'pause' | 'resume';
|
|
|
9
9
|
* A queue that can be used to manage (a)synchronous tasks with a specific key
|
|
10
10
|
*/
|
|
11
11
|
export type KeyedQueue<
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
Callback extends GenericCallback | GenericAsyncCallback,
|
|
13
|
+
CallbackParameters extends unknown[] = Parameters<Callback>,
|
|
14
14
|
> = {
|
|
15
15
|
/**
|
|
16
16
|
* Get keys of all active queues
|
|
@@ -74,7 +74,7 @@ export type KeyedQueue<
|
|
|
74
74
|
key: string,
|
|
75
75
|
parameters: Tail<CallbackParameters>,
|
|
76
76
|
signal?: AbortSignal,
|
|
77
|
-
): Queued<
|
|
77
|
+
): Queued<ReturnType<Callback>>;
|
|
78
78
|
|
|
79
79
|
/**
|
|
80
80
|
* Clear all items for a specific key _(or all items for all keys, if no key is provided)_
|
|
@@ -89,7 +89,7 @@ export type KeyedQueue<
|
|
|
89
89
|
* @param key Key to get the queue for
|
|
90
90
|
* @returns Queue for the key, or `undefined` if it doesn't exist
|
|
91
91
|
*/
|
|
92
|
-
get(key: string): Queue<Tail<CallbackParameters
|
|
92
|
+
get(key: string): Queue<Callback, Tail<CallbackParameters>> | undefined;
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
95
|
* Pause the queue for a specific key _(or all queues, if no key is provided)_
|
|
@@ -129,15 +129,18 @@ export type KeyedQueue<
|
|
|
129
129
|
};
|
|
130
130
|
|
|
131
131
|
type KeyedQueueState = {
|
|
132
|
-
callback: GenericAsyncCallback;
|
|
132
|
+
callback: GenericCallback | GenericAsyncCallback;
|
|
133
133
|
options: Required<QueueOptions>;
|
|
134
|
-
queues: Map<string, Queue<Tail<unknown[]
|
|
134
|
+
queues: Map<string, Queue<GenericAsyncCallback, Tail<unknown[]>>>;
|
|
135
135
|
};
|
|
136
136
|
|
|
137
137
|
/**
|
|
138
138
|
* A queue that can be used to manage (a)synchronous tasks
|
|
139
139
|
*/
|
|
140
|
-
export type Queue<
|
|
140
|
+
export type Queue<
|
|
141
|
+
Callback extends GenericCallback | GenericAsyncCallback,
|
|
142
|
+
CallbackParameters extends unknown[] = Parameters<Callback>,
|
|
143
|
+
> = {
|
|
141
144
|
/**
|
|
142
145
|
* Is the queue active?
|
|
143
146
|
*/
|
|
@@ -185,7 +188,7 @@ export type Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, C
|
|
|
185
188
|
* @param signal Optional signal to abort the item
|
|
186
189
|
* @returns Queued item
|
|
187
190
|
*/
|
|
188
|
-
add(parameters: CallbackParameters, signal?: AbortSignal): Queued<
|
|
191
|
+
add(parameters: CallbackParameters, signal?: AbortSignal): Queued<ReturnType<Callback>>;
|
|
189
192
|
|
|
190
193
|
/**
|
|
191
194
|
* Remove and reject all items in the queue
|
|
@@ -261,7 +264,7 @@ export type Queued<Value> = {
|
|
|
261
264
|
/**
|
|
262
265
|
* Queued promise
|
|
263
266
|
*/
|
|
264
|
-
readonly promise: Promise<
|
|
267
|
+
readonly promise: Promise<Value extends Promise<infer Result> ? Result : Value>;
|
|
265
268
|
};
|
|
266
269
|
|
|
267
270
|
type QueuedItem = {
|
|
@@ -269,23 +272,12 @@ type QueuedItem = {
|
|
|
269
272
|
id: number;
|
|
270
273
|
key?: string;
|
|
271
274
|
parameters: unknown[];
|
|
272
|
-
promise: Promise<
|
|
275
|
+
promise: Promise<unknown>;
|
|
273
276
|
reject: (reason?: unknown) => void;
|
|
274
|
-
resolve: (value:
|
|
277
|
+
resolve: (value: unknown) => void;
|
|
275
278
|
signal?: AbortSignal;
|
|
276
279
|
};
|
|
277
280
|
|
|
278
|
-
export type QueuedResult<Value> = {
|
|
279
|
-
/**
|
|
280
|
-
* Has the queue finished processing all items?
|
|
281
|
-
*/
|
|
282
|
-
finished: boolean;
|
|
283
|
-
/**
|
|
284
|
-
* Result for the queued promise
|
|
285
|
-
*/
|
|
286
|
-
value: Value extends Promise<infer Result> ? Result : Value;
|
|
287
|
-
};
|
|
288
|
-
|
|
289
281
|
type StatusKey = 'active' | 'empty' | 'full' | 'paused';
|
|
290
282
|
|
|
291
283
|
type Tail<Values extends any[]> = Values extends [infer _, ...infer Rest] ? Rest : never;
|
|
@@ -295,10 +287,10 @@ type Tail<Values extends any[]> = Values extends [infer _, ...infer Rest] ? Rest
|
|
|
295
287
|
// #region Functions
|
|
296
288
|
|
|
297
289
|
function createQueue(
|
|
298
|
-
callback:
|
|
290
|
+
callback: GenericAsyncCallback,
|
|
299
291
|
options?: QueueOptions,
|
|
300
292
|
key?: string,
|
|
301
|
-
): Queue<
|
|
293
|
+
): Queue<never, never> {
|
|
302
294
|
if (typeof callback !== 'function') {
|
|
303
295
|
throw new TypeError(MESSAGE_CALLBACK);
|
|
304
296
|
}
|
|
@@ -316,7 +308,7 @@ function createQueue(
|
|
|
316
308
|
|
|
317
309
|
const instance = {
|
|
318
310
|
add: (parameters: unknown[], signal?: AbortSignal) => {
|
|
319
|
-
if ((instance as Queue<
|
|
311
|
+
if ((instance as Queue<GenericCallback>).full) {
|
|
320
312
|
throw new QueueError(MESSAGE_MAXIMUM);
|
|
321
313
|
}
|
|
322
314
|
|
|
@@ -329,9 +321,9 @@ function createQueue(
|
|
|
329
321
|
const id = identify(state);
|
|
330
322
|
|
|
331
323
|
let rejector: (reason?: unknown) => void;
|
|
332
|
-
let resolver: (value:
|
|
324
|
+
let resolver: (value: unknown) => void;
|
|
333
325
|
|
|
334
|
-
const promise = new Promise<
|
|
326
|
+
const promise = new Promise<unknown>((resolve, reject) => {
|
|
335
327
|
rejector = reject;
|
|
336
328
|
resolver = resolve;
|
|
337
329
|
});
|
|
@@ -442,13 +434,25 @@ function createQueue(
|
|
|
442
434
|
},
|
|
443
435
|
});
|
|
444
436
|
|
|
445
|
-
return Object.freeze(instance) as Queue<
|
|
437
|
+
return Object.freeze(instance) as Queue<GenericCallback>;
|
|
446
438
|
}
|
|
447
439
|
|
|
448
440
|
function getBooleanOrDefault(value: unknown, defaultValue: boolean): boolean {
|
|
449
441
|
return typeof value === 'boolean' ? value : defaultValue;
|
|
450
442
|
}
|
|
451
443
|
|
|
444
|
+
function getItems(state: KeyedQueueState): Record<string, number> {
|
|
445
|
+
const size: Record<string, number> = {};
|
|
446
|
+
|
|
447
|
+
const queues = state.queues.entries();
|
|
448
|
+
|
|
449
|
+
for (const [key, queue] of queues) {
|
|
450
|
+
size[key] = queue.size;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
return size;
|
|
454
|
+
}
|
|
455
|
+
|
|
452
456
|
function getOptions(input?: QueueOptions): Required<QueueOptions> {
|
|
453
457
|
const options = typeof input === 'object' && input != null ? input : {};
|
|
454
458
|
|
|
@@ -459,15 +463,15 @@ function getOptions(input?: QueueOptions): Required<QueueOptions> {
|
|
|
459
463
|
};
|
|
460
464
|
}
|
|
461
465
|
|
|
462
|
-
function getQueue(state: KeyedQueueState, key: string, add: true): Queue<
|
|
466
|
+
function getQueue(state: KeyedQueueState, key: string, add: true): Queue<GenericCallback>;
|
|
463
467
|
|
|
464
|
-
function getQueue(state: KeyedQueueState, key: string): Queue<
|
|
468
|
+
function getQueue(state: KeyedQueueState, key: string): Queue<GenericCallback> | undefined;
|
|
465
469
|
|
|
466
470
|
function getQueue(
|
|
467
471
|
state: KeyedQueueState,
|
|
468
472
|
key: string,
|
|
469
473
|
add?: boolean,
|
|
470
|
-
): Queue<
|
|
474
|
+
): Queue<GenericCallback> | undefined {
|
|
471
475
|
if (typeof key !== 'string' || key.trim().length === 0) {
|
|
472
476
|
throw new TypeError(MESSAGE_KEY);
|
|
473
477
|
}
|
|
@@ -497,12 +501,7 @@ function getStatus(state: KeyedQueueState, status: StatusKey): string[] {
|
|
|
497
501
|
return result;
|
|
498
502
|
}
|
|
499
503
|
|
|
500
|
-
function handleQueuedResult(
|
|
501
|
-
item: QueuedItem,
|
|
502
|
-
error: boolean,
|
|
503
|
-
result: unknown,
|
|
504
|
-
finished: boolean,
|
|
505
|
-
): void {
|
|
504
|
+
function handleQueuedResult(item: QueuedItem, error: boolean, result: unknown): void {
|
|
506
505
|
item.signal?.removeEventListener(EVENT_NAME, item.abort!);
|
|
507
506
|
|
|
508
507
|
if (item.signal?.aborted ?? false) {
|
|
@@ -517,10 +516,7 @@ function handleQueuedResult(
|
|
|
517
516
|
return;
|
|
518
517
|
}
|
|
519
518
|
|
|
520
|
-
item.resolve(
|
|
521
|
-
finished,
|
|
522
|
-
value: result as never,
|
|
523
|
-
});
|
|
519
|
+
item.resolve(result);
|
|
524
520
|
}
|
|
525
521
|
|
|
526
522
|
function handleQueues(state: KeyedQueueState, type: HandleType, key?: string): void {
|
|
@@ -549,7 +545,7 @@ function identify(state: QueueState): number {
|
|
|
549
545
|
* @param value Value to check
|
|
550
546
|
* @returns `true` if the value is a keyed queue, otherwise `false`
|
|
551
547
|
*/
|
|
552
|
-
export function isKeyedQueue(value: unknown): value is KeyedQueue<
|
|
548
|
+
export function isKeyedQueue(value: unknown): value is KeyedQueue<GenericAsyncCallback> {
|
|
553
549
|
return isQueueInstance(NAME_KEYED, value);
|
|
554
550
|
}
|
|
555
551
|
|
|
@@ -559,7 +555,7 @@ export function isKeyedQueue(value: unknown): value is KeyedQueue<unknown[], unk
|
|
|
559
555
|
* @param value Value to check
|
|
560
556
|
* @returns `true` if the value is a queue, otherwise `false`
|
|
561
557
|
*/
|
|
562
|
-
export function isQueue(value: unknown): value is Queue<
|
|
558
|
+
export function isQueue(value: unknown): value is Queue<GenericCallback | GenericAsyncCallback> {
|
|
563
559
|
return isQueueInstance(NAME_QUEUE, value);
|
|
564
560
|
}
|
|
565
561
|
|
|
@@ -571,20 +567,32 @@ export function isQueueInstance<Instance>(name: string, value: unknown): value i
|
|
|
571
567
|
);
|
|
572
568
|
}
|
|
573
569
|
|
|
570
|
+
/**
|
|
571
|
+
* Create a keyed queue for an asynchronous callback function, where each key has its own queue
|
|
572
|
+
*
|
|
573
|
+
* @param callback Callback function for queued items
|
|
574
|
+
* @param options Queue options
|
|
575
|
+
*/
|
|
574
576
|
export function keyedQueue<Callback extends (key: string, ...parameters: any[]) => Promise<void>>(
|
|
575
577
|
callback: Callback,
|
|
576
578
|
options?: QueueOptions,
|
|
577
|
-
): KeyedQueue<
|
|
579
|
+
): KeyedQueue<Callback, Parameters<Callback>>;
|
|
578
580
|
|
|
581
|
+
/**
|
|
582
|
+
* Create a keyed queue for an asynchronous callback function, where each key has its own queue
|
|
583
|
+
*
|
|
584
|
+
* @param callback Callback function for queued items
|
|
585
|
+
* @param options Queue options
|
|
586
|
+
*/
|
|
579
587
|
export function keyedQueue<Callback extends (key: string, ...parameters: any[]) => void>(
|
|
580
588
|
callback: Callback,
|
|
581
589
|
options?: QueueOptions,
|
|
582
|
-
): KeyedQueue<
|
|
590
|
+
): KeyedQueue<Callback, Parameters<Callback>>;
|
|
583
591
|
|
|
584
|
-
export function keyedQueue(
|
|
585
|
-
callback:
|
|
592
|
+
export function keyedQueue<Callback extends (key: string, ...parameters: any[]) => Promise<void>>(
|
|
593
|
+
callback: Callback,
|
|
586
594
|
options?: QueueOptions,
|
|
587
|
-
): KeyedQueue<
|
|
595
|
+
): KeyedQueue<Callback, Parameters<Callback>> {
|
|
588
596
|
if (typeof callback !== 'function') {
|
|
589
597
|
throw new TypeError(MESSAGE_CALLBACK);
|
|
590
598
|
}
|
|
@@ -605,31 +613,7 @@ export function keyedQueue(
|
|
|
605
613
|
pause: (key?: string): void => {
|
|
606
614
|
handleQueues(state, HANDLE_PAUSE, key);
|
|
607
615
|
},
|
|
608
|
-
remove: (key?: string, id?: number): void =>
|
|
609
|
-
if (key == null) {
|
|
610
|
-
handleQueues(state, HANDLE_CLEAR);
|
|
611
|
-
|
|
612
|
-
state.queues.clear();
|
|
613
|
-
|
|
614
|
-
return;
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
const queue = getQueue(state, key);
|
|
618
|
-
|
|
619
|
-
if (queue == null) {
|
|
620
|
-
return;
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
if (typeof id === 'number') {
|
|
624
|
-
queue.remove(id);
|
|
625
|
-
|
|
626
|
-
return;
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
queue.clear();
|
|
630
|
-
|
|
631
|
-
state.queues.delete(key);
|
|
632
|
-
},
|
|
616
|
+
remove: (key?: string, id?: number): void => removeQueue(state, key, id),
|
|
633
617
|
resume: (key?: string): void => {
|
|
634
618
|
handleQueues(state, HANDLE_RESUME, key);
|
|
635
619
|
},
|
|
@@ -662,17 +646,7 @@ export function keyedQueue(
|
|
|
662
646
|
},
|
|
663
647
|
items: {
|
|
664
648
|
enumerable: true,
|
|
665
|
-
get: () =>
|
|
666
|
-
const size: Record<string, number> = {};
|
|
667
|
-
|
|
668
|
-
const queues = state.queues.entries();
|
|
669
|
-
|
|
670
|
-
for (const [key, queue] of queues) {
|
|
671
|
-
size[key] = queue.size;
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
return size;
|
|
675
|
-
},
|
|
649
|
+
get: () => getItems(state),
|
|
676
650
|
},
|
|
677
651
|
keys: {
|
|
678
652
|
enumerable: true,
|
|
@@ -692,7 +666,7 @@ export function keyedQueue(
|
|
|
692
666
|
},
|
|
693
667
|
});
|
|
694
668
|
|
|
695
|
-
return Object.freeze(instance) as KeyedQueue<
|
|
669
|
+
return Object.freeze(instance) as unknown as KeyedQueue<Callback, Parameters<Callback>>;
|
|
696
670
|
}
|
|
697
671
|
|
|
698
672
|
/**
|
|
@@ -705,7 +679,7 @@ export function keyedQueue(
|
|
|
705
679
|
export function queue<Callback extends GenericAsyncCallback>(
|
|
706
680
|
callback: Callback,
|
|
707
681
|
options?: QueueOptions,
|
|
708
|
-
): Queue<
|
|
682
|
+
): Queue<Callback, Parameters<Callback>>;
|
|
709
683
|
|
|
710
684
|
/**
|
|
711
685
|
* Create a queue for a synchronous callback function
|
|
@@ -717,17 +691,43 @@ export function queue<Callback extends GenericAsyncCallback>(
|
|
|
717
691
|
export function queue<Callback extends GenericCallback>(
|
|
718
692
|
callback: Callback,
|
|
719
693
|
options?: QueueOptions,
|
|
720
|
-
): Queue<
|
|
694
|
+
): Queue<Callback, Parameters<Callback>>;
|
|
721
695
|
|
|
722
|
-
export function queue(
|
|
723
|
-
callback:
|
|
696
|
+
export function queue<Callback extends GenericCallback | GenericAsyncCallback>(
|
|
697
|
+
callback: Callback,
|
|
724
698
|
options?: QueueOptions,
|
|
725
|
-
): Queue<
|
|
699
|
+
): Queue<Callback, Parameters<Callback>> {
|
|
726
700
|
return createQueue(callback, options);
|
|
727
701
|
}
|
|
728
702
|
|
|
729
703
|
queue.keyed = keyedQueue;
|
|
730
704
|
|
|
705
|
+
function removeQueue(state: KeyedQueueState, key?: string, id?: number): void {
|
|
706
|
+
if (key == null) {
|
|
707
|
+
handleQueues(state, HANDLE_CLEAR);
|
|
708
|
+
|
|
709
|
+
state.queues.clear();
|
|
710
|
+
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
const queue = getQueue(state, key);
|
|
715
|
+
|
|
716
|
+
if (queue == null) {
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (typeof id === 'number') {
|
|
721
|
+
queue.remove(id);
|
|
722
|
+
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
queue.clear();
|
|
727
|
+
|
|
728
|
+
state.queues.delete(key);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
731
|
async function run(state: QueueState): Promise<void> {
|
|
732
732
|
if (state.paused || state.runners >= state.options.concurrency) {
|
|
733
733
|
return;
|
|
@@ -757,13 +757,13 @@ async function run(state: QueueState): Promise<void> {
|
|
|
757
757
|
const paused = item;
|
|
758
758
|
|
|
759
759
|
state.handled.push(() => {
|
|
760
|
-
handleQueuedResult(paused, error, result
|
|
760
|
+
handleQueuedResult(paused, error, result);
|
|
761
761
|
});
|
|
762
762
|
|
|
763
763
|
break;
|
|
764
764
|
}
|
|
765
765
|
|
|
766
|
-
handleQueuedResult(item, error, result
|
|
766
|
+
handleQueuedResult(item, error, result);
|
|
767
767
|
|
|
768
768
|
item = state.items.shift();
|
|
769
769
|
}
|