@tanstack/pacer 0.8.0 → 0.9.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/cjs/async-batcher.cjs +163 -0
- package/dist/cjs/async-batcher.cjs.map +1 -0
- package/dist/cjs/async-batcher.d.cts +273 -0
- package/dist/cjs/async-debouncer.cjs +149 -162
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +76 -57
- package/dist/cjs/async-queuer.cjs +282 -343
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +121 -100
- package/dist/cjs/async-rate-limiter.cjs +128 -185
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +72 -61
- package/dist/cjs/async-throttler.cjs +168 -178
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +97 -69
- package/dist/cjs/batcher.cjs +110 -119
- package/dist/cjs/batcher.cjs.map +1 -1
- package/dist/cjs/batcher.d.cts +76 -51
- package/dist/cjs/debouncer.cjs +97 -85
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +54 -26
- package/dist/cjs/index.cjs +3 -6
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/cjs/queuer.cjs +247 -294
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +102 -81
- package/dist/cjs/rate-limiter.cjs +97 -130
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +50 -37
- package/dist/cjs/throttler.cjs +107 -123
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +59 -35
- package/dist/cjs/utils.cjs +0 -13
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +0 -1
- package/dist/esm/async-batcher.d.ts +273 -0
- package/dist/esm/async-batcher.js +163 -0
- package/dist/esm/async-batcher.js.map +1 -0
- package/dist/esm/async-debouncer.d.ts +76 -57
- package/dist/esm/async-debouncer.js +149 -162
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +121 -100
- package/dist/esm/async-queuer.js +282 -343
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +72 -61
- package/dist/esm/async-rate-limiter.js +128 -185
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +97 -69
- package/dist/esm/async-throttler.js +168 -178
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/batcher.d.ts +76 -51
- package/dist/esm/batcher.js +110 -119
- package/dist/esm/batcher.js.map +1 -1
- package/dist/esm/debouncer.d.ts +54 -26
- package/dist/esm/debouncer.js +97 -85
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +4 -7
- package/dist/esm/queuer.d.ts +102 -81
- package/dist/esm/queuer.js +247 -294
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +50 -37
- package/dist/esm/rate-limiter.js +97 -130
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +59 -35
- package/dist/esm/throttler.js +107 -123
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/utils.d.ts +0 -1
- package/dist/esm/utils.js +0 -13
- package/dist/esm/utils.js.map +1 -1
- package/package.json +14 -11
- package/src/async-batcher.ts +475 -0
- package/src/async-debouncer.ts +201 -121
- package/src/async-queuer.ts +337 -216
- package/src/async-rate-limiter.ts +176 -136
- package/src/async-throttler.ts +233 -139
- package/src/batcher.ts +158 -92
- package/src/debouncer.ts +135 -52
- package/src/index.ts +1 -1
- package/src/queuer.ts +349 -226
- package/src/rate-limiter.ts +125 -80
- package/src/throttler.ts +152 -78
- package/src/utils.ts +0 -15
- package/dist/cjs/compare.cjs +0 -72
- package/dist/cjs/compare.cjs.map +0 -1
- package/dist/cjs/compare.d.cts +0 -12
- package/dist/esm/compare.d.ts +0 -12
- package/dist/esm/compare.js +0 -72
- package/dist/esm/compare.js.map +0 -1
- package/src/compare.ts +0 -105
package/dist/esm/queuer.js
CHANGED
|
@@ -1,323 +1,276 @@
|
|
|
1
|
+
import { Store } from "@tanstack/store";
|
|
1
2
|
import { parseFunctionOrValue } from "./utils.js";
|
|
3
|
+
function getDefaultQueuerState() {
|
|
4
|
+
return {
|
|
5
|
+
executionCount: 0,
|
|
6
|
+
expirationCount: 0,
|
|
7
|
+
isEmpty: true,
|
|
8
|
+
isFull: false,
|
|
9
|
+
isIdle: true,
|
|
10
|
+
isRunning: true,
|
|
11
|
+
itemTimestamps: [],
|
|
12
|
+
items: [],
|
|
13
|
+
pendingTick: false,
|
|
14
|
+
rejectionCount: 0,
|
|
15
|
+
size: 0,
|
|
16
|
+
status: "idle"
|
|
17
|
+
};
|
|
18
|
+
}
|
|
2
19
|
const defaultOptions = {
|
|
3
20
|
addItemsTo: "back",
|
|
4
21
|
getItemsFrom: "front",
|
|
5
|
-
getPriority: (item) =>
|
|
22
|
+
getPriority: (item) => item?.priority ?? 0,
|
|
6
23
|
getIsExpired: () => false,
|
|
7
24
|
expirationDuration: Infinity,
|
|
8
25
|
initialItems: [],
|
|
9
26
|
maxSize: Infinity,
|
|
10
|
-
onExecute: () => {
|
|
11
|
-
},
|
|
12
|
-
onIsRunningChange: () => {
|
|
13
|
-
},
|
|
14
|
-
onItemsChange: () => {
|
|
15
|
-
},
|
|
16
|
-
onReject: () => {
|
|
17
|
-
},
|
|
18
|
-
onExpire: () => {
|
|
19
|
-
},
|
|
20
27
|
started: true,
|
|
21
28
|
wait: 0
|
|
22
29
|
};
|
|
23
30
|
class Queuer {
|
|
24
31
|
constructor(fn, initialOptions = {}) {
|
|
25
32
|
this.fn = fn;
|
|
26
|
-
this.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this
|
|
30
|
-
this.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
this
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
* Processes items in the queue up to the wait interval. Internal use only.
|
|
62
|
-
*/
|
|
63
|
-
tick() {
|
|
64
|
-
if (!this._running) {
|
|
65
|
-
this._pendingTick = false;
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
this.checkExpiredItems();
|
|
69
|
-
while (!this.getIsEmpty()) {
|
|
70
|
-
const nextItem = this.execute(this._options.getItemsFrom);
|
|
71
|
-
if (nextItem === void 0) {
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
this._onItemsChanges.forEach((cb) => cb(nextItem));
|
|
75
|
-
const wait = this.getWait();
|
|
76
|
-
if (wait > 0) {
|
|
77
|
-
setTimeout(() => this.tick(), wait);
|
|
33
|
+
this.store = new Store(
|
|
34
|
+
getDefaultQueuerState()
|
|
35
|
+
);
|
|
36
|
+
this.#timeoutId = null;
|
|
37
|
+
this.setOptions = (newOptions) => {
|
|
38
|
+
this.options = { ...this.options, ...newOptions };
|
|
39
|
+
};
|
|
40
|
+
this.#setState = (newState) => {
|
|
41
|
+
this.store.setState((state) => {
|
|
42
|
+
const combinedState = {
|
|
43
|
+
...state,
|
|
44
|
+
...newState
|
|
45
|
+
};
|
|
46
|
+
const { items, isRunning } = combinedState;
|
|
47
|
+
const size = items.length;
|
|
48
|
+
const isFull = size >= (this.options.maxSize ?? Infinity);
|
|
49
|
+
const isEmpty = size === 0;
|
|
50
|
+
const isIdle = isRunning && isEmpty;
|
|
51
|
+
const status = isIdle ? "idle" : isRunning ? "running" : "stopped";
|
|
52
|
+
return {
|
|
53
|
+
...combinedState,
|
|
54
|
+
isEmpty,
|
|
55
|
+
isFull,
|
|
56
|
+
isIdle,
|
|
57
|
+
size,
|
|
58
|
+
status
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
this.#getWait = () => {
|
|
63
|
+
return parseFunctionOrValue(this.options.wait ?? 0, this);
|
|
64
|
+
};
|
|
65
|
+
this.#tick = () => {
|
|
66
|
+
if (!this.store.state.isRunning) {
|
|
67
|
+
this.#setState({ pendingTick: false });
|
|
78
68
|
return;
|
|
79
69
|
}
|
|
80
|
-
this
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
for (let i = 0; i < this._items.length; i++) {
|
|
94
|
-
const timestamp = this._itemTimestamps[i];
|
|
95
|
-
if (timestamp === void 0) continue;
|
|
96
|
-
const item = this._items[i];
|
|
97
|
-
if (item === void 0) continue;
|
|
98
|
-
const isExpired = this._options.getIsExpired !== defaultOptions.getIsExpired ? this._options.getIsExpired(item, timestamp) : now - timestamp > this._options.expirationDuration;
|
|
99
|
-
if (isExpired) {
|
|
100
|
-
expiredIndices.push(i);
|
|
70
|
+
this.#setState({ pendingTick: true });
|
|
71
|
+
this.#checkExpiredItems();
|
|
72
|
+
while (this.store.state.items.length > 0) {
|
|
73
|
+
const nextItem = this.execute(this.options.getItemsFrom ?? "front");
|
|
74
|
+
if (nextItem === void 0) {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
const wait = this.#getWait();
|
|
78
|
+
if (wait > 0) {
|
|
79
|
+
this.#timeoutId = setTimeout(() => this.#tick(), wait);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this.#tick();
|
|
101
83
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
this.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
*/
|
|
128
|
-
start() {
|
|
129
|
-
this._running = true;
|
|
130
|
-
if (!this._pendingTick && !this.getIsEmpty()) {
|
|
131
|
-
this._pendingTick = true;
|
|
132
|
-
this.tick();
|
|
133
|
-
}
|
|
134
|
-
this._options.onIsRunningChange(this);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Removes all pending items from the queue. Does not affect items being processed.
|
|
138
|
-
*/
|
|
139
|
-
clear() {
|
|
140
|
-
this._items = [];
|
|
141
|
-
this._options.onItemsChange(this);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Resets the queuer to its initial state. Optionally repopulates with initial items.
|
|
145
|
-
* Does not affect callbacks or options.
|
|
146
|
-
*/
|
|
147
|
-
reset(withInitialItems) {
|
|
148
|
-
this.clear();
|
|
149
|
-
this._executionCount = 0;
|
|
150
|
-
if (withInitialItems) {
|
|
151
|
-
this._items = [...this._options.initialItems];
|
|
152
|
-
}
|
|
153
|
-
this._running = this._options.started;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
|
|
157
|
-
* Items can be inserted based on priority or at the front/back depending on configuration.
|
|
158
|
-
*
|
|
159
|
-
* Returns true if the item was added, false if the queue is full.
|
|
160
|
-
*
|
|
161
|
-
* Example usage:
|
|
162
|
-
* ```ts
|
|
163
|
-
* queuer.addItem('task');
|
|
164
|
-
* queuer.addItem('task2', 'front');
|
|
165
|
-
* ```
|
|
166
|
-
*/
|
|
167
|
-
addItem(item, position = this._options.addItemsTo, runOnUpdate = true) {
|
|
168
|
-
if (this.getIsFull()) {
|
|
169
|
-
this._rejectionCount++;
|
|
170
|
-
this._options.onReject(item, this);
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
if (this._options.getPriority !== defaultOptions.getPriority) {
|
|
174
|
-
const priority = this._options.getPriority(item);
|
|
175
|
-
const insertIndex = this._items.findIndex(
|
|
176
|
-
(existing) => this._options.getPriority(existing) < priority
|
|
177
|
-
);
|
|
178
|
-
if (insertIndex === -1) {
|
|
179
|
-
this._items.push(item);
|
|
180
|
-
this._itemTimestamps.push(Date.now());
|
|
84
|
+
this.#setState({ pendingTick: false });
|
|
85
|
+
};
|
|
86
|
+
this.addItem = (item, position = this.options.addItemsTo ?? "back", runOnItemsChange = true) => {
|
|
87
|
+
if (this.store.state.items.length >= (this.options.maxSize ?? Infinity)) {
|
|
88
|
+
this.#setState({
|
|
89
|
+
rejectionCount: this.store.state.rejectionCount + 1
|
|
90
|
+
});
|
|
91
|
+
this.options.onReject?.(item, this);
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
const priority = this.options.getPriority !== defaultOptions.getPriority ? this.options.getPriority(item) : item.priority;
|
|
95
|
+
const items = this.store.state.items;
|
|
96
|
+
const itemTimestamps = this.store.state.itemTimestamps;
|
|
97
|
+
if (priority !== void 0) {
|
|
98
|
+
const insertIndex = items.findIndex((existing) => {
|
|
99
|
+
const existingPriority = this.options.getPriority !== defaultOptions.getPriority ? this.options.getPriority(existing) : existing.priority;
|
|
100
|
+
return existingPriority < priority;
|
|
101
|
+
});
|
|
102
|
+
if (insertIndex === -1) {
|
|
103
|
+
items.push(item);
|
|
104
|
+
itemTimestamps.push(Date.now());
|
|
105
|
+
} else {
|
|
106
|
+
items.splice(insertIndex, 0, item);
|
|
107
|
+
itemTimestamps.splice(insertIndex, 0, Date.now());
|
|
108
|
+
}
|
|
181
109
|
} else {
|
|
182
|
-
|
|
183
|
-
|
|
110
|
+
if (position === "front") {
|
|
111
|
+
items.unshift(item);
|
|
112
|
+
itemTimestamps.unshift(Date.now());
|
|
113
|
+
} else {
|
|
114
|
+
items.push(item);
|
|
115
|
+
itemTimestamps.push(Date.now());
|
|
116
|
+
}
|
|
184
117
|
}
|
|
185
|
-
|
|
118
|
+
this.#setState({
|
|
119
|
+
items,
|
|
120
|
+
itemTimestamps
|
|
121
|
+
});
|
|
122
|
+
if (runOnItemsChange) {
|
|
123
|
+
this.options.onItemsChange?.(this);
|
|
124
|
+
}
|
|
125
|
+
if (this.store.state.isRunning && !this.store.state.pendingTick) {
|
|
126
|
+
this.#setState({ pendingTick: true });
|
|
127
|
+
this.#tick();
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
};
|
|
131
|
+
this.getNextItem = (position = this.options.getItemsFrom ?? "front") => {
|
|
132
|
+
const { items, itemTimestamps } = this.store.state;
|
|
133
|
+
let item;
|
|
186
134
|
if (position === "front") {
|
|
187
|
-
|
|
188
|
-
|
|
135
|
+
item = items[0];
|
|
136
|
+
if (item !== void 0) {
|
|
137
|
+
this.#setState({
|
|
138
|
+
items: items.slice(1),
|
|
139
|
+
itemTimestamps: itemTimestamps.slice(1)
|
|
140
|
+
});
|
|
141
|
+
}
|
|
189
142
|
} else {
|
|
190
|
-
|
|
191
|
-
|
|
143
|
+
item = items[items.length - 1];
|
|
144
|
+
if (item !== void 0) {
|
|
145
|
+
this.#setState({
|
|
146
|
+
items: items.slice(0, -1),
|
|
147
|
+
itemTimestamps: itemTimestamps.slice(0, -1)
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (item !== void 0) {
|
|
152
|
+
this.options.onItemsChange?.(this);
|
|
153
|
+
}
|
|
154
|
+
return item;
|
|
155
|
+
};
|
|
156
|
+
this.execute = (position) => {
|
|
157
|
+
const item = this.getNextItem(position);
|
|
158
|
+
if (item !== void 0) {
|
|
159
|
+
this.fn(item);
|
|
160
|
+
this.#setState({
|
|
161
|
+
executionCount: this.store.state.executionCount + 1
|
|
162
|
+
});
|
|
163
|
+
this.options.onExecute?.(item, this);
|
|
164
|
+
}
|
|
165
|
+
return item;
|
|
166
|
+
};
|
|
167
|
+
this.flush = (numberOfItems = this.store.state.items.length, position) => {
|
|
168
|
+
this.#clearTimeout();
|
|
169
|
+
for (let i = 0; i < numberOfItems; i++) {
|
|
170
|
+
this.execute(position);
|
|
171
|
+
}
|
|
172
|
+
this.#tick();
|
|
173
|
+
};
|
|
174
|
+
this.#checkExpiredItems = () => {
|
|
175
|
+
if ((this.options.expirationDuration ?? Infinity) === Infinity && this.options.getIsExpired === defaultOptions.getIsExpired) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const now = Date.now();
|
|
179
|
+
const expiredIndices = [];
|
|
180
|
+
for (let i = 0; i < this.store.state.items.length; i++) {
|
|
181
|
+
const timestamp = this.store.state.itemTimestamps[i];
|
|
182
|
+
if (timestamp === void 0) continue;
|
|
183
|
+
const item = this.store.state.items[i];
|
|
184
|
+
if (item === void 0) continue;
|
|
185
|
+
const isExpired = this.options.getIsExpired !== defaultOptions.getIsExpired ? this.options.getIsExpired(item, timestamp) : now - timestamp > (this.options.expirationDuration ?? Infinity);
|
|
186
|
+
if (isExpired) {
|
|
187
|
+
expiredIndices.push(i);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
for (let i = expiredIndices.length - 1; i >= 0; i--) {
|
|
191
|
+
const index = expiredIndices[i];
|
|
192
|
+
if (index === void 0) continue;
|
|
193
|
+
const expiredItem = this.store.state.items[index];
|
|
194
|
+
if (expiredItem === void 0) continue;
|
|
195
|
+
const newItems = [...this.store.state.items];
|
|
196
|
+
const newTimestamps = [...this.store.state.itemTimestamps];
|
|
197
|
+
newItems.splice(index, 1);
|
|
198
|
+
newTimestamps.splice(index, 1);
|
|
199
|
+
this.#setState({
|
|
200
|
+
items: newItems,
|
|
201
|
+
itemTimestamps: newTimestamps,
|
|
202
|
+
expirationCount: this.store.state.expirationCount + 1
|
|
203
|
+
});
|
|
204
|
+
this.options.onExpire?.(expiredItem, this);
|
|
205
|
+
}
|
|
206
|
+
if (expiredIndices.length > 0) {
|
|
207
|
+
this.options.onItemsChange?.(this);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
this.peekNextItem = (position = "front") => {
|
|
211
|
+
if (position === "front") {
|
|
212
|
+
return this.store.state.items[0];
|
|
213
|
+
}
|
|
214
|
+
return this.store.state.items[this.store.state.items.length - 1];
|
|
215
|
+
};
|
|
216
|
+
this.peekAllItems = () => {
|
|
217
|
+
return [...this.store.state.items];
|
|
218
|
+
};
|
|
219
|
+
this.start = () => {
|
|
220
|
+
this.#setState({ isRunning: true });
|
|
221
|
+
if (!this.store.state.pendingTick && this.store.state.items.length > 0) {
|
|
222
|
+
this.#tick();
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
this.stop = () => {
|
|
226
|
+
this.#clearTimeout();
|
|
227
|
+
this.#setState({ isRunning: false, pendingTick: false });
|
|
228
|
+
};
|
|
229
|
+
this.#clearTimeout = () => {
|
|
230
|
+
if (this.#timeoutId) {
|
|
231
|
+
clearTimeout(this.#timeoutId);
|
|
232
|
+
this.#timeoutId = null;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
this.clear = () => {
|
|
236
|
+
this.#setState({ items: [], itemTimestamps: [] });
|
|
237
|
+
this.options.onItemsChange?.(this);
|
|
238
|
+
};
|
|
239
|
+
this.reset = () => {
|
|
240
|
+
this.#setState(getDefaultQueuerState());
|
|
241
|
+
this.options.onItemsChange?.(this);
|
|
242
|
+
};
|
|
243
|
+
this.options = {
|
|
244
|
+
...defaultOptions,
|
|
245
|
+
...initialOptions
|
|
246
|
+
};
|
|
247
|
+
const isInitiallyRunning = this.options.initialState?.isRunning ?? this.options.started ?? true;
|
|
248
|
+
this.#setState({
|
|
249
|
+
...this.options.initialState,
|
|
250
|
+
isRunning: isInitiallyRunning
|
|
251
|
+
});
|
|
252
|
+
if (this.options.initialState?.items) {
|
|
253
|
+
if (this.store.state.isRunning) {
|
|
254
|
+
this.#tick();
|
|
192
255
|
}
|
|
193
|
-
}
|
|
194
|
-
if (this._running && !this._pendingTick) {
|
|
195
|
-
this._pendingTick = true;
|
|
196
|
-
this.tick();
|
|
197
|
-
}
|
|
198
|
-
if (runOnUpdate) {
|
|
199
|
-
this._options.onItemsChange(this);
|
|
200
|
-
}
|
|
201
|
-
return true;
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Removes and returns the next item from the queue without executing the function.
|
|
205
|
-
* Use for manual queue management. Normally, use execute() to process items.
|
|
206
|
-
*
|
|
207
|
-
* Example usage:
|
|
208
|
-
* ```ts
|
|
209
|
-
* // FIFO
|
|
210
|
-
* queuer.getNextItem();
|
|
211
|
-
* // LIFO
|
|
212
|
-
* queuer.getNextItem('back');
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
getNextItem(position = this._options.getItemsFrom) {
|
|
216
|
-
let item;
|
|
217
|
-
if (position === "front") {
|
|
218
|
-
item = this._items.shift();
|
|
219
|
-
this._itemTimestamps.shift();
|
|
220
256
|
} else {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
return item;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Removes and returns the next item from the queue and processes it using the provided function.
|
|
231
|
-
*
|
|
232
|
-
* Example usage:
|
|
233
|
-
* ```ts
|
|
234
|
-
* queuer.execute();
|
|
235
|
-
* // LIFO
|
|
236
|
-
* queuer.execute('back');
|
|
237
|
-
* ```
|
|
238
|
-
*/
|
|
239
|
-
execute(position) {
|
|
240
|
-
const item = this.getNextItem(position);
|
|
241
|
-
if (item !== void 0) {
|
|
242
|
-
this.fn(item);
|
|
243
|
-
this._executionCount++;
|
|
244
|
-
this._options.onExecute(item, this);
|
|
245
|
-
}
|
|
246
|
-
return item;
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Returns the next item in the queue without removing it.
|
|
250
|
-
*
|
|
251
|
-
* Example usage:
|
|
252
|
-
* ```ts
|
|
253
|
-
* queuer.peekNextItem(); // front
|
|
254
|
-
* queuer.peekNextItem('back'); // back
|
|
255
|
-
* ```
|
|
256
|
-
*/
|
|
257
|
-
peekNextItem(position = this._options.getItemsFrom) {
|
|
258
|
-
if (position === "front") {
|
|
259
|
-
return this._items[0];
|
|
257
|
+
for (let i = 0; i < (this.options.initialItems?.length ?? 0); i++) {
|
|
258
|
+
const item = this.options.initialItems[i];
|
|
259
|
+
const isLast = i === (this.options.initialItems?.length ?? 0) - 1;
|
|
260
|
+
this.addItem(item, this.options.addItemsTo ?? "back", isLast);
|
|
261
|
+
}
|
|
260
262
|
}
|
|
261
|
-
return this._items[this._items.length - 1];
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Returns true if the queue is empty (no pending items).
|
|
265
|
-
*/
|
|
266
|
-
getIsEmpty() {
|
|
267
|
-
return this._items.length === 0;
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Returns true if the queue is full (reached maxSize).
|
|
271
|
-
*/
|
|
272
|
-
getIsFull() {
|
|
273
|
-
return this._items.length >= this._options.maxSize;
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Returns the number of pending items in the queue.
|
|
277
|
-
*/
|
|
278
|
-
getSize() {
|
|
279
|
-
return this._items.length;
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Returns a copy of all items in the queue.
|
|
283
|
-
*/
|
|
284
|
-
peekAllItems() {
|
|
285
|
-
return [...this._items];
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Returns the number of items that have been processed and removed from the queue.
|
|
289
|
-
*/
|
|
290
|
-
getExecutionCount() {
|
|
291
|
-
return this._executionCount;
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Returns the number of items that have been rejected from being added to the queue.
|
|
295
|
-
*/
|
|
296
|
-
getRejectionCount() {
|
|
297
|
-
return this._rejectionCount;
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Returns the number of items that have expired and been removed from the queue.
|
|
301
|
-
*/
|
|
302
|
-
getExpirationCount() {
|
|
303
|
-
return this._expirationCount;
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Returns true if the queuer is currently running (processing items).
|
|
307
|
-
*/
|
|
308
|
-
getIsRunning() {
|
|
309
|
-
return this._running;
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Returns true if the queuer is running but has no items to process.
|
|
313
|
-
*/
|
|
314
|
-
getIsIdle() {
|
|
315
|
-
return this._running && this.getIsEmpty();
|
|
316
263
|
}
|
|
264
|
+
#timeoutId;
|
|
265
|
+
#setState;
|
|
266
|
+
#getWait;
|
|
267
|
+
#tick;
|
|
268
|
+
#checkExpiredItems;
|
|
269
|
+
#clearTimeout;
|
|
317
270
|
}
|
|
318
|
-
function queue(fn,
|
|
319
|
-
const queuer = new Queuer(fn,
|
|
320
|
-
return queuer.addItem
|
|
271
|
+
function queue(fn, initialOptions) {
|
|
272
|
+
const queuer = new Queuer(fn, initialOptions);
|
|
273
|
+
return queuer.addItem;
|
|
321
274
|
}
|
|
322
275
|
export {
|
|
323
276
|
Queuer,
|