@tanstack/pacer-lite 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +16 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/{esm/index.js → index.js} +2 -13
- package/dist/lite-batcher.cjs +175 -0
- package/dist/lite-batcher.cjs.map +1 -0
- package/dist/{cjs/lite-batcher.d.cts → lite-batcher.d.cts} +89 -85
- package/dist/{esm/lite-batcher.d.ts → lite-batcher.d.ts} +89 -85
- package/dist/lite-batcher.js +173 -0
- package/dist/lite-batcher.js.map +1 -0
- package/dist/lite-debouncer.cjs +125 -0
- package/dist/lite-debouncer.cjs.map +1 -0
- package/dist/{cjs/lite-debouncer.d.cts → lite-debouncer.d.cts} +53 -47
- package/dist/{esm/lite-debouncer.d.ts → lite-debouncer.d.ts} +53 -47
- package/dist/lite-debouncer.js +123 -0
- package/dist/lite-debouncer.js.map +1 -0
- package/dist/lite-queuer.cjs +208 -0
- package/dist/lite-queuer.cjs.map +1 -0
- package/dist/lite-queuer.d.cts +243 -0
- package/dist/lite-queuer.d.ts +243 -0
- package/dist/lite-queuer.js +206 -0
- package/dist/lite-queuer.js.map +1 -0
- package/dist/lite-rate-limiter.cjs +149 -0
- package/dist/lite-rate-limiter.cjs.map +1 -0
- package/dist/{cjs/lite-rate-limiter.d.cts → lite-rate-limiter.d.cts} +73 -67
- package/dist/{esm/lite-rate-limiter.d.ts → lite-rate-limiter.d.ts} +73 -67
- package/dist/lite-rate-limiter.js +147 -0
- package/dist/lite-rate-limiter.js.map +1 -0
- package/dist/lite-throttler.cjs +127 -0
- package/dist/lite-throttler.cjs.map +1 -0
- package/dist/{cjs/lite-throttler.d.cts → lite-throttler.d.cts} +60 -54
- package/dist/{esm/lite-throttler.d.ts → lite-throttler.d.ts} +60 -54
- package/dist/lite-throttler.js +125 -0
- package/dist/lite-throttler.js.map +1 -0
- package/package.json +22 -59
- package/dist/cjs/index.cjs +0 -18
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -5
- package/dist/cjs/lite-batcher.cjs +0 -92
- package/dist/cjs/lite-batcher.cjs.map +0 -1
- package/dist/cjs/lite-debouncer.cjs +0 -59
- package/dist/cjs/lite-debouncer.cjs.map +0 -1
- package/dist/cjs/lite-queuer.cjs +0 -171
- package/dist/cjs/lite-queuer.cjs.map +0 -1
- package/dist/cjs/lite-queuer.d.cts +0 -239
- package/dist/cjs/lite-rate-limiter.cjs +0 -95
- package/dist/cjs/lite-rate-limiter.cjs.map +0 -1
- package/dist/cjs/lite-throttler.cjs +0 -63
- package/dist/cjs/lite-throttler.cjs.map +0 -1
- package/dist/esm/index.d.ts +0 -5
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/lite-batcher.js +0 -92
- package/dist/esm/lite-batcher.js.map +0 -1
- package/dist/esm/lite-debouncer.js +0 -59
- package/dist/esm/lite-debouncer.js.map +0 -1
- package/dist/esm/lite-queuer.d.ts +0 -239
- package/dist/esm/lite-queuer.js +0 -171
- package/dist/esm/lite-queuer.js.map +0 -1
- package/dist/esm/lite-rate-limiter.js +0 -95
- package/dist/esm/lite-rate-limiter.js.map +0 -1
- package/dist/esm/lite-throttler.js +0 -63
- package/dist/esm/lite-throttler.js.map +0 -1
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
class LiteThrottler {
|
|
4
|
-
constructor(fn, options) {
|
|
5
|
-
this.fn = fn;
|
|
6
|
-
this.options = options;
|
|
7
|
-
this.lastExecutionTime = 0;
|
|
8
|
-
this.isPending = false;
|
|
9
|
-
this.maybeExecute = (...args) => {
|
|
10
|
-
const now = Date.now();
|
|
11
|
-
const timeSinceLastExecution = now - this.lastExecutionTime;
|
|
12
|
-
if (this.options.leading && timeSinceLastExecution >= this.options.wait) {
|
|
13
|
-
this.execute(...args);
|
|
14
|
-
} else {
|
|
15
|
-
this.lastArgs = args;
|
|
16
|
-
if (!this.timeoutId && this.options.trailing) {
|
|
17
|
-
const timeoutDuration = this.options.wait - timeSinceLastExecution;
|
|
18
|
-
this.isPending = true;
|
|
19
|
-
this.timeoutId = setTimeout(() => {
|
|
20
|
-
if (this.lastArgs !== void 0) {
|
|
21
|
-
this.execute(...this.lastArgs);
|
|
22
|
-
}
|
|
23
|
-
}, timeoutDuration);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
this.execute = (...args) => {
|
|
28
|
-
this.fn(...args);
|
|
29
|
-
this.options.onExecute?.(args, this);
|
|
30
|
-
this.lastExecutionTime = Date.now();
|
|
31
|
-
this.clearTimeout();
|
|
32
|
-
this.lastArgs = void 0;
|
|
33
|
-
this.isPending = false;
|
|
34
|
-
};
|
|
35
|
-
this.flush = () => {
|
|
36
|
-
if (this.isPending && this.lastArgs) {
|
|
37
|
-
this.execute(...this.lastArgs);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
this.cancel = () => {
|
|
41
|
-
this.clearTimeout();
|
|
42
|
-
this.lastArgs = void 0;
|
|
43
|
-
this.isPending = false;
|
|
44
|
-
};
|
|
45
|
-
this.clearTimeout = () => {
|
|
46
|
-
if (this.timeoutId) {
|
|
47
|
-
clearTimeout(this.timeoutId);
|
|
48
|
-
this.timeoutId = void 0;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
if (this.options.leading === void 0 && this.options.trailing === void 0) {
|
|
52
|
-
this.options.leading = true;
|
|
53
|
-
this.options.trailing = true;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
function liteThrottle(fn, options) {
|
|
58
|
-
const throttler = new LiteThrottler(fn, options);
|
|
59
|
-
return throttler.maybeExecute;
|
|
60
|
-
}
|
|
61
|
-
exports.LiteThrottler = LiteThrottler;
|
|
62
|
-
exports.liteThrottle = liteThrottle;
|
|
63
|
-
//# sourceMappingURL=lite-throttler.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lite-throttler.cjs","sources":["../../src/lite-throttler.ts"],"sourcesContent":["import type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * Options for configuring a lite throttled function\n */\nexport interface LiteThrottlerOptions<TFn extends AnyFunction = AnyFunction> {\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to true.\n */\n leading?: boolean\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (args: Parameters<TFn>, throttler: LiteThrottler<TFn>) => void\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Time window in milliseconds during which the function can only be executed once.\n */\n wait: number\n}\n\n/**\n * A lightweight class that creates a throttled function.\n *\n * This is an alternative to the Throttler in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core Throttler,\n * this version does not use TanStack Store for state management, has no devtools integration,\n * and provides only essential throttling functionality.\n *\n * Throttling ensures a function is called at most once within a specified time window.\n * Unlike debouncing which waits for a pause in calls, throttling guarantees consistent\n * execution timing regardless of call frequency.\n *\n * Supports both leading and trailing edge execution:\n * - Leading: Execute immediately on first call (default: true)\n * - Trailing: Execute after wait period if called during throttle (default: true)\n *\n * Features:\n * - Zero dependencies - no external libraries required\n * - Minimal API surface - only essential methods (maybeExecute, flush, cancel)\n * - Simple state management - uses basic private properties instead of reactive stores\n * - Callback support for monitoring execution events\n * - Lightweight - designed for use in npm packages where bundle size matters\n *\n * @example\n * ```ts\n * const throttler = new LiteThrottler((scrollY: number) => {\n * updateScrollPosition(scrollY);\n * }, {\n * wait: 100,\n * onExecute: (args, throttler) => {\n * console.log('Updated scroll position:', args[0]);\n * }\n * });\n *\n * // Will execute at most once per 100ms\n * window.addEventListener('scroll', () => {\n * throttler.maybeExecute(window.scrollY);\n * });\n * ```\n */\nexport class LiteThrottler<TFn extends AnyFunction> {\n private timeoutId: NodeJS.Timeout | undefined\n private lastArgs: Parameters<TFn> | undefined\n private lastExecutionTime = 0\n private isPending = false\n\n constructor(\n public fn: TFn,\n public options: LiteThrottlerOptions<TFn>,\n ) {\n // Default both leading and trailing to true if neither is specified\n if (\n this.options.leading === undefined &&\n this.options.trailing === undefined\n ) {\n this.options.leading = true\n this.options.trailing = true\n }\n }\n\n /**\n * Attempts to execute the throttled function. The execution behavior depends on the throttler options:\n *\n * - If enough time has passed since the last execution (>= wait period):\n * - With leading=true: Executes immediately\n * - With leading=false: Waits for the next trailing execution\n *\n * - If within the wait period:\n * - With trailing=true: Schedules execution for end of wait period\n * - With trailing=false: Drops the execution\n */\n maybeExecute = (...args: Parameters<TFn>): void => {\n const now = Date.now()\n const timeSinceLastExecution = now - this.lastExecutionTime\n\n // Handle leading execution\n if (this.options.leading && timeSinceLastExecution >= this.options.wait) {\n this.execute(...args)\n } else {\n // Store the most recent arguments for potential trailing execution\n this.lastArgs = args\n\n // Set up trailing execution if not already scheduled\n if (!this.timeoutId && this.options.trailing) {\n const timeoutDuration = this.options.wait - timeSinceLastExecution\n this.isPending = true\n this.timeoutId = setTimeout(() => {\n if (this.lastArgs !== undefined) {\n this.execute(...this.lastArgs)\n }\n }, timeoutDuration)\n }\n }\n }\n\n private execute = (...args: Parameters<TFn>): void => {\n this.fn(...args)\n this.options.onExecute?.(args, this)\n this.lastExecutionTime = Date.now()\n this.clearTimeout()\n this.lastArgs = undefined\n this.isPending = false\n }\n\n /**\n * Processes the current pending execution immediately.\n * If there's a pending execution, it will be executed right away\n * and the timeout will be cleared.\n */\n flush = (): void => {\n if (this.isPending && this.lastArgs) {\n this.execute(...this.lastArgs)\n }\n }\n\n /**\n * Cancels any pending trailing execution and clears internal state.\n * If a trailing execution is scheduled, this will prevent that execution from occurring.\n */\n cancel = (): void => {\n this.clearTimeout()\n this.lastArgs = undefined\n this.isPending = false\n }\n\n private clearTimeout = (): void => {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n }\n }\n}\n\n/**\n * Creates a lightweight throttled function that limits how often the provided function can execute.\n *\n * This is an alternative to the throttle function in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core version,\n * this function creates a throttler with no external dependencies, devtools integration, or reactive state.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\n *\n * @example\n * ```ts\n * const throttledScroll = liteThrottle(() => {\n * updateScrollIndicator();\n * }, { wait: 100 });\n *\n * // Will execute at most once per 100ms\n * window.addEventListener('scroll', throttledScroll);\n * ```\n *\n * @example\n * ```ts\n * // Leading edge execution - fires immediately then throttles\n * const throttledResize = liteThrottle(() => {\n * recalculateLayout();\n * }, { wait: 250, leading: true, trailing: false });\n * ```\n */\nexport function liteThrottle<TFn extends AnyFunction>(\n fn: TFn,\n options: LiteThrottlerOptions<TFn>,\n): (...args: Parameters<TFn>) => void {\n const throttler = new LiteThrottler(fn, options)\n return throttler.maybeExecute\n}\n"],"names":[],"mappings":";;AAkEO,MAAM,cAAuC;AAAA,EAMlD,YACS,IACA,SACP;AAFO,SAAA,KAAA;AACA,SAAA,UAAA;AALT,SAAQ,oBAAoB;AAC5B,SAAQ,YAAY;AA2BpB,SAAA,eAAe,IAAI,SAAgC;AACjD,YAAM,MAAM,KAAK,IAAA;AACjB,YAAM,yBAAyB,MAAM,KAAK;AAG1C,UAAI,KAAK,QAAQ,WAAW,0BAA0B,KAAK,QAAQ,MAAM;AACvE,aAAK,QAAQ,GAAG,IAAI;AAAA,MACtB,OAAO;AAEL,aAAK,WAAW;AAGhB,YAAI,CAAC,KAAK,aAAa,KAAK,QAAQ,UAAU;AAC5C,gBAAM,kBAAkB,KAAK,QAAQ,OAAO;AAC5C,eAAK,YAAY;AACjB,eAAK,YAAY,WAAW,MAAM;AAChC,gBAAI,KAAK,aAAa,QAAW;AAC/B,mBAAK,QAAQ,GAAG,KAAK,QAAQ;AAAA,YAC/B;AAAA,UACF,GAAG,eAAe;AAAA,QACpB;AAAA,MACF;AAAA,IACF;AAEA,SAAQ,UAAU,IAAI,SAAgC;AACpD,WAAK,GAAG,GAAG,IAAI;AACf,WAAK,QAAQ,YAAY,MAAM,IAAI;AACnC,WAAK,oBAAoB,KAAK,IAAA;AAC9B,WAAK,aAAA;AACL,WAAK,WAAW;AAChB,WAAK,YAAY;AAAA,IACnB;AAOA,SAAA,QAAQ,MAAY;AAClB,UAAI,KAAK,aAAa,KAAK,UAAU;AACnC,aAAK,QAAQ,GAAG,KAAK,QAAQ;AAAA,MAC/B;AAAA,IACF;AAMA,SAAA,SAAS,MAAY;AACnB,WAAK,aAAA;AACL,WAAK,WAAW;AAChB,WAAK,YAAY;AAAA,IACnB;AAEA,SAAQ,eAAe,MAAY;AACjC,UAAI,KAAK,WAAW;AAClB,qBAAa,KAAK,SAAS;AAC3B,aAAK,YAAY;AAAA,MACnB;AAAA,IACF;AA/EE,QACE,KAAK,QAAQ,YAAY,UACzB,KAAK,QAAQ,aAAa,QAC1B;AACA,WAAK,QAAQ,UAAU;AACvB,WAAK,QAAQ,WAAW;AAAA,IAC1B;AAAA,EACF;AAyEF;AA+BO,SAAS,aACd,IACA,SACoC;AACpC,QAAM,YAAY,IAAI,cAAc,IAAI,OAAO;AAC/C,SAAO,UAAU;AACnB;;;"}
|
package/dist/esm/index.d.ts
DELETED
package/dist/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
|
package/dist/esm/lite-batcher.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
class LiteBatcher {
|
|
2
|
-
constructor(fn, options = {}) {
|
|
3
|
-
this.fn = fn;
|
|
4
|
-
this.options = options;
|
|
5
|
-
this.items = [];
|
|
6
|
-
this.timeoutId = null;
|
|
7
|
-
this._isPending = false;
|
|
8
|
-
this.addItem = (item) => {
|
|
9
|
-
this.items.push(item);
|
|
10
|
-
this._isPending = this.options.wait !== Infinity;
|
|
11
|
-
this.options.onItemsChange?.(this);
|
|
12
|
-
const shouldProcess = this.items.length >= this.options.maxSize || this.options.getShouldExecute(this.items, this);
|
|
13
|
-
if (shouldProcess) {
|
|
14
|
-
this.execute();
|
|
15
|
-
} else if (this.options.wait !== Infinity) {
|
|
16
|
-
this.clearTimeout();
|
|
17
|
-
this.timeoutId = setTimeout(() => this.execute(), this.getWait());
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
this.execute = () => {
|
|
21
|
-
if (this.items.length === 0) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
const batch = this.peekAllItems();
|
|
25
|
-
this.clear();
|
|
26
|
-
this.fn(batch);
|
|
27
|
-
this.options.onExecute?.(batch, this);
|
|
28
|
-
};
|
|
29
|
-
this.flush = () => {
|
|
30
|
-
this.clearTimeout();
|
|
31
|
-
this.execute();
|
|
32
|
-
};
|
|
33
|
-
this.peekAllItems = () => {
|
|
34
|
-
return [...this.items];
|
|
35
|
-
};
|
|
36
|
-
this.clearTimeout = () => {
|
|
37
|
-
if (this.timeoutId) {
|
|
38
|
-
clearTimeout(this.timeoutId);
|
|
39
|
-
this.timeoutId = null;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
this.clear = () => {
|
|
43
|
-
const hadItems = this.items.length > 0;
|
|
44
|
-
this.items = [];
|
|
45
|
-
this._isPending = false;
|
|
46
|
-
if (hadItems) {
|
|
47
|
-
this.options.onItemsChange?.(this);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
this.cancel = () => {
|
|
51
|
-
this.clearTimeout();
|
|
52
|
-
this._isPending = false;
|
|
53
|
-
};
|
|
54
|
-
this.options.maxSize = this.options.maxSize ?? Infinity;
|
|
55
|
-
this.options.started = this.options.started ?? true;
|
|
56
|
-
this.options.wait = this.options.wait ?? Infinity;
|
|
57
|
-
this.options.getShouldExecute = this.options.getShouldExecute ?? (() => false);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Number of items currently in the batch
|
|
61
|
-
*/
|
|
62
|
-
get size() {
|
|
63
|
-
return this.items.length;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Whether the batch has no items to process (items array is empty)
|
|
67
|
-
*/
|
|
68
|
-
get isEmpty() {
|
|
69
|
-
return this.items.length === 0;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Whether the batcher is waiting for the timeout to trigger batch processing
|
|
73
|
-
*/
|
|
74
|
-
get isPending() {
|
|
75
|
-
return this._isPending;
|
|
76
|
-
}
|
|
77
|
-
getWait() {
|
|
78
|
-
if (typeof this.options.wait === "function") {
|
|
79
|
-
return this.options.wait(this);
|
|
80
|
-
}
|
|
81
|
-
return this.options.wait;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
function liteBatch(fn, options = {}) {
|
|
85
|
-
const batcher = new LiteBatcher(fn, options);
|
|
86
|
-
return batcher.addItem;
|
|
87
|
-
}
|
|
88
|
-
export {
|
|
89
|
-
LiteBatcher,
|
|
90
|
-
liteBatch
|
|
91
|
-
};
|
|
92
|
-
//# sourceMappingURL=lite-batcher.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lite-batcher.js","sources":["../../src/lite-batcher.ts"],"sourcesContent":["/**\n * Options for configuring a lite batcher instance\n */\nexport interface LiteBatcherOptions<TValue> {\n /**\n * Custom function to determine if a batch should be processed\n * Return true to process the batch immediately\n */\n getShouldExecute?: (\n items: Array<TValue>,\n batcher: LiteBatcher<TValue>,\n ) => boolean\n /**\n * Maximum number of items in a batch\n * @default Infinity\n */\n maxSize?: number\n /**\n * Callback fired after a batch is processed\n */\n onExecute?: (batch: Array<TValue>, batcher: LiteBatcher<TValue>) => void\n /**\n * Callback fired after items are added to the batcher\n */\n onItemsChange?: (batcher: LiteBatcher<TValue>) => void\n /**\n * Whether the batcher should start processing immediately\n * @default true\n */\n started?: boolean\n /**\n * Maximum time in milliseconds to wait before processing a batch.\n * If the wait duration has elapsed, the batch will be processed.\n * If not provided, the batch will not be triggered by a timeout.\n * @default Infinity\n */\n wait?: number | ((batcher: LiteBatcher<TValue>) => number)\n}\n\n/**\n * A lightweight class that collects items and processes them in batches.\n *\n * This is an alternative to the Batcher in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core Batcher,\n * this version does not use TanStack Store for state management, has no devtools integration,\n * no callbacks, and provides only essential batching functionality.\n *\n * Batching is a technique for grouping multiple operations together to be processed as a single unit.\n * This synchronous version is lighter weight and often all you need.\n *\n * The Batcher provides a flexible way to implement batching with configurable:\n * - Maximum batch size (number of items per batch)\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\n *\n * Features included:\n * - Core batching functionality (addItem, flush, clear, cancel)\n * - Size-based batching (maxSize)\n * - Time-based batching (wait timeout)\n * - Custom condition batching (getShouldExecute)\n * - Manual processing controls\n * - Public mutable options\n * - Callback support for monitoring batch execution and state changes\n *\n * Features NOT included (compared to core Batcher):\n * - No TanStack Store state management\n * - No devtools integration\n * - No complex state tracking (execution counts, etc.)\n * - No reactive state management\n *\n * @example\n * ```ts\n * // Basic batching\n * const batcher = new LiteBatcher<number>(\n * (items) => console.log('Processing batch:', items),\n * {\n * maxSize: 5,\n * wait: 2000,\n * onExecute: (batch, batcher) => {\n * console.log('Batch executed with', batch.length, 'items');\n * },\n * onItemsChange: (batcher) => {\n * console.log('Batch size changed to:', batcher.size);\n * }\n * }\n * );\n *\n * batcher.addItem(1);\n * batcher.addItem(2);\n * // After 2 seconds or when 5 items are added, whichever comes first,\n * // the batch will be processed\n * ```\n *\n * @example\n * ```ts\n * // Custom condition batching\n * const batcher = new LiteBatcher<Task>(\n * (items) => processTasks(items),\n * {\n * getShouldExecute: (items) => items.some(task => task.urgent),\n * maxSize: 10,\n * }\n * );\n *\n * batcher.addItem({ name: 'normal', urgent: false });\n * batcher.addItem({ name: 'urgent', urgent: true }); // Triggers immediate processing\n * ```\n */\nexport class LiteBatcher<TValue> {\n private items: Array<TValue> = []\n private timeoutId: NodeJS.Timeout | null = null\n private _isPending = false\n\n constructor(\n public fn: (items: Array<TValue>) => void,\n public options: LiteBatcherOptions<TValue> = {},\n ) {\n // Set defaults\n this.options.maxSize = this.options.maxSize ?? Infinity\n this.options.started = this.options.started ?? true\n this.options.wait = this.options.wait ?? Infinity\n this.options.getShouldExecute =\n this.options.getShouldExecute ?? (() => false)\n }\n\n /**\n * Number of items currently in the batch\n */\n get size(): number {\n return this.items.length\n }\n\n /**\n * Whether the batch has no items to process (items array is empty)\n */\n get isEmpty(): boolean {\n return this.items.length === 0\n }\n\n /**\n * Whether the batcher is waiting for the timeout to trigger batch processing\n */\n get isPending(): boolean {\n return this._isPending\n }\n\n private getWait(): number {\n if (typeof this.options.wait === 'function') {\n return this.options.wait(this)\n }\n return this.options.wait!\n }\n\n /**\n * Adds an item to the batcher\n * If the batch size is reached, timeout occurs, or getShouldExecute returns true, the batch will be processed\n */\n addItem = (item: TValue): void => {\n this.items.push(item)\n this._isPending = this.options.wait !== Infinity\n this.options.onItemsChange?.(this)\n\n const shouldProcess =\n this.items.length >= this.options.maxSize! ||\n this.options.getShouldExecute!(this.items, this)\n\n if (shouldProcess) {\n this.execute()\n } else if (this.options.wait !== Infinity) {\n this.clearTimeout() // clear any pending timeout to replace it with a new one\n this.timeoutId = setTimeout(() => this.execute(), this.getWait())\n }\n }\n\n /**\n * Processes the current batch of items.\n * This method will automatically be triggered if the batcher is running and any of these conditions are met:\n * - The number of items reaches maxSize\n * - The wait duration has elapsed\n * - The getShouldExecute function returns true upon adding an item\n *\n * You can also call this method manually to process the current batch at any time.\n */\n private execute = (): void => {\n if (this.items.length === 0) {\n return\n }\n\n const batch = this.peekAllItems() // copy of the items to be processed (to prevent race conditions)\n this.clear() // Clear items before processing to prevent race conditions\n\n this.fn(batch) // EXECUTE\n this.options.onExecute?.(batch, this)\n }\n\n /**\n * Processes the current batch of items immediately\n */\n flush = (): void => {\n this.clearTimeout() // clear any pending timeout\n this.execute() // execute immediately\n }\n\n /**\n * Returns a copy of all items in the batcher\n */\n peekAllItems = (): Array<TValue> => {\n return [...this.items]\n }\n\n private clearTimeout = (): void => {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = null\n }\n }\n\n /**\n * Removes all items from the batcher\n */\n clear = (): void => {\n const hadItems = this.items.length > 0\n this.items = []\n this._isPending = false\n if (hadItems) {\n this.options.onItemsChange?.(this)\n }\n }\n\n /**\n * Cancels any pending execution that was scheduled.\n * Does NOT clear out the items.\n */\n cancel = (): void => {\n this.clearTimeout()\n this._isPending = false\n }\n}\n\n/**\n * Creates a batcher that processes items in batches.\n *\n * This is an alternative to the batch function in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core version,\n * this function creates a batcher with no external dependencies, devtools integration, or reactive state.\n *\n * @example\n * ```ts\n * const batchItems = liteBatch<number>(\n * (items) => console.log('Processing:', items),\n * {\n * maxSize: 3,\n * }\n * );\n *\n * batchItems(1);\n * batchItems(2);\n * batchItems(3); // Triggers batch processing\n * ```\n */\nexport function liteBatch<TValue>(\n fn: (items: Array<TValue>) => void,\n options: LiteBatcherOptions<TValue> = {},\n): (item: TValue) => void {\n const batcher = new LiteBatcher<TValue>(fn, options)\n return batcher.addItem\n}\n"],"names":[],"mappings":"AA4GO,MAAM,YAAoB;AAAA,EAK/B,YACS,IACA,UAAsC,IAC7C;AAFO,SAAA,KAAA;AACA,SAAA,UAAA;AANT,SAAQ,QAAuB,CAAA;AAC/B,SAAQ,YAAmC;AAC3C,SAAQ,aAAa;AA8CrB,SAAA,UAAU,CAAC,SAAuB;AAChC,WAAK,MAAM,KAAK,IAAI;AACpB,WAAK,aAAa,KAAK,QAAQ,SAAS;AACxC,WAAK,QAAQ,gBAAgB,IAAI;AAEjC,YAAM,gBACJ,KAAK,MAAM,UAAU,KAAK,QAAQ,WAClC,KAAK,QAAQ,iBAAkB,KAAK,OAAO,IAAI;AAEjD,UAAI,eAAe;AACjB,aAAK,QAAA;AAAA,MACP,WAAW,KAAK,QAAQ,SAAS,UAAU;AACzC,aAAK,aAAA;AACL,aAAK,YAAY,WAAW,MAAM,KAAK,WAAW,KAAK,SAAS;AAAA,MAClE;AAAA,IACF;AAWA,SAAQ,UAAU,MAAY;AAC5B,UAAI,KAAK,MAAM,WAAW,GAAG;AAC3B;AAAA,MACF;AAEA,YAAM,QAAQ,KAAK,aAAA;AACnB,WAAK,MAAA;AAEL,WAAK,GAAG,KAAK;AACb,WAAK,QAAQ,YAAY,OAAO,IAAI;AAAA,IACtC;AAKA,SAAA,QAAQ,MAAY;AAClB,WAAK,aAAA;AACL,WAAK,QAAA;AAAA,IACP;AAKA,SAAA,eAAe,MAAqB;AAClC,aAAO,CAAC,GAAG,KAAK,KAAK;AAAA,IACvB;AAEA,SAAQ,eAAe,MAAY;AACjC,UAAI,KAAK,WAAW;AAClB,qBAAa,KAAK,SAAS;AAC3B,aAAK,YAAY;AAAA,MACnB;AAAA,IACF;AAKA,SAAA,QAAQ,MAAY;AAClB,YAAM,WAAW,KAAK,MAAM,SAAS;AACrC,WAAK,QAAQ,CAAA;AACb,WAAK,aAAa;AAClB,UAAI,UAAU;AACZ,aAAK,QAAQ,gBAAgB,IAAI;AAAA,MACnC;AAAA,IACF;AAMA,SAAA,SAAS,MAAY;AACnB,WAAK,aAAA;AACL,WAAK,aAAa;AAAA,IACpB;AAtHE,SAAK,QAAQ,UAAU,KAAK,QAAQ,WAAW;AAC/C,SAAK,QAAQ,UAAU,KAAK,QAAQ,WAAW;AAC/C,SAAK,QAAQ,OAAO,KAAK,QAAQ,QAAQ;AACzC,SAAK,QAAQ,mBACX,KAAK,QAAQ,qBAAqB,MAAM;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,OAAe;AACjB,WAAO,KAAK,MAAM;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,UAAmB;AACrB,WAAO,KAAK,MAAM,WAAW;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,YAAqB;AACvB,WAAO,KAAK;AAAA,EACd;AAAA,EAEQ,UAAkB;AACxB,QAAI,OAAO,KAAK,QAAQ,SAAS,YAAY;AAC3C,aAAO,KAAK,QAAQ,KAAK,IAAI;AAAA,IAC/B;AACA,WAAO,KAAK,QAAQ;AAAA,EACtB;AAsFF;AAuBO,SAAS,UACd,IACA,UAAsC,IACd;AACxB,QAAM,UAAU,IAAI,YAAoB,IAAI,OAAO;AACnD,SAAO,QAAQ;AACjB;"}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
class LiteDebouncer {
|
|
2
|
-
constructor(fn, options) {
|
|
3
|
-
this.fn = fn;
|
|
4
|
-
this.options = options;
|
|
5
|
-
this.canLeadingExecute = true;
|
|
6
|
-
this.maybeExecute = (...args) => {
|
|
7
|
-
let didLeadingExecute = false;
|
|
8
|
-
if (this.options.leading && this.canLeadingExecute) {
|
|
9
|
-
this.canLeadingExecute = false;
|
|
10
|
-
didLeadingExecute = true;
|
|
11
|
-
this.fn(...args);
|
|
12
|
-
this.options.onExecute?.(args, this);
|
|
13
|
-
}
|
|
14
|
-
this.lastArgs = args;
|
|
15
|
-
if (this.timeoutId) {
|
|
16
|
-
clearTimeout(this.timeoutId);
|
|
17
|
-
}
|
|
18
|
-
this.timeoutId = setTimeout(() => {
|
|
19
|
-
this.canLeadingExecute = true;
|
|
20
|
-
if (this.options.trailing && !didLeadingExecute && this.lastArgs) {
|
|
21
|
-
this.fn(...this.lastArgs);
|
|
22
|
-
this.options.onExecute?.(this.lastArgs, this);
|
|
23
|
-
}
|
|
24
|
-
this.lastArgs = void 0;
|
|
25
|
-
}, this.options.wait);
|
|
26
|
-
};
|
|
27
|
-
this.flush = () => {
|
|
28
|
-
if (this.timeoutId && this.lastArgs) {
|
|
29
|
-
clearTimeout(this.timeoutId);
|
|
30
|
-
this.timeoutId = void 0;
|
|
31
|
-
const args = this.lastArgs;
|
|
32
|
-
this.fn(...args);
|
|
33
|
-
this.options.onExecute?.(args, this);
|
|
34
|
-
this.lastArgs = void 0;
|
|
35
|
-
this.canLeadingExecute = true;
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
this.cancel = () => {
|
|
39
|
-
if (this.timeoutId) {
|
|
40
|
-
clearTimeout(this.timeoutId);
|
|
41
|
-
this.timeoutId = void 0;
|
|
42
|
-
}
|
|
43
|
-
this.lastArgs = void 0;
|
|
44
|
-
this.canLeadingExecute = true;
|
|
45
|
-
};
|
|
46
|
-
if (this.options.leading === void 0 && this.options.trailing === void 0) {
|
|
47
|
-
this.options.trailing = true;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function liteDebounce(fn, options) {
|
|
52
|
-
const debouncer = new LiteDebouncer(fn, options);
|
|
53
|
-
return debouncer.maybeExecute;
|
|
54
|
-
}
|
|
55
|
-
export {
|
|
56
|
-
LiteDebouncer,
|
|
57
|
-
liteDebounce
|
|
58
|
-
};
|
|
59
|
-
//# sourceMappingURL=lite-debouncer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lite-debouncer.js","sources":["../../src/lite-debouncer.ts"],"sourcesContent":["import type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * Options for configuring a lite debounced function\n */\nexport interface LiteDebouncerOptions<TFn extends AnyFunction = AnyFunction> {\n /**\n * Whether to execute on the leading edge of the timeout.\n * The first call will execute immediately and the rest will wait the delay.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (args: Parameters<TFn>, debouncer: LiteDebouncer<TFn>) => void\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Delay in milliseconds before executing the function.\n */\n wait: number\n}\n\n/**\n * A lightweight class that creates a debounced function.\n *\n * This is an alternative to the Debouncer in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core Debouncer,\n * this version does not use TanStack Store for state management, has no devtools integration,\n * and provides only essential debouncing functionality.\n *\n * Debouncing ensures that a function is only executed after a certain amount of time has passed\n * since its last invocation. This is useful for handling frequent events like window resizing,\n * scroll events, or input changes where you want to limit the rate of execution.\n *\n * The debounced function can be configured to execute either at the start of the delay period\n * (leading edge) or at the end (trailing edge, default). Each new call during the wait period\n * will reset the timer.\n *\n * Features:\n * - Zero dependencies - no external libraries required\n * - Minimal API surface - only essential methods (maybeExecute, flush, cancel)\n * - Simple state management - uses basic private properties instead of reactive stores\n * - Callback support for monitoring execution events\n * - Lightweight - designed for use in npm packages where bundle size matters\n *\n * @example\n * ```ts\n * const debouncer = new LiteDebouncer((value: string) => {\n * saveToDatabase(value);\n * }, {\n * wait: 500,\n * onExecute: (args, debouncer) => {\n * console.log('Saved value:', args[0]);\n * }\n * });\n *\n * // Will only save after 500ms of no new input\n * inputElement.addEventListener('input', () => {\n * debouncer.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class LiteDebouncer<TFn extends AnyFunction> {\n private timeoutId: NodeJS.Timeout | undefined\n private lastArgs: Parameters<TFn> | undefined\n private canLeadingExecute = true\n\n constructor(\n public fn: TFn,\n public options: LiteDebouncerOptions<TFn>,\n ) {\n // Default trailing to true if neither leading nor trailing is specified\n if (\n this.options.leading === undefined &&\n this.options.trailing === undefined\n ) {\n this.options.trailing = true\n }\n }\n\n /**\n * Attempts to execute the debounced function.\n * If leading is true and this is the first call, executes immediately.\n * Otherwise, queues the execution for after the wait time.\n * Each new call resets the timer.\n */\n maybeExecute = (...args: Parameters<TFn>): void => {\n let didLeadingExecute = false\n\n if (this.options.leading && this.canLeadingExecute) {\n this.canLeadingExecute = false\n didLeadingExecute = true\n this.fn(...args)\n this.options.onExecute?.(args, this)\n }\n\n this.lastArgs = args\n\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n }\n\n this.timeoutId = setTimeout(() => {\n this.canLeadingExecute = true\n if (this.options.trailing && !didLeadingExecute && this.lastArgs) {\n this.fn(...this.lastArgs)\n this.options.onExecute?.(this.lastArgs, this)\n }\n this.lastArgs = undefined\n }, this.options.wait)\n }\n\n /**\n * Processes the current pending execution immediately.\n * If there's a pending execution, it will be executed right away\n * and the timeout will be cleared.\n */\n flush = (): void => {\n if (this.timeoutId && this.lastArgs) {\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n const args = this.lastArgs\n this.fn(...args)\n this.options.onExecute?.(args, this)\n this.lastArgs = undefined\n this.canLeadingExecute = true\n }\n }\n\n /**\n * Cancels any pending execution.\n * Clears the timeout and resets the internal state.\n */\n cancel = (): void => {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n }\n this.lastArgs = undefined\n this.canLeadingExecute = true\n }\n}\n\n/**\n * Creates a lightweight debounced function that delays invoking the provided function until after a specified wait time.\n * Multiple calls during the wait period will cancel previous pending invocations and reset the timer.\n *\n * This is an alternative to the debounce function in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core version,\n * this function creates a debouncer with no external dependencies, devtools integration, or reactive state.\n *\n * If leading option is true, the function will execute immediately on the first call, then wait the delay\n * before allowing another execution.\n *\n * @example\n * ```ts\n * const debouncedSave = liteDebounce(() => {\n * saveChanges();\n * }, { wait: 1000 });\n *\n * // Called repeatedly but executes at most once per second\n * inputElement.addEventListener('input', debouncedSave);\n * ```\n *\n * @example\n * ```ts\n * // Leading edge execution - fires immediately then waits\n * const debouncedSearch = liteDebounce((query: string) => {\n * performSearch(query);\n * }, { wait: 300, leading: true });\n * ```\n */\nexport function liteDebounce<TFn extends AnyFunction>(\n fn: TFn,\n options: LiteDebouncerOptions<TFn>,\n): (...args: Parameters<TFn>) => void {\n const debouncer = new LiteDebouncer(fn, options)\n return debouncer.maybeExecute\n}\n"],"names":[],"mappings":"AAmEO,MAAM,cAAuC;AAAA,EAKlD,YACS,IACA,SACP;AAFO,SAAA,KAAA;AACA,SAAA,UAAA;AAJT,SAAQ,oBAAoB;AAqB5B,SAAA,eAAe,IAAI,SAAgC;AACjD,UAAI,oBAAoB;AAExB,UAAI,KAAK,QAAQ,WAAW,KAAK,mBAAmB;AAClD,aAAK,oBAAoB;AACzB,4BAAoB;AACpB,aAAK,GAAG,GAAG,IAAI;AACf,aAAK,QAAQ,YAAY,MAAM,IAAI;AAAA,MACrC;AAEA,WAAK,WAAW;AAEhB,UAAI,KAAK,WAAW;AAClB,qBAAa,KAAK,SAAS;AAAA,MAC7B;AAEA,WAAK,YAAY,WAAW,MAAM;AAChC,aAAK,oBAAoB;AACzB,YAAI,KAAK,QAAQ,YAAY,CAAC,qBAAqB,KAAK,UAAU;AAChE,eAAK,GAAG,GAAG,KAAK,QAAQ;AACxB,eAAK,QAAQ,YAAY,KAAK,UAAU,IAAI;AAAA,QAC9C;AACA,aAAK,WAAW;AAAA,MAClB,GAAG,KAAK,QAAQ,IAAI;AAAA,IACtB;AAOA,SAAA,QAAQ,MAAY;AAClB,UAAI,KAAK,aAAa,KAAK,UAAU;AACnC,qBAAa,KAAK,SAAS;AAC3B,aAAK,YAAY;AACjB,cAAM,OAAO,KAAK;AAClB,aAAK,GAAG,GAAG,IAAI;AACf,aAAK,QAAQ,YAAY,MAAM,IAAI;AACnC,aAAK,WAAW;AAChB,aAAK,oBAAoB;AAAA,MAC3B;AAAA,IACF;AAMA,SAAA,SAAS,MAAY;AACnB,UAAI,KAAK,WAAW;AAClB,qBAAa,KAAK,SAAS;AAC3B,aAAK,YAAY;AAAA,MACnB;AACA,WAAK,WAAW;AAChB,WAAK,oBAAoB;AAAA,IAC3B;AApEE,QACE,KAAK,QAAQ,YAAY,UACzB,KAAK,QAAQ,aAAa,QAC1B;AACA,WAAK,QAAQ,WAAW;AAAA,IAC1B;AAAA,EACF;AA+DF;AA+BO,SAAS,aACd,IACA,SACoC;AACpC,QAAM,YAAY,IAAI,cAAc,IAAI,OAAO;AAC/C,SAAO,UAAU;AACnB;"}
|
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Position type for addItem and getNextItem operations.
|
|
3
|
-
*
|
|
4
|
-
* - 'front': Operate on the front of the queue (FIFO for getNextItem)
|
|
5
|
-
* - 'back': Operate on the back of the queue (LIFO for getNextItem)
|
|
6
|
-
*/
|
|
7
|
-
export type QueuePosition = 'front' | 'back';
|
|
8
|
-
/**
|
|
9
|
-
* Options for configuring a lite queuer instance
|
|
10
|
-
*/
|
|
11
|
-
export interface LiteQueuerOptions<TValue> {
|
|
12
|
-
/**
|
|
13
|
-
* Default position to add items to the queue
|
|
14
|
-
* @default 'back'
|
|
15
|
-
*/
|
|
16
|
-
addItemsTo?: QueuePosition;
|
|
17
|
-
/**
|
|
18
|
-
* Default position to get items from during processing
|
|
19
|
-
* @default 'front'
|
|
20
|
-
*/
|
|
21
|
-
getItemsFrom?: QueuePosition;
|
|
22
|
-
/**
|
|
23
|
-
* Function to determine priority of items in the queue
|
|
24
|
-
* Higher priority items will be processed first
|
|
25
|
-
* Return undefined for items that should use positional ordering
|
|
26
|
-
*/
|
|
27
|
-
getPriority?: (item: TValue) => number | undefined;
|
|
28
|
-
/**
|
|
29
|
-
* Initial items to populate the queue with
|
|
30
|
-
*/
|
|
31
|
-
initialItems?: Array<TValue>;
|
|
32
|
-
/**
|
|
33
|
-
* Maximum number of items allowed in the queue
|
|
34
|
-
*/
|
|
35
|
-
maxSize?: number;
|
|
36
|
-
/**
|
|
37
|
-
* Whether the queuer should start processing items immediately
|
|
38
|
-
* @default true
|
|
39
|
-
*/
|
|
40
|
-
started?: boolean;
|
|
41
|
-
/**
|
|
42
|
-
* Time in milliseconds to wait between processing items
|
|
43
|
-
* @default 0
|
|
44
|
-
*/
|
|
45
|
-
wait?: number;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* A lightweight class that creates a queue for processing items.
|
|
49
|
-
*
|
|
50
|
-
* This is an alternative to the Queuer in the core @tanstack/pacer package, but is more
|
|
51
|
-
* suitable for libraries and npm packages that need minimal overhead. Unlike the core Queuer,
|
|
52
|
-
* this version does not use TanStack Store for state management, has no devtools integration,
|
|
53
|
-
* no callbacks, and provides only essential queueing functionality.
|
|
54
|
-
*
|
|
55
|
-
* The queuer supports FIFO (First In First Out), LIFO (Last In First Out), and priority-based
|
|
56
|
-
* processing of items. Items can be processed automatically with configurable wait times
|
|
57
|
-
* between executions, or processed manually using the execute methods.
|
|
58
|
-
*
|
|
59
|
-
* Features included:
|
|
60
|
-
* - Automatic or manual processing of items
|
|
61
|
-
* - FIFO, LIFO, and priority-based ordering
|
|
62
|
-
* - Queue size limits with item rejection
|
|
63
|
-
* - Configurable wait times between processing
|
|
64
|
-
* - Batch processing capabilities
|
|
65
|
-
* - Start/stop processing control
|
|
66
|
-
* - Callback support for monitoring execution, rejection, and state change events
|
|
67
|
-
*
|
|
68
|
-
* Features NOT included (compared to core Queuer):
|
|
69
|
-
* - No TanStack Store state management
|
|
70
|
-
* - No devtools integration
|
|
71
|
-
* - No item expiration functionality (no onExpire callback)
|
|
72
|
-
* - No dynamic options updates (setOptions)
|
|
73
|
-
* - No detailed state tracking (execution counts, etc.)
|
|
74
|
-
*
|
|
75
|
-
* Queue behavior:
|
|
76
|
-
* - Default: FIFO (add to back, process from front)
|
|
77
|
-
* - LIFO: Configure addItemsTo: 'back', getItemsFrom: 'back'
|
|
78
|
-
* - Priority: Provide getPriority function; higher values processed first
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* ```ts
|
|
82
|
-
* // Basic FIFO queue
|
|
83
|
-
* const queue = new LiteQueuer((item: string) => {
|
|
84
|
-
* console.log('Processing:', item);
|
|
85
|
-
* }, { wait: 100 });
|
|
86
|
-
*
|
|
87
|
-
* queue.addItem('task1');
|
|
88
|
-
* queue.addItem('task2');
|
|
89
|
-
* // Processes: task1, then task2 after 100ms delay
|
|
90
|
-
* ```
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```ts
|
|
94
|
-
* // Priority queue
|
|
95
|
-
* const priorityQueue = new LiteQueuer((item: Task) => {
|
|
96
|
-
* processTask(item);
|
|
97
|
-
* }, {
|
|
98
|
-
* getPriority: task => task.priority,
|
|
99
|
-
* wait: 500
|
|
100
|
-
* });
|
|
101
|
-
*
|
|
102
|
-
* priorityQueue.addItem({ name: 'low', priority: 1 });
|
|
103
|
-
* priorityQueue.addItem({ name: 'high', priority: 10 });
|
|
104
|
-
* // Processes high priority task first
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
export declare class LiteQueuer<TValue> {
|
|
108
|
-
fn: (item: TValue) => void;
|
|
109
|
-
options: LiteQueuerOptions<TValue>;
|
|
110
|
-
private items;
|
|
111
|
-
private timeoutId;
|
|
112
|
-
private isRunning;
|
|
113
|
-
private pendingTick;
|
|
114
|
-
constructor(fn: (item: TValue) => void, options?: LiteQueuerOptions<TValue>);
|
|
115
|
-
/**
|
|
116
|
-
* Number of items currently in the queue
|
|
117
|
-
*/
|
|
118
|
-
get size(): number;
|
|
119
|
-
/**
|
|
120
|
-
* Whether the queue is empty
|
|
121
|
-
*/
|
|
122
|
-
get isEmpty(): boolean;
|
|
123
|
-
/**
|
|
124
|
-
* Whether the queue is currently running (auto-processing items)
|
|
125
|
-
*/
|
|
126
|
-
get isQueueRunning(): boolean;
|
|
127
|
-
/**
|
|
128
|
-
* Adds an item to the queue. If the queue is full, the item is rejected.
|
|
129
|
-
* Items can be inserted at the front or back, and priority ordering is applied if getPriority is configured.
|
|
130
|
-
*
|
|
131
|
-
* Returns true if the item was added, false if the queue is full.
|
|
132
|
-
*
|
|
133
|
-
* @example
|
|
134
|
-
* ```ts
|
|
135
|
-
* queue.addItem('task1'); // Add to default position (back)
|
|
136
|
-
* queue.addItem('task2', 'front'); // Add to front
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
addItem: (item: TValue, position?: QueuePosition, startProcessing?: boolean) => boolean;
|
|
140
|
-
private insertAtPosition;
|
|
141
|
-
/**
|
|
142
|
-
* Removes and returns the next item from the queue without executing the function.
|
|
143
|
-
* Use for manual queue management. Normally, use execute() to process items.
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
* ```ts
|
|
147
|
-
* const nextItem = queue.getNextItem(); // Get from default position (front)
|
|
148
|
-
* const lastItem = queue.getNextItem('back'); // Get from back (LIFO)
|
|
149
|
-
* ```
|
|
150
|
-
*/
|
|
151
|
-
getNextItem: (position?: QueuePosition) => TValue | undefined;
|
|
152
|
-
/**
|
|
153
|
-
* Removes and returns the next item from the queue and processes it using the provided function.
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* ```ts
|
|
157
|
-
* queue.execute(); // Execute from default position
|
|
158
|
-
* queue.execute('back'); // Execute from back (LIFO)
|
|
159
|
-
* ```
|
|
160
|
-
*/
|
|
161
|
-
execute: (position?: QueuePosition) => TValue | undefined;
|
|
162
|
-
/**
|
|
163
|
-
* Internal method that processes items in the queue with wait intervals
|
|
164
|
-
*/
|
|
165
|
-
private tick;
|
|
166
|
-
/**
|
|
167
|
-
* Starts processing items in the queue. If already running, does nothing.
|
|
168
|
-
*/
|
|
169
|
-
start: () => void;
|
|
170
|
-
/**
|
|
171
|
-
* Stops processing items in the queue. Does not clear the queue.
|
|
172
|
-
*/
|
|
173
|
-
stop: () => void;
|
|
174
|
-
/**
|
|
175
|
-
* Clears any pending timeout
|
|
176
|
-
*/
|
|
177
|
-
private clearTimeout;
|
|
178
|
-
/**
|
|
179
|
-
* Returns the next item in the queue without removing it.
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* ```ts
|
|
183
|
-
* const next = queue.peekNextItem(); // Peek at front
|
|
184
|
-
* const last = queue.peekNextItem('back'); // Peek at back
|
|
185
|
-
* ```
|
|
186
|
-
*/
|
|
187
|
-
peekNextItem: (position?: QueuePosition) => TValue | undefined;
|
|
188
|
-
/**
|
|
189
|
-
* Returns a copy of all items in the queue.
|
|
190
|
-
*/
|
|
191
|
-
peekAllItems: () => Array<TValue>;
|
|
192
|
-
/**
|
|
193
|
-
* Processes a specified number of items immediately with no wait time.
|
|
194
|
-
* If no numberOfItems is provided, all items will be processed.
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```ts
|
|
198
|
-
* queue.flush(); // Process all items immediately
|
|
199
|
-
* queue.flush(3); // Process next 3 items immediately
|
|
200
|
-
* ```
|
|
201
|
-
*/
|
|
202
|
-
flush: (numberOfItems?: number, position?: QueuePosition) => void;
|
|
203
|
-
/**
|
|
204
|
-
* Processes all items in the queue as a batch using the provided function.
|
|
205
|
-
* The queue is cleared after processing.
|
|
206
|
-
*
|
|
207
|
-
* @example
|
|
208
|
-
* ```ts
|
|
209
|
-
* queue.flushAsBatch((items) => {
|
|
210
|
-
* console.log('Processing batch:', items);
|
|
211
|
-
* // Process all items together
|
|
212
|
-
* });
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
flushAsBatch: (batchFunction: (items: Array<TValue>) => void) => void;
|
|
216
|
-
/**
|
|
217
|
-
* Removes all items from the queue. Does not affect items being processed.
|
|
218
|
-
*/
|
|
219
|
-
clear: () => void;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Creates a lightweight queue that processes items using the provided function.
|
|
223
|
-
*
|
|
224
|
-
* This is an alternative to the queue function in the core @tanstack/pacer package, but is more
|
|
225
|
-
* suitable for libraries and npm packages that need minimal overhead. Unlike the core version,
|
|
226
|
-
* this function creates a queuer with no external dependencies, devtools integration, or reactive state.
|
|
227
|
-
*
|
|
228
|
-
* @example
|
|
229
|
-
* ```ts
|
|
230
|
-
* const processItem = liteQueue((item: string) => {
|
|
231
|
-
* console.log('Processing:', item);
|
|
232
|
-
* }, { wait: 1000 });
|
|
233
|
-
*
|
|
234
|
-
* processItem('task1');
|
|
235
|
-
* processItem('task2');
|
|
236
|
-
* // Processes each item with 1 second delay between them
|
|
237
|
-
* ```
|
|
238
|
-
*/
|
|
239
|
-
export declare function liteQueue<TValue>(fn: (item: TValue) => void, options?: LiteQueuerOptions<TValue>): (item: TValue) => boolean;
|