@darajs/core 1.26.3 → 1.26.5
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/{dara_core-1.26.3-py3-none-any.whl → dara_core-1.26.5-py3-none-any.whl} +0 -0
- package/dist/shared/interactivity/filtering.js +1 -1
- package/dist/shared/interactivity/filtering.js.map +1 -1
- package/dist/shared/interactivity/persistence.d.ts.map +1 -1
- package/dist/shared/interactivity/persistence.js +70 -6
- package/dist/shared/interactivity/persistence.js.map +1 -1
- package/dist/umd/dara.core.umd.cjs +1052 -4
- package/package.json +10 -9
|
@@ -39442,6 +39442,47 @@ You must set sticky: 'left' | 'right' for the '${bugWithUnderColumnsSticky.Heade
|
|
|
39442
39442
|
return Object.assign(Object.assign({}, sort), { id: ((_a = columns.find((col) => col.accessor === sort.id)) === null || _a === void 0 ? void 0 : _a.sortKey) || sort.id });
|
|
39443
39443
|
});
|
|
39444
39444
|
};
|
|
39445
|
+
const RE_SPLIT_ALPHANUMERIC = /([0-9]+)/g;
|
|
39446
|
+
const caseInsensitiveAlphanumeric = (rowA, rowB, columnId) => {
|
|
39447
|
+
const getVal = (row) => {
|
|
39448
|
+
const val = row.values[columnId];
|
|
39449
|
+
if (val == null) {
|
|
39450
|
+
return "";
|
|
39451
|
+
}
|
|
39452
|
+
if (typeof val === "number") {
|
|
39453
|
+
return Number.isNaN(val) || val === Infinity || val === -Infinity ? "" : String(val);
|
|
39454
|
+
}
|
|
39455
|
+
return typeof val === "string" ? val : String(val);
|
|
39456
|
+
};
|
|
39457
|
+
const a2 = getVal(rowA).split(RE_SPLIT_ALPHANUMERIC).filter(Boolean);
|
|
39458
|
+
const b2 = getVal(rowB).split(RE_SPLIT_ALPHANUMERIC).filter(Boolean);
|
|
39459
|
+
const len = Math.min(a2.length, b2.length);
|
|
39460
|
+
for (let i2 = 0; i2 < len; i2++) {
|
|
39461
|
+
const aa = a2[i2];
|
|
39462
|
+
const bb = b2[i2];
|
|
39463
|
+
const an = parseInt(aa);
|
|
39464
|
+
const bn = parseInt(bb);
|
|
39465
|
+
const aIsNaN = Number.isNaN(an);
|
|
39466
|
+
const bIsNaN = Number.isNaN(bn);
|
|
39467
|
+
if (aIsNaN && bIsNaN) {
|
|
39468
|
+
const cmp = aa.toLowerCase().localeCompare(bb.toLowerCase());
|
|
39469
|
+
if (cmp !== 0) {
|
|
39470
|
+
return cmp;
|
|
39471
|
+
}
|
|
39472
|
+
continue;
|
|
39473
|
+
}
|
|
39474
|
+
if (aIsNaN) {
|
|
39475
|
+
return 1;
|
|
39476
|
+
}
|
|
39477
|
+
if (bIsNaN) {
|
|
39478
|
+
return -1;
|
|
39479
|
+
}
|
|
39480
|
+
if (an !== bn) {
|
|
39481
|
+
return an > bn ? 1 : -1;
|
|
39482
|
+
}
|
|
39483
|
+
}
|
|
39484
|
+
return a2.length - b2.length;
|
|
39485
|
+
};
|
|
39445
39486
|
const orderStickyCols = (columns) => {
|
|
39446
39487
|
const leftStickyCols = [];
|
|
39447
39488
|
const nonStickyCols = [];
|
|
@@ -39579,6 +39620,9 @@ You must set sticky: 'left' | 'right' for the '${bugWithUnderColumnsSticky.Heade
|
|
|
39579
39620
|
columns: mappedColumns,
|
|
39580
39621
|
data: data2 || infiniteData,
|
|
39581
39622
|
filterTypes,
|
|
39623
|
+
sortTypes: {
|
|
39624
|
+
alphanumeric: caseInsensitiveAlphanumeric
|
|
39625
|
+
},
|
|
39582
39626
|
initialState: {
|
|
39583
39627
|
sortBy: currentSortBy.map((sort) => Object.assign(Object.assign({}, sort), { id: mappedColumns.find((col) => [col.sortKey, col.accessor].includes(sort.id)).accessor }))
|
|
39584
39628
|
},
|
|
@@ -71936,6 +71980,958 @@ ${String(error)}`;
|
|
|
71936
71980
|
function cleanArgs(values, forceKeyOverride) {
|
|
71937
71981
|
return values.map((val) => cleanValue(val, forceKeyOverride));
|
|
71938
71982
|
}
|
|
71983
|
+
var eventemitter3 = { exports: {} };
|
|
71984
|
+
var hasRequiredEventemitter3;
|
|
71985
|
+
function requireEventemitter3() {
|
|
71986
|
+
if (hasRequiredEventemitter3) return eventemitter3.exports;
|
|
71987
|
+
hasRequiredEventemitter3 = 1;
|
|
71988
|
+
(function(module2) {
|
|
71989
|
+
var has = Object.prototype.hasOwnProperty, prefix = "~";
|
|
71990
|
+
function Events() {
|
|
71991
|
+
}
|
|
71992
|
+
if (Object.create) {
|
|
71993
|
+
Events.prototype = /* @__PURE__ */ Object.create(null);
|
|
71994
|
+
if (!new Events().__proto__) prefix = false;
|
|
71995
|
+
}
|
|
71996
|
+
function EE(fn, context, once) {
|
|
71997
|
+
this.fn = fn;
|
|
71998
|
+
this.context = context;
|
|
71999
|
+
this.once = once || false;
|
|
72000
|
+
}
|
|
72001
|
+
function addListener(emitter, event, fn, context, once) {
|
|
72002
|
+
if (typeof fn !== "function") {
|
|
72003
|
+
throw new TypeError("The listener must be a function");
|
|
72004
|
+
}
|
|
72005
|
+
var listener2 = new EE(fn, context || emitter, once), evt = prefix ? prefix + event : event;
|
|
72006
|
+
if (!emitter._events[evt]) emitter._events[evt] = listener2, emitter._eventsCount++;
|
|
72007
|
+
else if (!emitter._events[evt].fn) emitter._events[evt].push(listener2);
|
|
72008
|
+
else emitter._events[evt] = [emitter._events[evt], listener2];
|
|
72009
|
+
return emitter;
|
|
72010
|
+
}
|
|
72011
|
+
function clearEvent(emitter, evt) {
|
|
72012
|
+
if (--emitter._eventsCount === 0) emitter._events = new Events();
|
|
72013
|
+
else delete emitter._events[evt];
|
|
72014
|
+
}
|
|
72015
|
+
function EventEmitter2() {
|
|
72016
|
+
this._events = new Events();
|
|
72017
|
+
this._eventsCount = 0;
|
|
72018
|
+
}
|
|
72019
|
+
EventEmitter2.prototype.eventNames = function eventNames() {
|
|
72020
|
+
var names = [], events, name;
|
|
72021
|
+
if (this._eventsCount === 0) return names;
|
|
72022
|
+
for (name in events = this._events) {
|
|
72023
|
+
if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
|
|
72024
|
+
}
|
|
72025
|
+
if (Object.getOwnPropertySymbols) {
|
|
72026
|
+
return names.concat(Object.getOwnPropertySymbols(events));
|
|
72027
|
+
}
|
|
72028
|
+
return names;
|
|
72029
|
+
};
|
|
72030
|
+
EventEmitter2.prototype.listeners = function listeners(event) {
|
|
72031
|
+
var evt = prefix ? prefix + event : event, handlers = this._events[evt];
|
|
72032
|
+
if (!handlers) return [];
|
|
72033
|
+
if (handlers.fn) return [handlers.fn];
|
|
72034
|
+
for (var i2 = 0, l2 = handlers.length, ee = new Array(l2); i2 < l2; i2++) {
|
|
72035
|
+
ee[i2] = handlers[i2].fn;
|
|
72036
|
+
}
|
|
72037
|
+
return ee;
|
|
72038
|
+
};
|
|
72039
|
+
EventEmitter2.prototype.listenerCount = function listenerCount(event) {
|
|
72040
|
+
var evt = prefix ? prefix + event : event, listeners = this._events[evt];
|
|
72041
|
+
if (!listeners) return 0;
|
|
72042
|
+
if (listeners.fn) return 1;
|
|
72043
|
+
return listeners.length;
|
|
72044
|
+
};
|
|
72045
|
+
EventEmitter2.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
|
|
72046
|
+
var evt = prefix ? prefix + event : event;
|
|
72047
|
+
if (!this._events[evt]) return false;
|
|
72048
|
+
var listeners = this._events[evt], len = arguments.length, args, i2;
|
|
72049
|
+
if (listeners.fn) {
|
|
72050
|
+
if (listeners.once) this.removeListener(event, listeners.fn, void 0, true);
|
|
72051
|
+
switch (len) {
|
|
72052
|
+
case 1:
|
|
72053
|
+
return listeners.fn.call(listeners.context), true;
|
|
72054
|
+
case 2:
|
|
72055
|
+
return listeners.fn.call(listeners.context, a1), true;
|
|
72056
|
+
case 3:
|
|
72057
|
+
return listeners.fn.call(listeners.context, a1, a2), true;
|
|
72058
|
+
case 4:
|
|
72059
|
+
return listeners.fn.call(listeners.context, a1, a2, a3), true;
|
|
72060
|
+
case 5:
|
|
72061
|
+
return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
|
|
72062
|
+
case 6:
|
|
72063
|
+
return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
|
|
72064
|
+
}
|
|
72065
|
+
for (i2 = 1, args = new Array(len - 1); i2 < len; i2++) {
|
|
72066
|
+
args[i2 - 1] = arguments[i2];
|
|
72067
|
+
}
|
|
72068
|
+
listeners.fn.apply(listeners.context, args);
|
|
72069
|
+
} else {
|
|
72070
|
+
var length = listeners.length, j;
|
|
72071
|
+
for (i2 = 0; i2 < length; i2++) {
|
|
72072
|
+
if (listeners[i2].once) this.removeListener(event, listeners[i2].fn, void 0, true);
|
|
72073
|
+
switch (len) {
|
|
72074
|
+
case 1:
|
|
72075
|
+
listeners[i2].fn.call(listeners[i2].context);
|
|
72076
|
+
break;
|
|
72077
|
+
case 2:
|
|
72078
|
+
listeners[i2].fn.call(listeners[i2].context, a1);
|
|
72079
|
+
break;
|
|
72080
|
+
case 3:
|
|
72081
|
+
listeners[i2].fn.call(listeners[i2].context, a1, a2);
|
|
72082
|
+
break;
|
|
72083
|
+
case 4:
|
|
72084
|
+
listeners[i2].fn.call(listeners[i2].context, a1, a2, a3);
|
|
72085
|
+
break;
|
|
72086
|
+
default:
|
|
72087
|
+
if (!args) for (j = 1, args = new Array(len - 1); j < len; j++) {
|
|
72088
|
+
args[j - 1] = arguments[j];
|
|
72089
|
+
}
|
|
72090
|
+
listeners[i2].fn.apply(listeners[i2].context, args);
|
|
72091
|
+
}
|
|
72092
|
+
}
|
|
72093
|
+
}
|
|
72094
|
+
return true;
|
|
72095
|
+
};
|
|
72096
|
+
EventEmitter2.prototype.on = function on(event, fn, context) {
|
|
72097
|
+
return addListener(this, event, fn, context, false);
|
|
72098
|
+
};
|
|
72099
|
+
EventEmitter2.prototype.once = function once(event, fn, context) {
|
|
72100
|
+
return addListener(this, event, fn, context, true);
|
|
72101
|
+
};
|
|
72102
|
+
EventEmitter2.prototype.removeListener = function removeListener(event, fn, context, once) {
|
|
72103
|
+
var evt = prefix ? prefix + event : event;
|
|
72104
|
+
if (!this._events[evt]) return this;
|
|
72105
|
+
if (!fn) {
|
|
72106
|
+
clearEvent(this, evt);
|
|
72107
|
+
return this;
|
|
72108
|
+
}
|
|
72109
|
+
var listeners = this._events[evt];
|
|
72110
|
+
if (listeners.fn) {
|
|
72111
|
+
if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) {
|
|
72112
|
+
clearEvent(this, evt);
|
|
72113
|
+
}
|
|
72114
|
+
} else {
|
|
72115
|
+
for (var i2 = 0, events = [], length = listeners.length; i2 < length; i2++) {
|
|
72116
|
+
if (listeners[i2].fn !== fn || once && !listeners[i2].once || context && listeners[i2].context !== context) {
|
|
72117
|
+
events.push(listeners[i2]);
|
|
72118
|
+
}
|
|
72119
|
+
}
|
|
72120
|
+
if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
|
|
72121
|
+
else clearEvent(this, evt);
|
|
72122
|
+
}
|
|
72123
|
+
return this;
|
|
72124
|
+
};
|
|
72125
|
+
EventEmitter2.prototype.removeAllListeners = function removeAllListeners(event) {
|
|
72126
|
+
var evt;
|
|
72127
|
+
if (event) {
|
|
72128
|
+
evt = prefix ? prefix + event : event;
|
|
72129
|
+
if (this._events[evt]) clearEvent(this, evt);
|
|
72130
|
+
} else {
|
|
72131
|
+
this._events = new Events();
|
|
72132
|
+
this._eventsCount = 0;
|
|
72133
|
+
}
|
|
72134
|
+
return this;
|
|
72135
|
+
};
|
|
72136
|
+
EventEmitter2.prototype.off = EventEmitter2.prototype.removeListener;
|
|
72137
|
+
EventEmitter2.prototype.addListener = EventEmitter2.prototype.on;
|
|
72138
|
+
EventEmitter2.prefixed = prefix;
|
|
72139
|
+
EventEmitter2.EventEmitter = EventEmitter2;
|
|
72140
|
+
{
|
|
72141
|
+
module2.exports = EventEmitter2;
|
|
72142
|
+
}
|
|
72143
|
+
})(eventemitter3);
|
|
72144
|
+
return eventemitter3.exports;
|
|
72145
|
+
}
|
|
72146
|
+
var eventemitter3Exports = requireEventemitter3();
|
|
72147
|
+
const EventEmitter = /* @__PURE__ */ getDefaultExportFromCjs(eventemitter3Exports);
|
|
72148
|
+
class TimeoutError extends Error {
|
|
72149
|
+
name = "TimeoutError";
|
|
72150
|
+
constructor(message, options) {
|
|
72151
|
+
super(message, options);
|
|
72152
|
+
Error.captureStackTrace?.(this, TimeoutError);
|
|
72153
|
+
}
|
|
72154
|
+
}
|
|
72155
|
+
const getAbortedReason = (signal) => signal.reason ?? new DOMException("This operation was aborted.", "AbortError");
|
|
72156
|
+
function pTimeout(promise, options) {
|
|
72157
|
+
const {
|
|
72158
|
+
milliseconds,
|
|
72159
|
+
fallback,
|
|
72160
|
+
message,
|
|
72161
|
+
customTimers = { setTimeout, clearTimeout },
|
|
72162
|
+
signal
|
|
72163
|
+
} = options;
|
|
72164
|
+
let timer2;
|
|
72165
|
+
let abortHandler;
|
|
72166
|
+
const wrappedPromise = new Promise((resolve, reject) => {
|
|
72167
|
+
if (typeof milliseconds !== "number" || Math.sign(milliseconds) !== 1) {
|
|
72168
|
+
throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
|
|
72169
|
+
}
|
|
72170
|
+
if (signal?.aborted) {
|
|
72171
|
+
reject(getAbortedReason(signal));
|
|
72172
|
+
return;
|
|
72173
|
+
}
|
|
72174
|
+
if (signal) {
|
|
72175
|
+
abortHandler = () => {
|
|
72176
|
+
reject(getAbortedReason(signal));
|
|
72177
|
+
};
|
|
72178
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
72179
|
+
}
|
|
72180
|
+
promise.then(resolve, reject);
|
|
72181
|
+
if (milliseconds === Number.POSITIVE_INFINITY) {
|
|
72182
|
+
return;
|
|
72183
|
+
}
|
|
72184
|
+
const timeoutError = new TimeoutError();
|
|
72185
|
+
timer2 = customTimers.setTimeout.call(void 0, () => {
|
|
72186
|
+
if (fallback) {
|
|
72187
|
+
try {
|
|
72188
|
+
resolve(fallback());
|
|
72189
|
+
} catch (error) {
|
|
72190
|
+
reject(error);
|
|
72191
|
+
}
|
|
72192
|
+
return;
|
|
72193
|
+
}
|
|
72194
|
+
if (typeof promise.cancel === "function") {
|
|
72195
|
+
promise.cancel();
|
|
72196
|
+
}
|
|
72197
|
+
if (message === false) {
|
|
72198
|
+
resolve();
|
|
72199
|
+
} else if (message instanceof Error) {
|
|
72200
|
+
reject(message);
|
|
72201
|
+
} else {
|
|
72202
|
+
timeoutError.message = message ?? `Promise timed out after ${milliseconds} milliseconds`;
|
|
72203
|
+
reject(timeoutError);
|
|
72204
|
+
}
|
|
72205
|
+
}, milliseconds);
|
|
72206
|
+
});
|
|
72207
|
+
const cancelablePromise = wrappedPromise.finally(() => {
|
|
72208
|
+
cancelablePromise.clear();
|
|
72209
|
+
if (abortHandler && signal) {
|
|
72210
|
+
signal.removeEventListener("abort", abortHandler);
|
|
72211
|
+
}
|
|
72212
|
+
});
|
|
72213
|
+
cancelablePromise.clear = () => {
|
|
72214
|
+
customTimers.clearTimeout.call(void 0, timer2);
|
|
72215
|
+
timer2 = void 0;
|
|
72216
|
+
};
|
|
72217
|
+
return cancelablePromise;
|
|
72218
|
+
}
|
|
72219
|
+
function lowerBound(array2, value, comparator) {
|
|
72220
|
+
let first = 0;
|
|
72221
|
+
let count2 = array2.length;
|
|
72222
|
+
while (count2 > 0) {
|
|
72223
|
+
const step = Math.trunc(count2 / 2);
|
|
72224
|
+
let it2 = first + step;
|
|
72225
|
+
if (comparator(array2[it2], value) <= 0) {
|
|
72226
|
+
first = ++it2;
|
|
72227
|
+
count2 -= step + 1;
|
|
72228
|
+
} else {
|
|
72229
|
+
count2 = step;
|
|
72230
|
+
}
|
|
72231
|
+
}
|
|
72232
|
+
return first;
|
|
72233
|
+
}
|
|
72234
|
+
class PriorityQueue {
|
|
72235
|
+
#queue = [];
|
|
72236
|
+
enqueue(run2, options) {
|
|
72237
|
+
const { priority = 0, id } = options ?? {};
|
|
72238
|
+
const element = {
|
|
72239
|
+
priority,
|
|
72240
|
+
id,
|
|
72241
|
+
run: run2
|
|
72242
|
+
};
|
|
72243
|
+
if (this.size === 0 || this.#queue[this.size - 1].priority >= priority) {
|
|
72244
|
+
this.#queue.push(element);
|
|
72245
|
+
return;
|
|
72246
|
+
}
|
|
72247
|
+
const index2 = lowerBound(this.#queue, element, (a2, b2) => b2.priority - a2.priority);
|
|
72248
|
+
this.#queue.splice(index2, 0, element);
|
|
72249
|
+
}
|
|
72250
|
+
setPriority(id, priority) {
|
|
72251
|
+
const index2 = this.#queue.findIndex((element) => element.id === id);
|
|
72252
|
+
if (index2 === -1) {
|
|
72253
|
+
throw new ReferenceError(`No promise function with the id "${id}" exists in the queue.`);
|
|
72254
|
+
}
|
|
72255
|
+
const [item] = this.#queue.splice(index2, 1);
|
|
72256
|
+
this.enqueue(item.run, { priority, id });
|
|
72257
|
+
}
|
|
72258
|
+
dequeue() {
|
|
72259
|
+
const item = this.#queue.shift();
|
|
72260
|
+
return item?.run;
|
|
72261
|
+
}
|
|
72262
|
+
filter(options) {
|
|
72263
|
+
return this.#queue.filter((element) => element.priority === options.priority).map((element) => element.run);
|
|
72264
|
+
}
|
|
72265
|
+
get size() {
|
|
72266
|
+
return this.#queue.length;
|
|
72267
|
+
}
|
|
72268
|
+
}
|
|
72269
|
+
class PQueue extends EventEmitter {
|
|
72270
|
+
#carryoverIntervalCount;
|
|
72271
|
+
#isIntervalIgnored;
|
|
72272
|
+
#intervalCount = 0;
|
|
72273
|
+
#intervalCap;
|
|
72274
|
+
#rateLimitedInInterval = false;
|
|
72275
|
+
#rateLimitFlushScheduled = false;
|
|
72276
|
+
#interval;
|
|
72277
|
+
#intervalEnd = 0;
|
|
72278
|
+
#lastExecutionTime = 0;
|
|
72279
|
+
#intervalId;
|
|
72280
|
+
#timeoutId;
|
|
72281
|
+
#strict;
|
|
72282
|
+
// Circular buffer implementation for better performance
|
|
72283
|
+
#strictTicks = [];
|
|
72284
|
+
#strictTicksStartIndex = 0;
|
|
72285
|
+
#queue;
|
|
72286
|
+
#queueClass;
|
|
72287
|
+
#pending = 0;
|
|
72288
|
+
// The `!` is needed because of https://github.com/microsoft/TypeScript/issues/32194
|
|
72289
|
+
#concurrency;
|
|
72290
|
+
#isPaused;
|
|
72291
|
+
// Use to assign a unique identifier to a promise function, if not explicitly specified
|
|
72292
|
+
#idAssigner = 1n;
|
|
72293
|
+
// Track currently running tasks for debugging
|
|
72294
|
+
#runningTasks = /* @__PURE__ */ new Map();
|
|
72295
|
+
/**
|
|
72296
|
+
Get or set the default timeout for all tasks. Can be changed at runtime.
|
|
72297
|
+
|
|
72298
|
+
Operations will throw a `TimeoutError` if they don't complete within the specified time.
|
|
72299
|
+
|
|
72300
|
+
The timeout begins when the operation is dequeued and starts execution, not while it's waiting in the queue.
|
|
72301
|
+
|
|
72302
|
+
@example
|
|
72303
|
+
```
|
|
72304
|
+
const queue = new PQueue({timeout: 5000});
|
|
72305
|
+
|
|
72306
|
+
// Change timeout for all future tasks
|
|
72307
|
+
queue.timeout = 10000;
|
|
72308
|
+
```
|
|
72309
|
+
*/
|
|
72310
|
+
timeout;
|
|
72311
|
+
constructor(options) {
|
|
72312
|
+
super();
|
|
72313
|
+
options = {
|
|
72314
|
+
carryoverIntervalCount: false,
|
|
72315
|
+
intervalCap: Number.POSITIVE_INFINITY,
|
|
72316
|
+
interval: 0,
|
|
72317
|
+
concurrency: Number.POSITIVE_INFINITY,
|
|
72318
|
+
autoStart: true,
|
|
72319
|
+
queueClass: PriorityQueue,
|
|
72320
|
+
strict: false,
|
|
72321
|
+
...options
|
|
72322
|
+
};
|
|
72323
|
+
if (!(typeof options.intervalCap === "number" && options.intervalCap >= 1)) {
|
|
72324
|
+
throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${options.intervalCap?.toString() ?? ""}\` (${typeof options.intervalCap})`);
|
|
72325
|
+
}
|
|
72326
|
+
if (options.interval === void 0 || !(Number.isFinite(options.interval) && options.interval >= 0)) {
|
|
72327
|
+
throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${options.interval?.toString() ?? ""}\` (${typeof options.interval})`);
|
|
72328
|
+
}
|
|
72329
|
+
if (options.strict && options.interval === 0) {
|
|
72330
|
+
throw new TypeError("The `strict` option requires a non-zero `interval`");
|
|
72331
|
+
}
|
|
72332
|
+
if (options.strict && options.intervalCap === Number.POSITIVE_INFINITY) {
|
|
72333
|
+
throw new TypeError("The `strict` option requires a finite `intervalCap`");
|
|
72334
|
+
}
|
|
72335
|
+
this.#carryoverIntervalCount = options.carryoverIntervalCount ?? options.carryoverConcurrencyCount ?? false;
|
|
72336
|
+
this.#isIntervalIgnored = options.intervalCap === Number.POSITIVE_INFINITY || options.interval === 0;
|
|
72337
|
+
this.#intervalCap = options.intervalCap;
|
|
72338
|
+
this.#interval = options.interval;
|
|
72339
|
+
this.#strict = options.strict;
|
|
72340
|
+
this.#queue = new options.queueClass();
|
|
72341
|
+
this.#queueClass = options.queueClass;
|
|
72342
|
+
this.concurrency = options.concurrency;
|
|
72343
|
+
if (options.timeout !== void 0 && !(Number.isFinite(options.timeout) && options.timeout > 0)) {
|
|
72344
|
+
throw new TypeError(`Expected \`timeout\` to be a positive finite number, got \`${options.timeout}\` (${typeof options.timeout})`);
|
|
72345
|
+
}
|
|
72346
|
+
this.timeout = options.timeout;
|
|
72347
|
+
this.#isPaused = options.autoStart === false;
|
|
72348
|
+
this.#setupRateLimitTracking();
|
|
72349
|
+
}
|
|
72350
|
+
#cleanupStrictTicks(now2) {
|
|
72351
|
+
while (this.#strictTicksStartIndex < this.#strictTicks.length) {
|
|
72352
|
+
const oldestTick = this.#strictTicks[this.#strictTicksStartIndex];
|
|
72353
|
+
if (oldestTick !== void 0 && now2 - oldestTick >= this.#interval) {
|
|
72354
|
+
this.#strictTicksStartIndex++;
|
|
72355
|
+
} else {
|
|
72356
|
+
break;
|
|
72357
|
+
}
|
|
72358
|
+
}
|
|
72359
|
+
const shouldCompact = this.#strictTicksStartIndex > 100 && this.#strictTicksStartIndex > this.#strictTicks.length / 2 || this.#strictTicksStartIndex === this.#strictTicks.length;
|
|
72360
|
+
if (shouldCompact) {
|
|
72361
|
+
this.#strictTicks = this.#strictTicks.slice(this.#strictTicksStartIndex);
|
|
72362
|
+
this.#strictTicksStartIndex = 0;
|
|
72363
|
+
}
|
|
72364
|
+
}
|
|
72365
|
+
// Helper methods for interval consumption
|
|
72366
|
+
#consumeIntervalSlot(now2) {
|
|
72367
|
+
if (this.#strict) {
|
|
72368
|
+
this.#strictTicks.push(now2);
|
|
72369
|
+
} else {
|
|
72370
|
+
this.#intervalCount++;
|
|
72371
|
+
}
|
|
72372
|
+
}
|
|
72373
|
+
#rollbackIntervalSlot() {
|
|
72374
|
+
if (this.#strict) {
|
|
72375
|
+
if (this.#strictTicks.length > this.#strictTicksStartIndex) {
|
|
72376
|
+
this.#strictTicks.pop();
|
|
72377
|
+
}
|
|
72378
|
+
} else if (this.#intervalCount > 0) {
|
|
72379
|
+
this.#intervalCount--;
|
|
72380
|
+
}
|
|
72381
|
+
}
|
|
72382
|
+
#getActiveTicksCount() {
|
|
72383
|
+
return this.#strictTicks.length - this.#strictTicksStartIndex;
|
|
72384
|
+
}
|
|
72385
|
+
get #doesIntervalAllowAnother() {
|
|
72386
|
+
if (this.#isIntervalIgnored) {
|
|
72387
|
+
return true;
|
|
72388
|
+
}
|
|
72389
|
+
if (this.#strict) {
|
|
72390
|
+
return this.#getActiveTicksCount() < this.#intervalCap;
|
|
72391
|
+
}
|
|
72392
|
+
return this.#intervalCount < this.#intervalCap;
|
|
72393
|
+
}
|
|
72394
|
+
get #doesConcurrentAllowAnother() {
|
|
72395
|
+
return this.#pending < this.#concurrency;
|
|
72396
|
+
}
|
|
72397
|
+
#next() {
|
|
72398
|
+
this.#pending--;
|
|
72399
|
+
if (this.#pending === 0) {
|
|
72400
|
+
this.emit("pendingZero");
|
|
72401
|
+
}
|
|
72402
|
+
this.#tryToStartAnother();
|
|
72403
|
+
this.emit("next");
|
|
72404
|
+
}
|
|
72405
|
+
#onResumeInterval() {
|
|
72406
|
+
this.#timeoutId = void 0;
|
|
72407
|
+
this.#onInterval();
|
|
72408
|
+
this.#initializeIntervalIfNeeded();
|
|
72409
|
+
}
|
|
72410
|
+
#isIntervalPausedAt(now2) {
|
|
72411
|
+
if (this.#strict) {
|
|
72412
|
+
this.#cleanupStrictTicks(now2);
|
|
72413
|
+
const activeTicksCount = this.#getActiveTicksCount();
|
|
72414
|
+
if (activeTicksCount >= this.#intervalCap) {
|
|
72415
|
+
const oldestTick = this.#strictTicks[this.#strictTicksStartIndex];
|
|
72416
|
+
const delay2 = this.#interval - (now2 - oldestTick);
|
|
72417
|
+
this.#createIntervalTimeout(delay2);
|
|
72418
|
+
return true;
|
|
72419
|
+
}
|
|
72420
|
+
return false;
|
|
72421
|
+
}
|
|
72422
|
+
if (this.#intervalId === void 0) {
|
|
72423
|
+
const delay2 = this.#intervalEnd - now2;
|
|
72424
|
+
if (delay2 < 0) {
|
|
72425
|
+
if (this.#lastExecutionTime > 0) {
|
|
72426
|
+
const timeSinceLastExecution = now2 - this.#lastExecutionTime;
|
|
72427
|
+
if (timeSinceLastExecution < this.#interval) {
|
|
72428
|
+
this.#createIntervalTimeout(this.#interval - timeSinceLastExecution);
|
|
72429
|
+
return true;
|
|
72430
|
+
}
|
|
72431
|
+
}
|
|
72432
|
+
this.#intervalCount = this.#carryoverIntervalCount ? this.#pending : 0;
|
|
72433
|
+
} else {
|
|
72434
|
+
this.#createIntervalTimeout(delay2);
|
|
72435
|
+
return true;
|
|
72436
|
+
}
|
|
72437
|
+
}
|
|
72438
|
+
return false;
|
|
72439
|
+
}
|
|
72440
|
+
#createIntervalTimeout(delay2) {
|
|
72441
|
+
if (this.#timeoutId !== void 0) {
|
|
72442
|
+
return;
|
|
72443
|
+
}
|
|
72444
|
+
this.#timeoutId = setTimeout(() => {
|
|
72445
|
+
this.#onResumeInterval();
|
|
72446
|
+
}, delay2);
|
|
72447
|
+
}
|
|
72448
|
+
#clearIntervalTimer() {
|
|
72449
|
+
if (this.#intervalId) {
|
|
72450
|
+
clearInterval(this.#intervalId);
|
|
72451
|
+
this.#intervalId = void 0;
|
|
72452
|
+
}
|
|
72453
|
+
}
|
|
72454
|
+
#clearTimeoutTimer() {
|
|
72455
|
+
if (this.#timeoutId) {
|
|
72456
|
+
clearTimeout(this.#timeoutId);
|
|
72457
|
+
this.#timeoutId = void 0;
|
|
72458
|
+
}
|
|
72459
|
+
}
|
|
72460
|
+
#tryToStartAnother() {
|
|
72461
|
+
if (this.#queue.size === 0) {
|
|
72462
|
+
this.#clearIntervalTimer();
|
|
72463
|
+
this.emit("empty");
|
|
72464
|
+
if (this.#pending === 0) {
|
|
72465
|
+
this.#clearTimeoutTimer();
|
|
72466
|
+
if (this.#strict && this.#strictTicksStartIndex > 0) {
|
|
72467
|
+
const now2 = Date.now();
|
|
72468
|
+
this.#cleanupStrictTicks(now2);
|
|
72469
|
+
}
|
|
72470
|
+
this.emit("idle");
|
|
72471
|
+
}
|
|
72472
|
+
return false;
|
|
72473
|
+
}
|
|
72474
|
+
let taskStarted = false;
|
|
72475
|
+
if (!this.#isPaused) {
|
|
72476
|
+
const now2 = Date.now();
|
|
72477
|
+
const canInitializeInterval = !this.#isIntervalPausedAt(now2);
|
|
72478
|
+
if (this.#doesIntervalAllowAnother && this.#doesConcurrentAllowAnother) {
|
|
72479
|
+
const job = this.#queue.dequeue();
|
|
72480
|
+
if (!this.#isIntervalIgnored) {
|
|
72481
|
+
this.#consumeIntervalSlot(now2);
|
|
72482
|
+
this.#scheduleRateLimitUpdate();
|
|
72483
|
+
}
|
|
72484
|
+
this.emit("active");
|
|
72485
|
+
job();
|
|
72486
|
+
if (canInitializeInterval) {
|
|
72487
|
+
this.#initializeIntervalIfNeeded();
|
|
72488
|
+
}
|
|
72489
|
+
taskStarted = true;
|
|
72490
|
+
}
|
|
72491
|
+
}
|
|
72492
|
+
return taskStarted;
|
|
72493
|
+
}
|
|
72494
|
+
#initializeIntervalIfNeeded() {
|
|
72495
|
+
if (this.#isIntervalIgnored || this.#intervalId !== void 0) {
|
|
72496
|
+
return;
|
|
72497
|
+
}
|
|
72498
|
+
if (this.#strict) {
|
|
72499
|
+
return;
|
|
72500
|
+
}
|
|
72501
|
+
this.#intervalId = setInterval(() => {
|
|
72502
|
+
this.#onInterval();
|
|
72503
|
+
}, this.#interval);
|
|
72504
|
+
this.#intervalEnd = Date.now() + this.#interval;
|
|
72505
|
+
}
|
|
72506
|
+
#onInterval() {
|
|
72507
|
+
if (!this.#strict) {
|
|
72508
|
+
if (this.#intervalCount === 0 && this.#pending === 0 && this.#intervalId) {
|
|
72509
|
+
this.#clearIntervalTimer();
|
|
72510
|
+
}
|
|
72511
|
+
this.#intervalCount = this.#carryoverIntervalCount ? this.#pending : 0;
|
|
72512
|
+
}
|
|
72513
|
+
this.#processQueue();
|
|
72514
|
+
this.#scheduleRateLimitUpdate();
|
|
72515
|
+
}
|
|
72516
|
+
/**
|
|
72517
|
+
Executes all queued functions until it reaches the limit.
|
|
72518
|
+
*/
|
|
72519
|
+
#processQueue() {
|
|
72520
|
+
while (this.#tryToStartAnother()) {
|
|
72521
|
+
}
|
|
72522
|
+
}
|
|
72523
|
+
get concurrency() {
|
|
72524
|
+
return this.#concurrency;
|
|
72525
|
+
}
|
|
72526
|
+
set concurrency(newConcurrency) {
|
|
72527
|
+
if (!(typeof newConcurrency === "number" && newConcurrency >= 1)) {
|
|
72528
|
+
throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${newConcurrency}\` (${typeof newConcurrency})`);
|
|
72529
|
+
}
|
|
72530
|
+
this.#concurrency = newConcurrency;
|
|
72531
|
+
this.#processQueue();
|
|
72532
|
+
}
|
|
72533
|
+
/**
|
|
72534
|
+
Updates the priority of a promise function by its id, affecting its execution order. Requires a defined concurrency limit to take effect.
|
|
72535
|
+
|
|
72536
|
+
For example, this can be used to prioritize a promise function to run earlier.
|
|
72537
|
+
|
|
72538
|
+
```js
|
|
72539
|
+
import PQueue from 'p-queue';
|
|
72540
|
+
|
|
72541
|
+
const queue = new PQueue({concurrency: 1});
|
|
72542
|
+
|
|
72543
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
72544
|
+
queue.add(async () => '🦀', {priority: 0, id: '🦀'});
|
|
72545
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
72546
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
72547
|
+
|
|
72548
|
+
queue.setPriority('🦀', 2);
|
|
72549
|
+
```
|
|
72550
|
+
|
|
72551
|
+
In this case, the promise function with `id: '🦀'` runs second.
|
|
72552
|
+
|
|
72553
|
+
You can also deprioritize a promise function to delay its execution:
|
|
72554
|
+
|
|
72555
|
+
```js
|
|
72556
|
+
import PQueue from 'p-queue';
|
|
72557
|
+
|
|
72558
|
+
const queue = new PQueue({concurrency: 1});
|
|
72559
|
+
|
|
72560
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
72561
|
+
queue.add(async () => '🦀', {priority: 1, id: '🦀'});
|
|
72562
|
+
queue.add(async () => '🦄');
|
|
72563
|
+
queue.add(async () => '🦄', {priority: 0});
|
|
72564
|
+
|
|
72565
|
+
queue.setPriority('🦀', -1);
|
|
72566
|
+
```
|
|
72567
|
+
Here, the promise function with `id: '🦀'` executes last.
|
|
72568
|
+
*/
|
|
72569
|
+
setPriority(id, priority) {
|
|
72570
|
+
if (typeof priority !== "number" || !Number.isFinite(priority)) {
|
|
72571
|
+
throw new TypeError(`Expected \`priority\` to be a finite number, got \`${priority}\` (${typeof priority})`);
|
|
72572
|
+
}
|
|
72573
|
+
this.#queue.setPriority(id, priority);
|
|
72574
|
+
}
|
|
72575
|
+
async add(function_, options = {}) {
|
|
72576
|
+
options = {
|
|
72577
|
+
timeout: this.timeout,
|
|
72578
|
+
...options,
|
|
72579
|
+
// Assign unique ID if not provided
|
|
72580
|
+
id: options.id ?? (this.#idAssigner++).toString()
|
|
72581
|
+
};
|
|
72582
|
+
return new Promise((resolve, reject) => {
|
|
72583
|
+
const taskSymbol = Symbol(`task-${options.id}`);
|
|
72584
|
+
this.#queue.enqueue(async () => {
|
|
72585
|
+
this.#pending++;
|
|
72586
|
+
this.#runningTasks.set(taskSymbol, {
|
|
72587
|
+
id: options.id,
|
|
72588
|
+
priority: options.priority ?? 0,
|
|
72589
|
+
// Match priority-queue default
|
|
72590
|
+
startTime: Date.now(),
|
|
72591
|
+
timeout: options.timeout
|
|
72592
|
+
});
|
|
72593
|
+
let eventListener;
|
|
72594
|
+
try {
|
|
72595
|
+
try {
|
|
72596
|
+
options.signal?.throwIfAborted();
|
|
72597
|
+
} catch (error) {
|
|
72598
|
+
this.#rollbackIntervalConsumption();
|
|
72599
|
+
this.#runningTasks.delete(taskSymbol);
|
|
72600
|
+
throw error;
|
|
72601
|
+
}
|
|
72602
|
+
this.#lastExecutionTime = Date.now();
|
|
72603
|
+
let operation = function_({ signal: options.signal });
|
|
72604
|
+
if (options.timeout) {
|
|
72605
|
+
operation = pTimeout(Promise.resolve(operation), {
|
|
72606
|
+
milliseconds: options.timeout,
|
|
72607
|
+
message: `Task timed out after ${options.timeout}ms (queue has ${this.#pending} running, ${this.#queue.size} waiting)`
|
|
72608
|
+
});
|
|
72609
|
+
}
|
|
72610
|
+
if (options.signal) {
|
|
72611
|
+
const { signal } = options;
|
|
72612
|
+
operation = Promise.race([operation, new Promise((_resolve, reject2) => {
|
|
72613
|
+
eventListener = () => {
|
|
72614
|
+
reject2(signal.reason);
|
|
72615
|
+
};
|
|
72616
|
+
signal.addEventListener("abort", eventListener, { once: true });
|
|
72617
|
+
})]);
|
|
72618
|
+
}
|
|
72619
|
+
const result = await operation;
|
|
72620
|
+
resolve(result);
|
|
72621
|
+
this.emit("completed", result);
|
|
72622
|
+
} catch (error) {
|
|
72623
|
+
reject(error);
|
|
72624
|
+
this.emit("error", error);
|
|
72625
|
+
} finally {
|
|
72626
|
+
if (eventListener) {
|
|
72627
|
+
options.signal?.removeEventListener("abort", eventListener);
|
|
72628
|
+
}
|
|
72629
|
+
this.#runningTasks.delete(taskSymbol);
|
|
72630
|
+
queueMicrotask(() => {
|
|
72631
|
+
this.#next();
|
|
72632
|
+
});
|
|
72633
|
+
}
|
|
72634
|
+
}, options);
|
|
72635
|
+
this.emit("add");
|
|
72636
|
+
this.#tryToStartAnother();
|
|
72637
|
+
});
|
|
72638
|
+
}
|
|
72639
|
+
async addAll(functions2, options) {
|
|
72640
|
+
return Promise.all(functions2.map(async (function_) => this.add(function_, options)));
|
|
72641
|
+
}
|
|
72642
|
+
/**
|
|
72643
|
+
Start (or resume) executing enqueued tasks within concurrency limit. No need to call this if queue is not paused (via `options.autoStart = false` or by `.pause()` method.)
|
|
72644
|
+
*/
|
|
72645
|
+
start() {
|
|
72646
|
+
if (!this.#isPaused) {
|
|
72647
|
+
return this;
|
|
72648
|
+
}
|
|
72649
|
+
this.#isPaused = false;
|
|
72650
|
+
this.#processQueue();
|
|
72651
|
+
return this;
|
|
72652
|
+
}
|
|
72653
|
+
/**
|
|
72654
|
+
Put queue execution on hold.
|
|
72655
|
+
*/
|
|
72656
|
+
pause() {
|
|
72657
|
+
this.#isPaused = true;
|
|
72658
|
+
}
|
|
72659
|
+
/**
|
|
72660
|
+
Clear the queue.
|
|
72661
|
+
*/
|
|
72662
|
+
clear() {
|
|
72663
|
+
this.#queue = new this.#queueClass();
|
|
72664
|
+
this.#clearIntervalTimer();
|
|
72665
|
+
this.#updateRateLimitState();
|
|
72666
|
+
this.emit("empty");
|
|
72667
|
+
if (this.#pending === 0) {
|
|
72668
|
+
this.#clearTimeoutTimer();
|
|
72669
|
+
this.emit("idle");
|
|
72670
|
+
}
|
|
72671
|
+
this.emit("next");
|
|
72672
|
+
}
|
|
72673
|
+
/**
|
|
72674
|
+
Can be called multiple times. Useful if you for example add additional items at a later time.
|
|
72675
|
+
|
|
72676
|
+
@returns A promise that settles when the queue becomes empty.
|
|
72677
|
+
*/
|
|
72678
|
+
async onEmpty() {
|
|
72679
|
+
if (this.#queue.size === 0) {
|
|
72680
|
+
return;
|
|
72681
|
+
}
|
|
72682
|
+
await this.#onEvent("empty");
|
|
72683
|
+
}
|
|
72684
|
+
/**
|
|
72685
|
+
@returns A promise that settles when the queue size is less than the given limit: `queue.size < limit`.
|
|
72686
|
+
|
|
72687
|
+
If you want to avoid having the queue grow beyond a certain size you can `await queue.onSizeLessThan()` before adding a new item.
|
|
72688
|
+
|
|
72689
|
+
Note that this only limits the number of items waiting to start. There could still be up to `concurrency` jobs already running that this call does not include in its calculation.
|
|
72690
|
+
*/
|
|
72691
|
+
async onSizeLessThan(limit) {
|
|
72692
|
+
if (this.#queue.size < limit) {
|
|
72693
|
+
return;
|
|
72694
|
+
}
|
|
72695
|
+
await this.#onEvent("next", () => this.#queue.size < limit);
|
|
72696
|
+
}
|
|
72697
|
+
/**
|
|
72698
|
+
The difference with `.onEmpty` is that `.onIdle` guarantees that all work from the queue has finished. `.onEmpty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.
|
|
72699
|
+
|
|
72700
|
+
@returns A promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
|
|
72701
|
+
*/
|
|
72702
|
+
async onIdle() {
|
|
72703
|
+
if (this.#pending === 0 && this.#queue.size === 0) {
|
|
72704
|
+
return;
|
|
72705
|
+
}
|
|
72706
|
+
await this.#onEvent("idle");
|
|
72707
|
+
}
|
|
72708
|
+
/**
|
|
72709
|
+
The difference with `.onIdle` is that `.onPendingZero` only waits for currently running tasks to finish, ignoring queued tasks.
|
|
72710
|
+
|
|
72711
|
+
@returns A promise that settles when all currently running tasks have completed; `queue.pending === 0`.
|
|
72712
|
+
*/
|
|
72713
|
+
async onPendingZero() {
|
|
72714
|
+
if (this.#pending === 0) {
|
|
72715
|
+
return;
|
|
72716
|
+
}
|
|
72717
|
+
await this.#onEvent("pendingZero");
|
|
72718
|
+
}
|
|
72719
|
+
/**
|
|
72720
|
+
@returns A promise that settles when the queue becomes rate-limited due to intervalCap.
|
|
72721
|
+
*/
|
|
72722
|
+
async onRateLimit() {
|
|
72723
|
+
if (this.isRateLimited) {
|
|
72724
|
+
return;
|
|
72725
|
+
}
|
|
72726
|
+
await this.#onEvent("rateLimit");
|
|
72727
|
+
}
|
|
72728
|
+
/**
|
|
72729
|
+
@returns A promise that settles when the queue is no longer rate-limited.
|
|
72730
|
+
*/
|
|
72731
|
+
async onRateLimitCleared() {
|
|
72732
|
+
if (!this.isRateLimited) {
|
|
72733
|
+
return;
|
|
72734
|
+
}
|
|
72735
|
+
await this.#onEvent("rateLimitCleared");
|
|
72736
|
+
}
|
|
72737
|
+
/**
|
|
72738
|
+
@returns A promise that rejects when any task in the queue errors.
|
|
72739
|
+
|
|
72740
|
+
Use with `Promise.race([queue.onError(), queue.onIdle()])` to fail fast on the first error while still resolving normally when the queue goes idle.
|
|
72741
|
+
|
|
72742
|
+
Important: The promise returned by `add()` still rejects. You must handle each `add()` promise (for example, `.catch(() => {})`) to avoid unhandled rejections.
|
|
72743
|
+
|
|
72744
|
+
@example
|
|
72745
|
+
```
|
|
72746
|
+
import PQueue from 'p-queue';
|
|
72747
|
+
|
|
72748
|
+
const queue = new PQueue({concurrency: 2});
|
|
72749
|
+
|
|
72750
|
+
queue.add(() => fetchData(1)).catch(() => {});
|
|
72751
|
+
queue.add(() => fetchData(2)).catch(() => {});
|
|
72752
|
+
queue.add(() => fetchData(3)).catch(() => {});
|
|
72753
|
+
|
|
72754
|
+
// Stop processing on first error
|
|
72755
|
+
try {
|
|
72756
|
+
await Promise.race([
|
|
72757
|
+
queue.onError(),
|
|
72758
|
+
queue.onIdle()
|
|
72759
|
+
]);
|
|
72760
|
+
} catch (error) {
|
|
72761
|
+
queue.pause(); // Stop processing remaining tasks
|
|
72762
|
+
console.error('Queue failed:', error);
|
|
72763
|
+
}
|
|
72764
|
+
```
|
|
72765
|
+
*/
|
|
72766
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
72767
|
+
onError() {
|
|
72768
|
+
return new Promise((_resolve, reject) => {
|
|
72769
|
+
const handleError = (error) => {
|
|
72770
|
+
this.off("error", handleError);
|
|
72771
|
+
reject(error);
|
|
72772
|
+
};
|
|
72773
|
+
this.on("error", handleError);
|
|
72774
|
+
});
|
|
72775
|
+
}
|
|
72776
|
+
async #onEvent(event, filter2) {
|
|
72777
|
+
return new Promise((resolve) => {
|
|
72778
|
+
const listener2 = () => {
|
|
72779
|
+
if (filter2 && !filter2()) {
|
|
72780
|
+
return;
|
|
72781
|
+
}
|
|
72782
|
+
this.off(event, listener2);
|
|
72783
|
+
resolve();
|
|
72784
|
+
};
|
|
72785
|
+
this.on(event, listener2);
|
|
72786
|
+
});
|
|
72787
|
+
}
|
|
72788
|
+
/**
|
|
72789
|
+
Size of the queue, the number of queued items waiting to run.
|
|
72790
|
+
*/
|
|
72791
|
+
get size() {
|
|
72792
|
+
return this.#queue.size;
|
|
72793
|
+
}
|
|
72794
|
+
/**
|
|
72795
|
+
Size of the queue, filtered by the given options.
|
|
72796
|
+
|
|
72797
|
+
For example, this can be used to find the number of items remaining in the queue with a specific priority level.
|
|
72798
|
+
*/
|
|
72799
|
+
sizeBy(options) {
|
|
72800
|
+
return this.#queue.filter(options).length;
|
|
72801
|
+
}
|
|
72802
|
+
/**
|
|
72803
|
+
Number of running items (no longer in the queue).
|
|
72804
|
+
*/
|
|
72805
|
+
get pending() {
|
|
72806
|
+
return this.#pending;
|
|
72807
|
+
}
|
|
72808
|
+
/**
|
|
72809
|
+
Whether the queue is currently paused.
|
|
72810
|
+
*/
|
|
72811
|
+
get isPaused() {
|
|
72812
|
+
return this.#isPaused;
|
|
72813
|
+
}
|
|
72814
|
+
#setupRateLimitTracking() {
|
|
72815
|
+
if (this.#isIntervalIgnored) {
|
|
72816
|
+
return;
|
|
72817
|
+
}
|
|
72818
|
+
this.on("add", () => {
|
|
72819
|
+
if (this.#queue.size > 0) {
|
|
72820
|
+
this.#scheduleRateLimitUpdate();
|
|
72821
|
+
}
|
|
72822
|
+
});
|
|
72823
|
+
this.on("next", () => {
|
|
72824
|
+
this.#scheduleRateLimitUpdate();
|
|
72825
|
+
});
|
|
72826
|
+
}
|
|
72827
|
+
#scheduleRateLimitUpdate() {
|
|
72828
|
+
if (this.#isIntervalIgnored || this.#rateLimitFlushScheduled) {
|
|
72829
|
+
return;
|
|
72830
|
+
}
|
|
72831
|
+
this.#rateLimitFlushScheduled = true;
|
|
72832
|
+
queueMicrotask(() => {
|
|
72833
|
+
this.#rateLimitFlushScheduled = false;
|
|
72834
|
+
this.#updateRateLimitState();
|
|
72835
|
+
});
|
|
72836
|
+
}
|
|
72837
|
+
#rollbackIntervalConsumption() {
|
|
72838
|
+
if (this.#isIntervalIgnored) {
|
|
72839
|
+
return;
|
|
72840
|
+
}
|
|
72841
|
+
this.#rollbackIntervalSlot();
|
|
72842
|
+
this.#scheduleRateLimitUpdate();
|
|
72843
|
+
}
|
|
72844
|
+
#updateRateLimitState() {
|
|
72845
|
+
const previous = this.#rateLimitedInInterval;
|
|
72846
|
+
if (this.#isIntervalIgnored || this.#queue.size === 0) {
|
|
72847
|
+
if (previous) {
|
|
72848
|
+
this.#rateLimitedInInterval = false;
|
|
72849
|
+
this.emit("rateLimitCleared");
|
|
72850
|
+
}
|
|
72851
|
+
return;
|
|
72852
|
+
}
|
|
72853
|
+
let count2;
|
|
72854
|
+
if (this.#strict) {
|
|
72855
|
+
const now2 = Date.now();
|
|
72856
|
+
this.#cleanupStrictTicks(now2);
|
|
72857
|
+
count2 = this.#getActiveTicksCount();
|
|
72858
|
+
} else {
|
|
72859
|
+
count2 = this.#intervalCount;
|
|
72860
|
+
}
|
|
72861
|
+
const shouldBeRateLimited = count2 >= this.#intervalCap;
|
|
72862
|
+
if (shouldBeRateLimited !== previous) {
|
|
72863
|
+
this.#rateLimitedInInterval = shouldBeRateLimited;
|
|
72864
|
+
this.emit(shouldBeRateLimited ? "rateLimit" : "rateLimitCleared");
|
|
72865
|
+
}
|
|
72866
|
+
}
|
|
72867
|
+
/**
|
|
72868
|
+
Whether the queue is currently rate-limited due to intervalCap.
|
|
72869
|
+
*/
|
|
72870
|
+
get isRateLimited() {
|
|
72871
|
+
return this.#rateLimitedInInterval;
|
|
72872
|
+
}
|
|
72873
|
+
/**
|
|
72874
|
+
Whether the queue is saturated. Returns `true` when:
|
|
72875
|
+
- All concurrency slots are occupied and tasks are waiting, OR
|
|
72876
|
+
- The queue is rate-limited and tasks are waiting
|
|
72877
|
+
|
|
72878
|
+
Useful for detecting backpressure and potential hanging tasks.
|
|
72879
|
+
|
|
72880
|
+
```js
|
|
72881
|
+
import PQueue from 'p-queue';
|
|
72882
|
+
|
|
72883
|
+
const queue = new PQueue({concurrency: 2});
|
|
72884
|
+
|
|
72885
|
+
// Backpressure handling
|
|
72886
|
+
if (queue.isSaturated) {
|
|
72887
|
+
console.log('Queue is saturated, waiting for capacity...');
|
|
72888
|
+
await queue.onSizeLessThan(queue.concurrency);
|
|
72889
|
+
}
|
|
72890
|
+
|
|
72891
|
+
// Monitoring for stuck tasks
|
|
72892
|
+
setInterval(() => {
|
|
72893
|
+
if (queue.isSaturated) {
|
|
72894
|
+
console.warn(`Queue saturated: ${queue.pending} running, ${queue.size} waiting`);
|
|
72895
|
+
}
|
|
72896
|
+
}, 60000);
|
|
72897
|
+
```
|
|
72898
|
+
*/
|
|
72899
|
+
get isSaturated() {
|
|
72900
|
+
return this.#pending === this.#concurrency && this.#queue.size > 0 || this.isRateLimited && this.#queue.size > 0;
|
|
72901
|
+
}
|
|
72902
|
+
/**
|
|
72903
|
+
The tasks currently being executed. Each task includes its `id`, `priority`, `startTime`, and `timeout` (if set).
|
|
72904
|
+
|
|
72905
|
+
Returns an array of task info objects.
|
|
72906
|
+
|
|
72907
|
+
```js
|
|
72908
|
+
import PQueue from 'p-queue';
|
|
72909
|
+
|
|
72910
|
+
const queue = new PQueue({concurrency: 2});
|
|
72911
|
+
|
|
72912
|
+
// Add tasks with IDs for better debugging
|
|
72913
|
+
queue.add(() => fetchUser(123), {id: 'user-123'});
|
|
72914
|
+
queue.add(() => fetchPosts(456), {id: 'posts-456', priority: 1});
|
|
72915
|
+
|
|
72916
|
+
// Check what's running
|
|
72917
|
+
console.log(queue.runningTasks);
|
|
72918
|
+
// => [{
|
|
72919
|
+
// id: 'user-123',
|
|
72920
|
+
// priority: 0,
|
|
72921
|
+
// startTime: 1759253001716,
|
|
72922
|
+
// timeout: undefined
|
|
72923
|
+
// }, {
|
|
72924
|
+
// id: 'posts-456',
|
|
72925
|
+
// priority: 1,
|
|
72926
|
+
// startTime: 1759253001916,
|
|
72927
|
+
// timeout: undefined
|
|
72928
|
+
// }]
|
|
72929
|
+
```
|
|
72930
|
+
*/
|
|
72931
|
+
get runningTasks() {
|
|
72932
|
+
return [...this.#runningTasks.values()].map((task) => ({ ...task }));
|
|
72933
|
+
}
|
|
72934
|
+
}
|
|
71939
72935
|
function cleanSessionCache(sessionIdentifier2) {
|
|
71940
72936
|
for (const storage of [sessionStorage, localStorage]) {
|
|
71941
72937
|
const keys = Object.keys(storage);
|
|
@@ -72312,7 +73308,7 @@ Inferred class string: "${iconClasses}."`
|
|
|
72312
73308
|
}
|
|
72313
73309
|
comparison = aTime - bTime;
|
|
72314
73310
|
} else {
|
|
72315
|
-
comparison = String(aVal).localeCompare(String(bVal));
|
|
73311
|
+
comparison = String(aVal).localeCompare(String(bVal), void 0, { sensitivity: "base" });
|
|
72316
73312
|
}
|
|
72317
73313
|
return desc ? -comparison : comparison;
|
|
72318
73314
|
});
|
|
@@ -74757,6 +75753,25 @@ Inferred class string: "${iconClasses}."`
|
|
|
74757
75753
|
const STORE_VARIABLE_MAP = /* @__PURE__ */ new Map();
|
|
74758
75754
|
const STORE_SEQUENCE_MAP = /* @__PURE__ */ new Map();
|
|
74759
75755
|
const STORE_LATEST_VALUE_MAP = /* @__PURE__ */ new Map();
|
|
75756
|
+
const STORE_RECOVERY_IN_PROGRESS = /* @__PURE__ */ new Set();
|
|
75757
|
+
class PatchQueue {
|
|
75758
|
+
constructor() {
|
|
75759
|
+
this.queues = /* @__PURE__ */ new Map();
|
|
75760
|
+
}
|
|
75761
|
+
getQueue(storeUid) {
|
|
75762
|
+
let queue = this.queues.get(storeUid);
|
|
75763
|
+
if (!queue) {
|
|
75764
|
+
queue = new PQueue({ concurrency: 1 });
|
|
75765
|
+
this.queues.set(storeUid, queue);
|
|
75766
|
+
}
|
|
75767
|
+
return queue;
|
|
75768
|
+
}
|
|
75769
|
+
enqueue(storeUid, patches, sequenceNumber, applyFn) {
|
|
75770
|
+
const queue = this.getQueue(storeUid);
|
|
75771
|
+
queue.add(() => applyFn(storeUid, patches, sequenceNumber));
|
|
75772
|
+
}
|
|
75773
|
+
}
|
|
75774
|
+
const PATCH_QUEUE = new PatchQueue();
|
|
74760
75775
|
const ROUTE_MATCHES_KEY = "__route_matches";
|
|
74761
75776
|
function BackendStoreSync({ children }) {
|
|
74762
75777
|
const { client: client2 } = React__namespace.useContext(websocketCtx);
|
|
@@ -74774,7 +75789,7 @@ Inferred class string: "${iconClasses}."`
|
|
|
74774
75789
|
async ({ diff }) => {
|
|
74775
75790
|
const extrasMap = /* @__PURE__ */ new Map();
|
|
74776
75791
|
Array.from(diff.entries()).filter(
|
|
74777
|
-
([itemKey, value]) => !STORE_LATEST_VALUE_MAP.has(itemKey) || STORE_LATEST_VALUE_MAP.get(itemKey)
|
|
75792
|
+
([itemKey, value]) => !STORE_LATEST_VALUE_MAP.has(itemKey) || !isEqual$4(STORE_LATEST_VALUE_MAP.get(itemKey), value)
|
|
74778
75793
|
).forEach(([itemKey, value]) => {
|
|
74779
75794
|
STORE_LATEST_VALUE_MAP.set(itemKey, value);
|
|
74780
75795
|
const extras = STORE_EXTRAS_MAP.get(itemKey);
|
|
@@ -74810,9 +75825,42 @@ Inferred class string: "${iconClasses}."`
|
|
|
74810
75825
|
({ snapshot, set: set2 }) => async (storeUid, patches, sequenceNumber) => {
|
|
74811
75826
|
const expectedSequence = STORE_SEQUENCE_MAP.get(storeUid) || 0;
|
|
74812
75827
|
if (sequenceNumber !== expectedSequence + 1) {
|
|
75828
|
+
if (sequenceNumber <= expectedSequence) {
|
|
75829
|
+
return;
|
|
75830
|
+
}
|
|
74813
75831
|
console.warn(
|
|
74814
|
-
`Sequence number mismatch for store ${storeUid}. Expected: ${expectedSequence + 1}, Got: ${sequenceNumber}
|
|
75832
|
+
`Sequence number mismatch for store ${storeUid}. Expected: ${expectedSequence + 1}, Got: ${sequenceNumber}.`
|
|
74815
75833
|
);
|
|
75834
|
+
if (!STORE_RECOVERY_IN_PROGRESS.has(storeUid)) {
|
|
75835
|
+
STORE_RECOVERY_IN_PROGRESS.add(storeUid);
|
|
75836
|
+
console.warn(`Fetching full state for store ${storeUid} to recover.`);
|
|
75837
|
+
try {
|
|
75838
|
+
const fullValue = await getStoreValue(storeUid);
|
|
75839
|
+
const variableUids2 = STORE_VARIABLE_MAP.get(storeUid);
|
|
75840
|
+
if (variableUids2) {
|
|
75841
|
+
for (const variableUid of variableUids2) {
|
|
75842
|
+
const directAtom = atomRegistry.get(variableUid);
|
|
75843
|
+
if (directAtom) {
|
|
75844
|
+
set2(directAtom, fullValue);
|
|
75845
|
+
continue;
|
|
75846
|
+
}
|
|
75847
|
+
const atomFamily2 = atomFamilyRegistry.get(variableUid);
|
|
75848
|
+
if (atomFamily2) {
|
|
75849
|
+
const familyMembers = atomFamilyMembersRegistry.get(atomFamily2);
|
|
75850
|
+
if (familyMembers) {
|
|
75851
|
+
for (const [, atomInstance] of familyMembers) {
|
|
75852
|
+
set2(atomInstance, fullValue);
|
|
75853
|
+
}
|
|
75854
|
+
}
|
|
75855
|
+
}
|
|
75856
|
+
}
|
|
75857
|
+
}
|
|
75858
|
+
} catch (e2) {
|
|
75859
|
+
console.error(`Failed to recover store ${storeUid} after sequence mismatch:`, e2);
|
|
75860
|
+
} finally {
|
|
75861
|
+
STORE_RECOVERY_IN_PROGRESS.delete(storeUid);
|
|
75862
|
+
}
|
|
75863
|
+
}
|
|
74816
75864
|
return;
|
|
74817
75865
|
}
|
|
74818
75866
|
STORE_SEQUENCE_MAP.set(storeUid, sequenceNumber);
|
|
@@ -74887,7 +75935,7 @@ Inferred class string: "${iconClasses}."`
|
|
|
74887
75935
|
STORE_LATEST_VALUE_MAP.set(message.store_uid, message.value);
|
|
74888
75936
|
});
|
|
74889
75937
|
const patchSub = client2.backendStorePatchMessages$().subscribe((message) => {
|
|
74890
|
-
|
|
75938
|
+
PATCH_QUEUE.enqueue(message.store_uid, message.patches, message.sequence_number, applyPatchesToAtoms);
|
|
74891
75939
|
});
|
|
74892
75940
|
return () => {
|
|
74893
75941
|
valueSub.unsubscribe();
|