@tanstack/pacer 0.22.0 → 0.23.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/README.md +4 -4
- package/dist/async-batcher.d.ts +11 -13
- package/dist/async-batcher.js +185 -131
- package/dist/async-debouncer.d.ts +6 -8
- package/dist/async-debouncer.js +186 -142
- package/dist/async-queuer.d.ts +11 -13
- package/dist/async-queuer.js +383 -290
- package/dist/async-rate-limiter.d.ts +6 -8
- package/dist/async-rate-limiter.js +197 -143
- package/dist/async-retryer.d.ts +6 -8
- package/dist/async-retryer.js +209 -179
- package/dist/async-throttler.d.ts +6 -8
- package/dist/async-throttler.js +214 -157
- package/dist/batcher.d.ts +5 -7
- package/dist/batcher.js +107 -87
- package/dist/debouncer.d.ts +6 -8
- package/dist/debouncer.js +99 -86
- package/dist/event-client.d.ts +8 -10
- package/dist/event-client.js +1 -2
- package/dist/queuer.d.ts +7 -9
- package/dist/queuer.js +277 -212
- package/dist/rate-limiter.d.ts +6 -8
- package/dist/rate-limiter.js +127 -109
- package/dist/throttler.d.ts +6 -8
- package/dist/throttler.js +127 -90
- package/dist/types.d.ts +4 -6
- package/dist/utils.d.ts +3 -5
- package/dist/utils.js +1 -2
- package/package.json +21 -68
- package/dist/async-batcher.cjs +0 -337
- package/dist/async-batcher.cjs.map +0 -1
- package/dist/async-batcher.d.cts +0 -343
- package/dist/async-batcher.js.map +0 -1
- package/dist/async-debouncer.cjs +0 -327
- package/dist/async-debouncer.cjs.map +0 -1
- package/dist/async-debouncer.d.cts +0 -299
- package/dist/async-debouncer.js.map +0 -1
- package/dist/async-queuer.cjs +0 -516
- package/dist/async-queuer.cjs.map +0 -1
- package/dist/async-queuer.d.cts +0 -440
- package/dist/async-queuer.js.map +0 -1
- package/dist/async-rate-limiter.cjs +0 -370
- package/dist/async-rate-limiter.cjs.map +0 -1
- package/dist/async-rate-limiter.d.cts +0 -356
- package/dist/async-rate-limiter.js.map +0 -1
- package/dist/async-retryer.cjs +0 -365
- package/dist/async-retryer.cjs.map +0 -1
- package/dist/async-retryer.d.cts +0 -321
- package/dist/async-retryer.js.map +0 -1
- package/dist/async-throttler.cjs +0 -344
- package/dist/async-throttler.cjs.map +0 -1
- package/dist/async-throttler.d.cts +0 -319
- package/dist/async-throttler.js.map +0 -1
- package/dist/batcher.cjs +0 -200
- package/dist/batcher.cjs.map +0 -1
- package/dist/batcher.d.cts +0 -179
- package/dist/batcher.js.map +0 -1
- package/dist/debouncer.cjs +0 -203
- package/dist/debouncer.cjs.map +0 -1
- package/dist/debouncer.d.cts +0 -166
- package/dist/debouncer.js.map +0 -1
- package/dist/event-client.cjs +0 -64
- package/dist/event-client.cjs.map +0 -1
- package/dist/event-client.d.cts +0 -65
- package/dist/event-client.js.map +0 -1
- package/dist/index.cjs +0 -52
- package/dist/index.d.cts +0 -15
- package/dist/queuer.cjs +0 -406
- package/dist/queuer.cjs.map +0 -1
- package/dist/queuer.d.cts +0 -345
- package/dist/queuer.js.map +0 -1
- package/dist/rate-limiter.cjs +0 -263
- package/dist/rate-limiter.cjs.map +0 -1
- package/dist/rate-limiter.d.cts +0 -214
- package/dist/rate-limiter.js.map +0 -1
- package/dist/throttler.cjs +0 -215
- package/dist/throttler.cjs.map +0 -1
- package/dist/throttler.d.cts +0 -206
- package/dist/throttler.js.map +0 -1
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +0 -13
- package/dist/utils.cjs +0 -13
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.cts +0 -7
- package/dist/utils.js.map +0 -1
- package/src/async-batcher.ts +0 -594
- package/src/async-debouncer.ts +0 -566
- package/src/async-queuer.ts +0 -988
- package/src/async-rate-limiter.ts +0 -648
- package/src/async-retryer.ts +0 -673
- package/src/async-throttler.ts +0 -634
- package/src/batcher.ts +0 -329
- package/src/debouncer.ts +0 -334
- package/src/event-client.ts +0 -129
- package/src/index.ts +0 -24
- package/src/queuer.ts +0 -751
- package/src/rate-limiter.ts +0 -429
- package/src/throttler.ts +0 -380
- package/src/types.ts +0 -12
- package/src/utils.ts +0 -12
package/dist/event-client.cjs
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
let _tanstack_devtools_event_client = require("@tanstack/devtools-event-client");
|
|
3
|
-
|
|
4
|
-
//#region src/event-client.ts
|
|
5
|
-
const pacerDevtoolsInstancesByKey = /* @__PURE__ */ new Map();
|
|
6
|
-
function registerPacerDevtoolsInstance(key, instance) {
|
|
7
|
-
pacerDevtoolsInstancesByKey.set(key, instance);
|
|
8
|
-
}
|
|
9
|
-
function getPacerDevtoolsInstance(key) {
|
|
10
|
-
return pacerDevtoolsInstancesByKey.get(key);
|
|
11
|
-
}
|
|
12
|
-
function cloneJsonSafe(value) {
|
|
13
|
-
if (value === void 0) return;
|
|
14
|
-
try {
|
|
15
|
-
return JSON.parse(JSON.stringify(value));
|
|
16
|
-
} catch {
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function readStoreSnapshot(store) {
|
|
21
|
-
if (typeof store.get === "function") return store.get();
|
|
22
|
-
return store.state;
|
|
23
|
-
}
|
|
24
|
-
function toPacerDevtoolsWirePayload(instance) {
|
|
25
|
-
return {
|
|
26
|
-
key: instance.key,
|
|
27
|
-
store: { state: cloneJsonSafe(readStoreSnapshot(instance.store)) },
|
|
28
|
-
options: cloneJsonSafe(instance.options)
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
var PacerEventClient = class extends _tanstack_devtools_event_client.EventClient {
|
|
32
|
-
constructor(props) {
|
|
33
|
-
super({
|
|
34
|
-
pluginId: "pacer",
|
|
35
|
-
debug: props?.debug,
|
|
36
|
-
/**
|
|
37
|
-
* TanStack Devtools starts `ClientEventBus` inside an async dynamic import
|
|
38
|
-
* after the first commit. Pacer utils often emit during the first render
|
|
39
|
-
* (constructor → store sync). `EventClient` only retries `tanstack-connect`
|
|
40
|
-
* `#maxRetries` times at `reconnectEveryMs` before it gives up and drops
|
|
41
|
-
* all queued events permanently. Default 300ms × 5 ≈ 1.5s is often too
|
|
42
|
-
* short; Form uses 1000ms here for the same reason.
|
|
43
|
-
*/
|
|
44
|
-
reconnectEveryMs: 1e3
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
const emitChange = (event, instance) => {
|
|
49
|
-
const key = instance.key;
|
|
50
|
-
if (!key) return;
|
|
51
|
-
registerPacerDevtoolsInstance(key, instance);
|
|
52
|
-
pacerEventClient.emit(event, toPacerDevtoolsWirePayload({
|
|
53
|
-
...instance,
|
|
54
|
-
key
|
|
55
|
-
}));
|
|
56
|
-
};
|
|
57
|
-
const pacerEventClient = new PacerEventClient();
|
|
58
|
-
|
|
59
|
-
//#endregion
|
|
60
|
-
exports.emitChange = emitChange;
|
|
61
|
-
exports.getPacerDevtoolsInstance = getPacerDevtoolsInstance;
|
|
62
|
-
exports.pacerEventClient = pacerEventClient;
|
|
63
|
-
exports.registerPacerDevtoolsInstance = registerPacerDevtoolsInstance;
|
|
64
|
-
//# sourceMappingURL=event-client.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"event-client.cjs","names":["EventClient"],"sources":["../src/event-client.ts"],"sourcesContent":["import { EventClient } from '@tanstack/devtools-event-client'\n\n/**\n * Payload on the devtools event bus must be JSON-serializable: `ClientEventBus`\n * always stringifies events for `emitToServer` and `BroadcastChannel`, so live\n * util instances (with reactive `store` graphs) cannot be sent as-is.\n *\n * Live instances are kept in {@link pacerDevtoolsInstancesByKey} for the panel.\n */\nexport interface PacerDevtoolsWirePayload {\n key: string\n store: { state: unknown }\n options: unknown\n}\n\nconst pacerDevtoolsInstancesByKey = new Map<string, unknown>()\n\nexport function registerPacerDevtoolsInstance(\n key: string,\n instance: unknown,\n): void {\n pacerDevtoolsInstancesByKey.set(key, instance)\n}\n\nexport function getPacerDevtoolsInstance(key: string): unknown {\n return pacerDevtoolsInstancesByKey.get(key)\n}\n\nfunction cloneJsonSafe(value: unknown): unknown {\n if (value === undefined) {\n return undefined\n }\n try {\n return JSON.parse(JSON.stringify(value)) as unknown\n } catch {\n return null\n }\n}\n\nfunction readStoreSnapshot(store: {\n get?: () => unknown\n state?: unknown\n}): unknown {\n if (typeof store.get === 'function') {\n return store.get()\n }\n return store.state\n}\n\nfunction toPacerDevtoolsWirePayload(instance: {\n key: string\n store: { get?: () => unknown; state?: unknown }\n options: unknown\n}): PacerDevtoolsWirePayload {\n return {\n key: instance.key,\n store: { state: cloneJsonSafe(readStoreSnapshot(instance.store)) },\n options: cloneJsonSafe(instance.options),\n }\n}\n\n/**\n * Suffix-only keys: {@link EventClient} prepends `pluginId:` (`pacer:`) at runtime\n * for `emit` / `on`. Wire `type` values are `pacer:${key}`.\n */\nexport interface PacerEventMap {\n 'd-AsyncBatcher': PacerDevtoolsWirePayload\n 'd-AsyncDebouncer': PacerDevtoolsWirePayload\n 'd-AsyncQueuer': PacerDevtoolsWirePayload\n 'd-AsyncRateLimiter': PacerDevtoolsWirePayload\n 'd-AsyncRetryer': PacerDevtoolsWirePayload\n 'd-AsyncThrottler': PacerDevtoolsWirePayload\n 'd-Batcher': PacerDevtoolsWirePayload\n 'd-Debouncer': PacerDevtoolsWirePayload\n 'd-Queuer': PacerDevtoolsWirePayload\n 'd-RateLimiter': PacerDevtoolsWirePayload\n 'd-Throttler': PacerDevtoolsWirePayload\n AsyncBatcher: PacerDevtoolsWirePayload\n AsyncDebouncer: PacerDevtoolsWirePayload\n AsyncQueuer: PacerDevtoolsWirePayload\n AsyncRateLimiter: PacerDevtoolsWirePayload\n AsyncRetryer: PacerDevtoolsWirePayload\n AsyncThrottler: PacerDevtoolsWirePayload\n Batcher: PacerDevtoolsWirePayload\n Debouncer: PacerDevtoolsWirePayload\n Queuer: PacerDevtoolsWirePayload\n RateLimiter: PacerDevtoolsWirePayload\n Throttler: PacerDevtoolsWirePayload\n}\n\nexport type PacerEventName = keyof PacerEventMap\n\ntype PacerUtilEmitSource = {\n key?: string\n store: { get?: () => unknown; state?: unknown }\n options: unknown\n}\n\nclass PacerEventClient extends EventClient<PacerEventMap> {\n constructor(props?: { debug: boolean }) {\n super({\n pluginId: 'pacer',\n debug: props?.debug,\n /**\n * TanStack Devtools starts `ClientEventBus` inside an async dynamic import\n * after the first commit. Pacer utils often emit during the first render\n * (constructor → store sync). `EventClient` only retries `tanstack-connect`\n * `#maxRetries` times at `reconnectEveryMs` before it gives up and drops\n * all queued events permanently. Default 300ms × 5 ≈ 1.5s is often too\n * short; Form uses 1000ms here for the same reason.\n */\n reconnectEveryMs: 1000,\n })\n }\n}\n\nexport const emitChange = <TEvent extends keyof PacerEventMap>(\n event: TEvent,\n instance: PacerUtilEmitSource,\n) => {\n const key = instance.key\n if (!key) {\n return\n }\n registerPacerDevtoolsInstance(key, instance)\n pacerEventClient.emit(event, toPacerDevtoolsWirePayload({ ...instance, key }))\n}\n\nexport const pacerEventClient = new PacerEventClient()\n"],"mappings":";;;;AAeA,MAAM,8CAA8B,IAAI,IAAqB;AAE7D,SAAgB,8BACd,KACA,UACM;CACN,4BAA4B,IAAI,KAAK,QAAQ;AAC/C;AAEA,SAAgB,yBAAyB,KAAsB;CAC7D,OAAO,4BAA4B,IAAI,GAAG;AAC5C;AAEA,SAAS,cAAc,OAAyB;CAC9C,IAAI,UAAU,QACZ;CAEF,IAAI;EACF,OAAO,KAAK,MAAM,KAAK,UAAU,KAAK,CAAC;CACzC,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAS,kBAAkB,OAGf;CACV,IAAI,OAAO,MAAM,QAAQ,YACvB,OAAO,MAAM,IAAI;CAEnB,OAAO,MAAM;AACf;AAEA,SAAS,2BAA2B,UAIP;CAC3B,OAAO;EACL,KAAK,SAAS;EACd,OAAO,EAAE,OAAO,cAAc,kBAAkB,SAAS,KAAK,CAAC,EAAE;EACjE,SAAS,cAAc,SAAS,OAAO;CACzC;AACF;AAuCA,IAAM,mBAAN,cAA+BA,4CAA2B;CACxD,YAAY,OAA4B;EACtC,MAAM;GACJ,UAAU;GACV,OAAO,OAAO;;;;;;;;;GASd,kBAAkB;EACpB,CAAC;CACH;AACF;AAEA,MAAa,cACX,OACA,aACG;CACH,MAAM,MAAM,SAAS;CACrB,IAAI,CAAC,KACH;CAEF,8BAA8B,KAAK,QAAQ;CAC3C,iBAAiB,KAAK,OAAO,2BAA2B;EAAE,GAAG;EAAU;CAAI,CAAC,CAAC;AAC/E;AAEA,MAAa,mBAAmB,IAAI,iBAAiB"}
|
package/dist/event-client.d.cts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { EventClient } from "@tanstack/devtools-event-client";
|
|
2
|
-
//#region src/event-client.d.ts
|
|
3
|
-
/**
|
|
4
|
-
* Payload on the devtools event bus must be JSON-serializable: `ClientEventBus`
|
|
5
|
-
* always stringifies events for `emitToServer` and `BroadcastChannel`, so live
|
|
6
|
-
* util instances (with reactive `store` graphs) cannot be sent as-is.
|
|
7
|
-
*
|
|
8
|
-
* Live instances are kept in {@link pacerDevtoolsInstancesByKey} for the panel.
|
|
9
|
-
*/
|
|
10
|
-
interface PacerDevtoolsWirePayload {
|
|
11
|
-
key: string;
|
|
12
|
-
store: {
|
|
13
|
-
state: unknown;
|
|
14
|
-
};
|
|
15
|
-
options: unknown;
|
|
16
|
-
}
|
|
17
|
-
declare function registerPacerDevtoolsInstance(key: string, instance: unknown): void;
|
|
18
|
-
declare function getPacerDevtoolsInstance(key: string): unknown;
|
|
19
|
-
/**
|
|
20
|
-
* Suffix-only keys: {@link EventClient} prepends `pluginId:` (`pacer:`) at runtime
|
|
21
|
-
* for `emit` / `on`. Wire `type` values are `pacer:${key}`.
|
|
22
|
-
*/
|
|
23
|
-
interface PacerEventMap {
|
|
24
|
-
'd-AsyncBatcher': PacerDevtoolsWirePayload;
|
|
25
|
-
'd-AsyncDebouncer': PacerDevtoolsWirePayload;
|
|
26
|
-
'd-AsyncQueuer': PacerDevtoolsWirePayload;
|
|
27
|
-
'd-AsyncRateLimiter': PacerDevtoolsWirePayload;
|
|
28
|
-
'd-AsyncRetryer': PacerDevtoolsWirePayload;
|
|
29
|
-
'd-AsyncThrottler': PacerDevtoolsWirePayload;
|
|
30
|
-
'd-Batcher': PacerDevtoolsWirePayload;
|
|
31
|
-
'd-Debouncer': PacerDevtoolsWirePayload;
|
|
32
|
-
'd-Queuer': PacerDevtoolsWirePayload;
|
|
33
|
-
'd-RateLimiter': PacerDevtoolsWirePayload;
|
|
34
|
-
'd-Throttler': PacerDevtoolsWirePayload;
|
|
35
|
-
AsyncBatcher: PacerDevtoolsWirePayload;
|
|
36
|
-
AsyncDebouncer: PacerDevtoolsWirePayload;
|
|
37
|
-
AsyncQueuer: PacerDevtoolsWirePayload;
|
|
38
|
-
AsyncRateLimiter: PacerDevtoolsWirePayload;
|
|
39
|
-
AsyncRetryer: PacerDevtoolsWirePayload;
|
|
40
|
-
AsyncThrottler: PacerDevtoolsWirePayload;
|
|
41
|
-
Batcher: PacerDevtoolsWirePayload;
|
|
42
|
-
Debouncer: PacerDevtoolsWirePayload;
|
|
43
|
-
Queuer: PacerDevtoolsWirePayload;
|
|
44
|
-
RateLimiter: PacerDevtoolsWirePayload;
|
|
45
|
-
Throttler: PacerDevtoolsWirePayload;
|
|
46
|
-
}
|
|
47
|
-
type PacerEventName = keyof PacerEventMap;
|
|
48
|
-
type PacerUtilEmitSource = {
|
|
49
|
-
key?: string;
|
|
50
|
-
store: {
|
|
51
|
-
get?: () => unknown;
|
|
52
|
-
state?: unknown;
|
|
53
|
-
};
|
|
54
|
-
options: unknown;
|
|
55
|
-
};
|
|
56
|
-
declare class PacerEventClient extends EventClient<PacerEventMap> {
|
|
57
|
-
constructor(props?: {
|
|
58
|
-
debug: boolean;
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
declare const emitChange: <TEvent extends keyof PacerEventMap>(event: TEvent, instance: PacerUtilEmitSource) => void;
|
|
62
|
-
declare const pacerEventClient: PacerEventClient;
|
|
63
|
-
//#endregion
|
|
64
|
-
export { PacerDevtoolsWirePayload, PacerEventMap, PacerEventName, emitChange, getPacerDevtoolsInstance, pacerEventClient, registerPacerDevtoolsInstance };
|
|
65
|
-
//# sourceMappingURL=event-client.d.cts.map
|
package/dist/event-client.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"event-client.js","names":[],"sources":["../src/event-client.ts"],"sourcesContent":["import { EventClient } from '@tanstack/devtools-event-client'\n\n/**\n * Payload on the devtools event bus must be JSON-serializable: `ClientEventBus`\n * always stringifies events for `emitToServer` and `BroadcastChannel`, so live\n * util instances (with reactive `store` graphs) cannot be sent as-is.\n *\n * Live instances are kept in {@link pacerDevtoolsInstancesByKey} for the panel.\n */\nexport interface PacerDevtoolsWirePayload {\n key: string\n store: { state: unknown }\n options: unknown\n}\n\nconst pacerDevtoolsInstancesByKey = new Map<string, unknown>()\n\nexport function registerPacerDevtoolsInstance(\n key: string,\n instance: unknown,\n): void {\n pacerDevtoolsInstancesByKey.set(key, instance)\n}\n\nexport function getPacerDevtoolsInstance(key: string): unknown {\n return pacerDevtoolsInstancesByKey.get(key)\n}\n\nfunction cloneJsonSafe(value: unknown): unknown {\n if (value === undefined) {\n return undefined\n }\n try {\n return JSON.parse(JSON.stringify(value)) as unknown\n } catch {\n return null\n }\n}\n\nfunction readStoreSnapshot(store: {\n get?: () => unknown\n state?: unknown\n}): unknown {\n if (typeof store.get === 'function') {\n return store.get()\n }\n return store.state\n}\n\nfunction toPacerDevtoolsWirePayload(instance: {\n key: string\n store: { get?: () => unknown; state?: unknown }\n options: unknown\n}): PacerDevtoolsWirePayload {\n return {\n key: instance.key,\n store: { state: cloneJsonSafe(readStoreSnapshot(instance.store)) },\n options: cloneJsonSafe(instance.options),\n }\n}\n\n/**\n * Suffix-only keys: {@link EventClient} prepends `pluginId:` (`pacer:`) at runtime\n * for `emit` / `on`. Wire `type` values are `pacer:${key}`.\n */\nexport interface PacerEventMap {\n 'd-AsyncBatcher': PacerDevtoolsWirePayload\n 'd-AsyncDebouncer': PacerDevtoolsWirePayload\n 'd-AsyncQueuer': PacerDevtoolsWirePayload\n 'd-AsyncRateLimiter': PacerDevtoolsWirePayload\n 'd-AsyncRetryer': PacerDevtoolsWirePayload\n 'd-AsyncThrottler': PacerDevtoolsWirePayload\n 'd-Batcher': PacerDevtoolsWirePayload\n 'd-Debouncer': PacerDevtoolsWirePayload\n 'd-Queuer': PacerDevtoolsWirePayload\n 'd-RateLimiter': PacerDevtoolsWirePayload\n 'd-Throttler': PacerDevtoolsWirePayload\n AsyncBatcher: PacerDevtoolsWirePayload\n AsyncDebouncer: PacerDevtoolsWirePayload\n AsyncQueuer: PacerDevtoolsWirePayload\n AsyncRateLimiter: PacerDevtoolsWirePayload\n AsyncRetryer: PacerDevtoolsWirePayload\n AsyncThrottler: PacerDevtoolsWirePayload\n Batcher: PacerDevtoolsWirePayload\n Debouncer: PacerDevtoolsWirePayload\n Queuer: PacerDevtoolsWirePayload\n RateLimiter: PacerDevtoolsWirePayload\n Throttler: PacerDevtoolsWirePayload\n}\n\nexport type PacerEventName = keyof PacerEventMap\n\ntype PacerUtilEmitSource = {\n key?: string\n store: { get?: () => unknown; state?: unknown }\n options: unknown\n}\n\nclass PacerEventClient extends EventClient<PacerEventMap> {\n constructor(props?: { debug: boolean }) {\n super({\n pluginId: 'pacer',\n debug: props?.debug,\n /**\n * TanStack Devtools starts `ClientEventBus` inside an async dynamic import\n * after the first commit. Pacer utils often emit during the first render\n * (constructor → store sync). `EventClient` only retries `tanstack-connect`\n * `#maxRetries` times at `reconnectEveryMs` before it gives up and drops\n * all queued events permanently. Default 300ms × 5 ≈ 1.5s is often too\n * short; Form uses 1000ms here for the same reason.\n */\n reconnectEveryMs: 1000,\n })\n }\n}\n\nexport const emitChange = <TEvent extends keyof PacerEventMap>(\n event: TEvent,\n instance: PacerUtilEmitSource,\n) => {\n const key = instance.key\n if (!key) {\n return\n }\n registerPacerDevtoolsInstance(key, instance)\n pacerEventClient.emit(event, toPacerDevtoolsWirePayload({ ...instance, key }))\n}\n\nexport const pacerEventClient = new PacerEventClient()\n"],"mappings":";;;AAeA,MAAM,8CAA8B,IAAI,IAAqB;AAE7D,SAAgB,8BACd,KACA,UACM;CACN,4BAA4B,IAAI,KAAK,QAAQ;AAC/C;AAEA,SAAgB,yBAAyB,KAAsB;CAC7D,OAAO,4BAA4B,IAAI,GAAG;AAC5C;AAEA,SAAS,cAAc,OAAyB;CAC9C,IAAI,UAAU,QACZ;CAEF,IAAI;EACF,OAAO,KAAK,MAAM,KAAK,UAAU,KAAK,CAAC;CACzC,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAS,kBAAkB,OAGf;CACV,IAAI,OAAO,MAAM,QAAQ,YACvB,OAAO,MAAM,IAAI;CAEnB,OAAO,MAAM;AACf;AAEA,SAAS,2BAA2B,UAIP;CAC3B,OAAO;EACL,KAAK,SAAS;EACd,OAAO,EAAE,OAAO,cAAc,kBAAkB,SAAS,KAAK,CAAC,EAAE;EACjE,SAAS,cAAc,SAAS,OAAO;CACzC;AACF;AAuCA,IAAM,mBAAN,cAA+B,YAA2B;CACxD,YAAY,OAA4B;EACtC,MAAM;GACJ,UAAU;GACV,OAAO,OAAO;;;;;;;;;GASd,kBAAkB;EACpB,CAAC;CACH;AACF;AAEA,MAAa,cACX,OACA,aACG;CACH,MAAM,MAAM,SAAS;CACrB,IAAI,CAAC,KACH;CAEF,8BAA8B,KAAK,QAAQ;CAC3C,iBAAiB,KAAK,OAAO,2BAA2B;EAAE,GAAG;EAAU;CAAI,CAAC,CAAC;AAC/E;AAEA,MAAa,mBAAmB,IAAI,iBAAiB"}
|
package/dist/index.cjs
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_utils = require('./utils.cjs');
|
|
3
|
-
const require_async_retryer = require('./async-retryer.cjs');
|
|
4
|
-
const require_event_client = require('./event-client.cjs');
|
|
5
|
-
const require_async_batcher = require('./async-batcher.cjs');
|
|
6
|
-
const require_async_debouncer = require('./async-debouncer.cjs');
|
|
7
|
-
const require_async_queuer = require('./async-queuer.cjs');
|
|
8
|
-
const require_async_rate_limiter = require('./async-rate-limiter.cjs');
|
|
9
|
-
const require_async_throttler = require('./async-throttler.cjs');
|
|
10
|
-
const require_batcher = require('./batcher.cjs');
|
|
11
|
-
const require_debouncer = require('./debouncer.cjs');
|
|
12
|
-
const require_queuer = require('./queuer.cjs');
|
|
13
|
-
const require_rate_limiter = require('./rate-limiter.cjs');
|
|
14
|
-
const require_throttler = require('./throttler.cjs');
|
|
15
|
-
|
|
16
|
-
exports.AsyncBatcher = require_async_batcher.AsyncBatcher;
|
|
17
|
-
exports.AsyncDebouncer = require_async_debouncer.AsyncDebouncer;
|
|
18
|
-
exports.AsyncQueuer = require_async_queuer.AsyncQueuer;
|
|
19
|
-
exports.AsyncRateLimiter = require_async_rate_limiter.AsyncRateLimiter;
|
|
20
|
-
exports.AsyncRetryer = require_async_retryer.AsyncRetryer;
|
|
21
|
-
exports.AsyncThrottler = require_async_throttler.AsyncThrottler;
|
|
22
|
-
exports.Batcher = require_batcher.Batcher;
|
|
23
|
-
exports.Debouncer = require_debouncer.Debouncer;
|
|
24
|
-
exports.Queuer = require_queuer.Queuer;
|
|
25
|
-
exports.RateLimiter = require_rate_limiter.RateLimiter;
|
|
26
|
-
exports.Throttler = require_throttler.Throttler;
|
|
27
|
-
exports.asyncBatch = require_async_batcher.asyncBatch;
|
|
28
|
-
exports.asyncBatcherOptions = require_async_batcher.asyncBatcherOptions;
|
|
29
|
-
exports.asyncDebounce = require_async_debouncer.asyncDebounce;
|
|
30
|
-
exports.asyncDebouncerOptions = require_async_debouncer.asyncDebouncerOptions;
|
|
31
|
-
exports.asyncQueue = require_async_queuer.asyncQueue;
|
|
32
|
-
exports.asyncQueuerOptions = require_async_queuer.asyncQueuerOptions;
|
|
33
|
-
exports.asyncRateLimit = require_async_rate_limiter.asyncRateLimit;
|
|
34
|
-
exports.asyncRateLimiterOptions = require_async_rate_limiter.asyncRateLimiterOptions;
|
|
35
|
-
exports.asyncRetry = require_async_retryer.asyncRetry;
|
|
36
|
-
exports.asyncRetryerOptions = require_async_retryer.asyncRetryerOptions;
|
|
37
|
-
exports.asyncThrottle = require_async_throttler.asyncThrottle;
|
|
38
|
-
exports.asyncThrottlerOptions = require_async_throttler.asyncThrottlerOptions;
|
|
39
|
-
exports.batch = require_batcher.batch;
|
|
40
|
-
exports.debounce = require_debouncer.debounce;
|
|
41
|
-
exports.debouncerOptions = require_debouncer.debouncerOptions;
|
|
42
|
-
exports.emitChange = require_event_client.emitChange;
|
|
43
|
-
exports.getPacerDevtoolsInstance = require_event_client.getPacerDevtoolsInstance;
|
|
44
|
-
exports.isFunction = require_utils.isFunction;
|
|
45
|
-
exports.pacerEventClient = require_event_client.pacerEventClient;
|
|
46
|
-
exports.parseFunctionOrValue = require_utils.parseFunctionOrValue;
|
|
47
|
-
exports.queue = require_queuer.queue;
|
|
48
|
-
exports.queuerOptions = require_queuer.queuerOptions;
|
|
49
|
-
exports.rateLimit = require_rate_limiter.rateLimit;
|
|
50
|
-
exports.rateLimiterOptions = require_rate_limiter.rateLimiterOptions;
|
|
51
|
-
exports.throttle = require_throttler.throttle;
|
|
52
|
-
exports.throttlerOptions = require_throttler.throttlerOptions;
|
package/dist/index.d.cts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { AnyAsyncFunction, AnyFunction, OptionalKeys } from "./types.cjs";
|
|
2
|
-
import { AsyncRetryer, AsyncRetryerOptions, AsyncRetryerState, asyncRetry, asyncRetryerOptions } from "./async-retryer.cjs";
|
|
3
|
-
import { AsyncBatcher, AsyncBatcherOptions, AsyncBatcherState, asyncBatch, asyncBatcherOptions } from "./async-batcher.cjs";
|
|
4
|
-
import { AsyncDebouncer, AsyncDebouncerOptions, AsyncDebouncerState, asyncDebounce, asyncDebouncerOptions } from "./async-debouncer.cjs";
|
|
5
|
-
import { QueuePosition, Queuer, QueuerOptions, QueuerState, queue, queuerOptions } from "./queuer.cjs";
|
|
6
|
-
import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState, asyncQueue, asyncQueuerOptions } from "./async-queuer.cjs";
|
|
7
|
-
import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState, asyncRateLimit, asyncRateLimiterOptions } from "./async-rate-limiter.cjs";
|
|
8
|
-
import { AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState, asyncThrottle, asyncThrottlerOptions } from "./async-throttler.cjs";
|
|
9
|
-
import { Batcher, BatcherOptions, BatcherState, batch } from "./batcher.cjs";
|
|
10
|
-
import { Debouncer, DebouncerOptions, DebouncerState, debounce, debouncerOptions } from "./debouncer.cjs";
|
|
11
|
-
import { PacerDevtoolsWirePayload, PacerEventMap, PacerEventName, emitChange, getPacerDevtoolsInstance, pacerEventClient } from "./event-client.cjs";
|
|
12
|
-
import { RateLimiter, RateLimiterOptions, RateLimiterState, rateLimit, rateLimiterOptions } from "./rate-limiter.cjs";
|
|
13
|
-
import { Throttler, ThrottlerOptions, ThrottlerState, throttle, throttlerOptions } from "./throttler.cjs";
|
|
14
|
-
import { isFunction, parseFunctionOrValue } from "./utils.cjs";
|
|
15
|
-
export { AnyAsyncFunction, AnyFunction, AsyncBatcher, AsyncBatcherOptions, AsyncBatcherState, AsyncDebouncer, AsyncDebouncerOptions, AsyncDebouncerState, AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState, AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState, AsyncRetryer, AsyncRetryerOptions, AsyncRetryerState, AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState, Batcher, BatcherOptions, BatcherState, Debouncer, DebouncerOptions, DebouncerState, OptionalKeys, type PacerDevtoolsWirePayload, type PacerEventMap, type PacerEventName, QueuePosition, Queuer, QueuerOptions, QueuerState, RateLimiter, RateLimiterOptions, RateLimiterState, Throttler, ThrottlerOptions, ThrottlerState, asyncBatch, asyncBatcherOptions, asyncDebounce, asyncDebouncerOptions, asyncQueue, asyncQueuerOptions, asyncRateLimit, asyncRateLimiterOptions, asyncRetry, asyncRetryerOptions, asyncThrottle, asyncThrottlerOptions, batch, debounce, debouncerOptions, emitChange, getPacerDevtoolsInstance, isFunction, pacerEventClient, parseFunctionOrValue, queue, queuerOptions, rateLimit, rateLimiterOptions, throttle, throttlerOptions };
|
package/dist/queuer.cjs
DELETED
|
@@ -1,406 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_utils = require('./utils.cjs');
|
|
3
|
-
const require_event_client = require('./event-client.cjs');
|
|
4
|
-
let _tanstack_store = require("@tanstack/store");
|
|
5
|
-
|
|
6
|
-
//#region src/queuer.ts
|
|
7
|
-
function getDefaultQueuerState() {
|
|
8
|
-
return {
|
|
9
|
-
executionCount: 0,
|
|
10
|
-
expirationCount: 0,
|
|
11
|
-
isEmpty: true,
|
|
12
|
-
isFull: false,
|
|
13
|
-
isIdle: true,
|
|
14
|
-
isRunning: true,
|
|
15
|
-
itemTimestamps: [],
|
|
16
|
-
items: [],
|
|
17
|
-
pendingTick: false,
|
|
18
|
-
rejectionCount: 0,
|
|
19
|
-
size: 0,
|
|
20
|
-
status: "idle",
|
|
21
|
-
addItemCount: 0
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Utility function for sharing common `QueuerOptions` options between different `Queuer` instances.
|
|
26
|
-
*/
|
|
27
|
-
function queuerOptions(options) {
|
|
28
|
-
return options;
|
|
29
|
-
}
|
|
30
|
-
const defaultOptions = {
|
|
31
|
-
addItemsTo: "back",
|
|
32
|
-
getItemsFrom: "front",
|
|
33
|
-
getPriority: (item) => item?.priority ?? 0,
|
|
34
|
-
getIsExpired: () => false,
|
|
35
|
-
expirationDuration: Infinity,
|
|
36
|
-
initialItems: [],
|
|
37
|
-
maxSize: Infinity,
|
|
38
|
-
started: true,
|
|
39
|
-
wait: 0
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* A flexible queue that processes items with configurable wait times, expiration, and priority.
|
|
43
|
-
*
|
|
44
|
-
* This synchronous version is lighter weight and often all you need - upgrade to AsyncQueuer when you need promises, retry support, abort capabilities, concurrent execution, or advanced error handling.
|
|
45
|
-
*
|
|
46
|
-
* Features:
|
|
47
|
-
* - Automatic or manual processing of items
|
|
48
|
-
* - FIFO (First In First Out), LIFO (Last In First Out), or double-ended queue behavior
|
|
49
|
-
* - Priority-based ordering when getPriority is provided
|
|
50
|
-
* - Item expiration and removal of stale items
|
|
51
|
-
* - Callbacks for queue state changes, execution, rejection, and expiration
|
|
52
|
-
*
|
|
53
|
-
* Running behavior:
|
|
54
|
-
* - `start()`: Begins automatically processing items in the queue (defaults to isRunning)
|
|
55
|
-
* - `stop()`: Pauses processing but maintains queue state
|
|
56
|
-
* - `wait`: Configurable delay between processing items
|
|
57
|
-
* - `onItemsChange`/`onExecute`: Callbacks for monitoring queue state
|
|
58
|
-
*
|
|
59
|
-
* Manual processing is also supported when automatic processing is disabled:
|
|
60
|
-
* - `execute()`: Processes the next item using the provided function
|
|
61
|
-
* - `getNextItem()`: Removes and returns the next item without processing
|
|
62
|
-
*
|
|
63
|
-
* Queue behavior defaults to FIFO:
|
|
64
|
-
* - `addItem(item)`: Adds to the back of the queue
|
|
65
|
-
* - Items processed from the front of the queue
|
|
66
|
-
*
|
|
67
|
-
* Priority queue:
|
|
68
|
-
* - Provide a `getPriority` function; higher values are processed first
|
|
69
|
-
*
|
|
70
|
-
* Stack (LIFO):
|
|
71
|
-
* - `addItem(item, 'back')`: Adds to the back
|
|
72
|
-
* - `getNextItem('back')`: Removes from the back
|
|
73
|
-
*
|
|
74
|
-
* Double-ended queue:
|
|
75
|
-
* - `addItem(item, position)`: Adds to specified position ('front'/'back')
|
|
76
|
-
* - `getNextItem(position)`: Removes from specified position
|
|
77
|
-
*
|
|
78
|
-
* Item expiration:
|
|
79
|
-
* - `expirationDuration`: Maximum time items can stay in the queue
|
|
80
|
-
* - `getIsExpired`: Function to override default expiration
|
|
81
|
-
* - `onExpire`: Callback for expired items
|
|
82
|
-
*
|
|
83
|
-
* State Management:
|
|
84
|
-
* - Uses TanStack Store for reactive state management
|
|
85
|
-
* - Use `initialState` to provide initial state values when creating the queuer
|
|
86
|
-
* - Use `onExecute` callback to react to item execution and implement custom logic
|
|
87
|
-
* - Use `onItemsChange` callback to react to items being added or removed from the queue
|
|
88
|
-
* - Use `onExpire` callback to react to items expiring and implement custom logic
|
|
89
|
-
* - Use `onReject` callback to react to items being rejected when the queue is full
|
|
90
|
-
* - The state includes execution count, expiration count, rejection count, and isRunning status
|
|
91
|
-
* - State can be accessed via `queuer.store.state` when using the class directly
|
|
92
|
-
* - When using framework adapters (React/Solid), state is accessed from `queuer.state`
|
|
93
|
-
*
|
|
94
|
-
* Example usage:
|
|
95
|
-
* ```ts
|
|
96
|
-
* // Auto-processing queue with wait time
|
|
97
|
-
* const autoQueue = new Queuer<number>((n) => console.log(n), {
|
|
98
|
-
* started: true, // Begin processing immediately
|
|
99
|
-
* wait: 1000, // Wait 1s between items
|
|
100
|
-
* onExecute: (item, queuer) => console.log(`Processed ${item}`)
|
|
101
|
-
* });
|
|
102
|
-
* autoQueue.addItem(1); // Will process after 1s
|
|
103
|
-
* autoQueue.addItem(2); // Will process 1s after first item
|
|
104
|
-
*
|
|
105
|
-
* // Manual processing queue
|
|
106
|
-
* const manualQueue = new Queuer<number>((n) => console.log(n), {
|
|
107
|
-
* started: false
|
|
108
|
-
* });
|
|
109
|
-
* manualQueue.addItem(1); // [1]
|
|
110
|
-
* manualQueue.addItem(2); // [1, 2]
|
|
111
|
-
* manualQueue.execute(); // logs 1, queue is [2]
|
|
112
|
-
* manualQueue.getNextItem(); // returns 2, queue is empty
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
var Queuer = class {
|
|
116
|
-
#timeoutId;
|
|
117
|
-
constructor(fn, initialOptions = {}) {
|
|
118
|
-
this.fn = fn;
|
|
119
|
-
this.store = new _tanstack_store.Store(getDefaultQueuerState());
|
|
120
|
-
this.#timeoutId = null;
|
|
121
|
-
this.setOptions = (newOptions) => {
|
|
122
|
-
this.options = {
|
|
123
|
-
...this.options,
|
|
124
|
-
...newOptions
|
|
125
|
-
};
|
|
126
|
-
};
|
|
127
|
-
this.#setState = (newState) => {
|
|
128
|
-
this.store.setState((state) => {
|
|
129
|
-
const combinedState = {
|
|
130
|
-
...state,
|
|
131
|
-
...newState
|
|
132
|
-
};
|
|
133
|
-
const { items, isRunning } = combinedState;
|
|
134
|
-
const size = items.length;
|
|
135
|
-
const isFull = size >= (this.options.maxSize ?? Infinity);
|
|
136
|
-
const isEmpty = size === 0;
|
|
137
|
-
const isIdle = isRunning && isEmpty;
|
|
138
|
-
const status = isIdle ? "idle" : isRunning ? "running" : "stopped";
|
|
139
|
-
return {
|
|
140
|
-
...combinedState,
|
|
141
|
-
isEmpty,
|
|
142
|
-
isFull,
|
|
143
|
-
isIdle,
|
|
144
|
-
size,
|
|
145
|
-
status
|
|
146
|
-
};
|
|
147
|
-
});
|
|
148
|
-
require_event_client.emitChange("Queuer", this);
|
|
149
|
-
};
|
|
150
|
-
this.#getWait = () => {
|
|
151
|
-
return require_utils.parseFunctionOrValue(this.options.wait ?? 0, this);
|
|
152
|
-
};
|
|
153
|
-
this.#tick = () => {
|
|
154
|
-
if (!this.store.state.isRunning) {
|
|
155
|
-
this.#setState({ pendingTick: false });
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
this.#setState({ pendingTick: true });
|
|
159
|
-
this.#checkExpiredItems();
|
|
160
|
-
while (this.store.state.items.length > 0) {
|
|
161
|
-
if (this.execute(this.options.getItemsFrom ?? "front") === void 0) break;
|
|
162
|
-
const wait = this.#getWait();
|
|
163
|
-
if (wait > 0) {
|
|
164
|
-
this.#timeoutId = setTimeout(() => this.#tick(), wait);
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
this.#tick();
|
|
168
|
-
}
|
|
169
|
-
this.#setState({ pendingTick: false });
|
|
170
|
-
};
|
|
171
|
-
this.addItem = (item, position = this.options.addItemsTo ?? "back", runOnItemsChange = true) => {
|
|
172
|
-
this.#setState({ addItemCount: this.store.state.addItemCount + 1 });
|
|
173
|
-
if (item === void 0) {
|
|
174
|
-
this.#setState({ rejectionCount: this.store.state.rejectionCount + 1 });
|
|
175
|
-
this.options.onReject?.(item, this);
|
|
176
|
-
return false;
|
|
177
|
-
}
|
|
178
|
-
if (this.store.state.items.length >= (this.options.maxSize ?? Infinity)) {
|
|
179
|
-
this.#setState({ rejectionCount: this.store.state.rejectionCount + 1 });
|
|
180
|
-
this.options.onReject?.(item, this);
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
const priority = this.options.getPriority !== defaultOptions.getPriority ? this.options.getPriority(item) : item?.priority;
|
|
184
|
-
const items = this.store.state.items;
|
|
185
|
-
const itemTimestamps = this.store.state.itemTimestamps;
|
|
186
|
-
if (priority !== void 0) {
|
|
187
|
-
const insertIndex = items.findIndex((existing) => {
|
|
188
|
-
return (this.options.getPriority !== defaultOptions.getPriority ? this.options.getPriority(existing) : existing?.priority) < priority;
|
|
189
|
-
});
|
|
190
|
-
if (insertIndex === -1) {
|
|
191
|
-
items.push(item);
|
|
192
|
-
itemTimestamps.push(Date.now());
|
|
193
|
-
} else {
|
|
194
|
-
items.splice(insertIndex, 0, item);
|
|
195
|
-
itemTimestamps.splice(insertIndex, 0, Date.now());
|
|
196
|
-
}
|
|
197
|
-
} else if (position === "front") {
|
|
198
|
-
items.unshift(item);
|
|
199
|
-
itemTimestamps.unshift(Date.now());
|
|
200
|
-
} else {
|
|
201
|
-
items.push(item);
|
|
202
|
-
itemTimestamps.push(Date.now());
|
|
203
|
-
}
|
|
204
|
-
this.#setState({
|
|
205
|
-
items,
|
|
206
|
-
itemTimestamps
|
|
207
|
-
});
|
|
208
|
-
if (runOnItemsChange) this.options.onItemsChange?.(this);
|
|
209
|
-
if (this.store.state.isRunning && !this.store.state.pendingTick) {
|
|
210
|
-
this.#setState({ pendingTick: true });
|
|
211
|
-
this.#tick();
|
|
212
|
-
}
|
|
213
|
-
return true;
|
|
214
|
-
};
|
|
215
|
-
this.getNextItem = (position = this.options.getItemsFrom ?? "front") => {
|
|
216
|
-
const { items, itemTimestamps } = this.store.state;
|
|
217
|
-
let item;
|
|
218
|
-
if (this.options.getPriority !== defaultOptions.getPriority || position === "front") {
|
|
219
|
-
item = items[0];
|
|
220
|
-
if (item !== void 0) this.#setState({
|
|
221
|
-
items: items.slice(1),
|
|
222
|
-
itemTimestamps: itemTimestamps.slice(1)
|
|
223
|
-
});
|
|
224
|
-
} else {
|
|
225
|
-
item = items[items.length - 1];
|
|
226
|
-
if (item !== void 0) this.#setState({
|
|
227
|
-
items: items.slice(0, -1),
|
|
228
|
-
itemTimestamps: itemTimestamps.slice(0, -1)
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
if (item !== void 0) this.options.onItemsChange?.(this);
|
|
232
|
-
return item;
|
|
233
|
-
};
|
|
234
|
-
this.#getAllItems = () => {
|
|
235
|
-
const items = this.peekAllItems();
|
|
236
|
-
this.clear();
|
|
237
|
-
return items;
|
|
238
|
-
};
|
|
239
|
-
this.execute = (position) => {
|
|
240
|
-
const item = this.getNextItem(position);
|
|
241
|
-
if (item !== void 0) {
|
|
242
|
-
this.fn(item);
|
|
243
|
-
this.#setState({ executionCount: this.store.state.executionCount + 1 });
|
|
244
|
-
this.options.onExecute?.(item, this);
|
|
245
|
-
}
|
|
246
|
-
return item;
|
|
247
|
-
};
|
|
248
|
-
this.flush = (numberOfItems = this.store.state.items.length, position) => {
|
|
249
|
-
this.#clearTimeout();
|
|
250
|
-
for (let i = 0; i < numberOfItems; i++) this.execute(position);
|
|
251
|
-
this.#tick();
|
|
252
|
-
};
|
|
253
|
-
this.flushAsBatch = (batchFunction) => {
|
|
254
|
-
const items = this.#getAllItems();
|
|
255
|
-
this.clear();
|
|
256
|
-
batchFunction(items);
|
|
257
|
-
};
|
|
258
|
-
this.#checkExpiredItems = () => {
|
|
259
|
-
if ((this.options.expirationDuration ?? Infinity) === Infinity && this.options.getIsExpired === defaultOptions.getIsExpired) return;
|
|
260
|
-
const now = Date.now();
|
|
261
|
-
const expiredIndices = [];
|
|
262
|
-
for (let i = 0; i < this.store.state.items.length; i++) {
|
|
263
|
-
const timestamp = this.store.state.itemTimestamps[i];
|
|
264
|
-
if (timestamp === void 0) continue;
|
|
265
|
-
const item = this.store.state.items[i];
|
|
266
|
-
if (item === void 0) continue;
|
|
267
|
-
if (this.options.getIsExpired !== defaultOptions.getIsExpired ? this.options.getIsExpired(item, timestamp) : now - timestamp > (this.options.expirationDuration ?? Infinity)) expiredIndices.push(i);
|
|
268
|
-
}
|
|
269
|
-
for (let i = expiredIndices.length - 1; i >= 0; i--) {
|
|
270
|
-
const index = expiredIndices[i];
|
|
271
|
-
if (index === void 0) continue;
|
|
272
|
-
const expiredItem = this.store.state.items[index];
|
|
273
|
-
if (expiredItem === void 0) continue;
|
|
274
|
-
const newItems = [...this.store.state.items];
|
|
275
|
-
const newTimestamps = [...this.store.state.itemTimestamps];
|
|
276
|
-
newItems.splice(index, 1);
|
|
277
|
-
newTimestamps.splice(index, 1);
|
|
278
|
-
this.#setState({
|
|
279
|
-
items: newItems,
|
|
280
|
-
itemTimestamps: newTimestamps,
|
|
281
|
-
expirationCount: this.store.state.expirationCount + 1
|
|
282
|
-
});
|
|
283
|
-
this.options.onExpire?.(expiredItem, this);
|
|
284
|
-
}
|
|
285
|
-
if (expiredIndices.length > 0) this.options.onItemsChange?.(this);
|
|
286
|
-
};
|
|
287
|
-
this.peekNextItem = (position = "front") => {
|
|
288
|
-
if (position === "front") return this.store.state.items[0];
|
|
289
|
-
return this.store.state.items[this.store.state.items.length - 1];
|
|
290
|
-
};
|
|
291
|
-
this.peekAllItems = () => {
|
|
292
|
-
return [...this.store.state.items];
|
|
293
|
-
};
|
|
294
|
-
this.start = () => {
|
|
295
|
-
this.#setState({ isRunning: true });
|
|
296
|
-
if (!this.store.state.pendingTick && this.store.state.items.length > 0) this.#tick();
|
|
297
|
-
};
|
|
298
|
-
this.stop = () => {
|
|
299
|
-
this.#clearTimeout();
|
|
300
|
-
this.#setState({
|
|
301
|
-
isRunning: false,
|
|
302
|
-
pendingTick: false
|
|
303
|
-
});
|
|
304
|
-
};
|
|
305
|
-
this.#clearTimeout = () => {
|
|
306
|
-
if (this.#timeoutId) {
|
|
307
|
-
clearTimeout(this.#timeoutId);
|
|
308
|
-
this.#timeoutId = null;
|
|
309
|
-
}
|
|
310
|
-
};
|
|
311
|
-
this.clear = () => {
|
|
312
|
-
this.#setState({
|
|
313
|
-
items: [],
|
|
314
|
-
itemTimestamps: []
|
|
315
|
-
});
|
|
316
|
-
this.options.onItemsChange?.(this);
|
|
317
|
-
};
|
|
318
|
-
this.reset = () => {
|
|
319
|
-
this.#setState(getDefaultQueuerState());
|
|
320
|
-
this.options.onItemsChange?.(this);
|
|
321
|
-
};
|
|
322
|
-
this.key = initialOptions.key;
|
|
323
|
-
this.options = {
|
|
324
|
-
...defaultOptions,
|
|
325
|
-
...initialOptions
|
|
326
|
-
};
|
|
327
|
-
const isInitiallyRunning = this.options.initialState?.isRunning ?? this.options.started ?? true;
|
|
328
|
-
this.#setState({
|
|
329
|
-
...this.options.initialState,
|
|
330
|
-
isRunning: isInitiallyRunning
|
|
331
|
-
});
|
|
332
|
-
if (this.options.initialState?.items) {
|
|
333
|
-
if (this.store.state.isRunning) this.#tick();
|
|
334
|
-
} else for (let i = 0; i < (this.options.initialItems?.length ?? 0); i++) {
|
|
335
|
-
const item = this.options.initialItems[i];
|
|
336
|
-
const isLast = i === (this.options.initialItems?.length ?? 0) - 1;
|
|
337
|
-
this.addItem(item, this.options.addItemsTo ?? "back", isLast);
|
|
338
|
-
}
|
|
339
|
-
if (this.key) require_event_client.pacerEventClient.on("d-Queuer", (event) => {
|
|
340
|
-
if (event.payload.key !== this.key) return;
|
|
341
|
-
this.#setState(event.payload.store.state);
|
|
342
|
-
this.setOptions(event.payload.options);
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
#setState;
|
|
346
|
-
/**
|
|
347
|
-
* Returns the current wait time (in milliseconds) between processing items.
|
|
348
|
-
* If a function is provided, it is called with the queuer instance.
|
|
349
|
-
*/
|
|
350
|
-
#getWait;
|
|
351
|
-
/**
|
|
352
|
-
* Processes items in the queue up to the wait interval. Internal use only.
|
|
353
|
-
*/
|
|
354
|
-
#tick;
|
|
355
|
-
#getAllItems;
|
|
356
|
-
/**
|
|
357
|
-
* Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
|
|
358
|
-
* Internal use only.
|
|
359
|
-
*/
|
|
360
|
-
#checkExpiredItems;
|
|
361
|
-
#clearTimeout;
|
|
362
|
-
};
|
|
363
|
-
/**
|
|
364
|
-
* Creates a queue that processes items immediately upon addition.
|
|
365
|
-
* Items are processed sequentially in FIFO order by default.
|
|
366
|
-
*
|
|
367
|
-
* This synchronous version is lighter weight and often all you need - upgrade to asyncQueue when you need promises, retry support, abort capabilities, concurrent execution, or advanced error handling.
|
|
368
|
-
*
|
|
369
|
-
* State Management:
|
|
370
|
-
* - Uses TanStack Store for reactive state management
|
|
371
|
-
* - Use `initialState` to provide initial state values when creating the queuer
|
|
372
|
-
* - Use `onExecute` callback to react to item execution and implement custom logic
|
|
373
|
-
* - Use `onItemsChange` callback to react to items being added or removed from the queue
|
|
374
|
-
* - Use `onExpire` callback to react to items expiring and implement custom logic
|
|
375
|
-
* - Use `onReject` callback to react to items being rejected when the queue is full
|
|
376
|
-
* - The state includes execution count, expiration count, rejection count, and isRunning status
|
|
377
|
-
* - State can be accessed via the underlying Queuer instance's `store.state` property
|
|
378
|
-
* - When using framework adapters (React/Solid), state is accessed from the hook's state property
|
|
379
|
-
*
|
|
380
|
-
* Example usage:
|
|
381
|
-
* ```ts
|
|
382
|
-
* // Basic sequential processing
|
|
383
|
-
* const processItems = queue<number>((n) => console.log(n), {
|
|
384
|
-
* wait: 1000,
|
|
385
|
-
* onItemsChange: (queuer) => console.log(queuer.peekAllItems())
|
|
386
|
-
* });
|
|
387
|
-
* processItems(1); // Logs: 1
|
|
388
|
-
* processItems(2); // Logs: 2 after 1 completes
|
|
389
|
-
*
|
|
390
|
-
* // Priority queue
|
|
391
|
-
* const processPriority = queue<number>((n) => console.log(n), {
|
|
392
|
-
* getPriority: n => n // Higher numbers processed first
|
|
393
|
-
* });
|
|
394
|
-
* processPriority(1);
|
|
395
|
-
* processPriority(3); // Processed before 1
|
|
396
|
-
* ```
|
|
397
|
-
*/
|
|
398
|
-
function queue(fn, initialOptions) {
|
|
399
|
-
return new Queuer(fn, initialOptions).addItem;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
//#endregion
|
|
403
|
-
exports.Queuer = Queuer;
|
|
404
|
-
exports.queue = queue;
|
|
405
|
-
exports.queuerOptions = queuerOptions;
|
|
406
|
-
//# sourceMappingURL=queuer.cjs.map
|