@tanstack/pacer 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/async-debouncer.cjs +112 -63
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +66 -25
- package/dist/cjs/async-queuer.cjs +198 -124
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +91 -49
- package/dist/cjs/async-rate-limiter.cjs +83 -55
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +55 -28
- package/dist/cjs/async-throttler.cjs +121 -70
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +75 -25
- package/dist/cjs/debouncer.cjs +45 -23
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +27 -12
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/queuer.cjs +161 -101
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +80 -38
- package/dist/cjs/rate-limiter.cjs +52 -44
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +38 -46
- package/dist/cjs/throttler.cjs +57 -44
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +35 -23
- package/dist/cjs/types.d.cts +8 -0
- package/dist/cjs/utils.cjs +13 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +1 -0
- package/dist/esm/async-debouncer.d.ts +66 -25
- package/dist/esm/async-debouncer.js +112 -63
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +91 -49
- package/dist/esm/async-queuer.js +198 -124
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +55 -28
- package/dist/esm/async-rate-limiter.js +83 -55
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +75 -25
- package/dist/esm/async-throttler.js +121 -70
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/debouncer.d.ts +27 -12
- package/dist/esm/debouncer.js +45 -23
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +80 -38
- package/dist/esm/queuer.js +161 -101
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +38 -46
- package/dist/esm/rate-limiter.js +52 -44
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +35 -23
- package/dist/esm/throttler.js +57 -44
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/types.d.ts +8 -0
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.js +13 -0
- package/dist/esm/utils.js.map +1 -0
- package/package.json +8 -1
- package/src/async-debouncer.ts +157 -88
- package/src/async-queuer.ts +266 -148
- package/src/async-rate-limiter.ts +123 -83
- package/src/async-throttler.ts +173 -89
- package/src/debouncer.ts +71 -42
- package/src/index.ts +2 -0
- package/src/queuer.ts +219 -114
- package/src/rate-limiter.ts +74 -88
- package/src/throttler.ts +83 -65
- package/src/types.ts +9 -0
- package/src/utils.ts +13 -0
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
CHANGED
package/dist/cjs/queuer.cjs
CHANGED
|
@@ -3,91 +3,183 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const defaultOptions = {
|
|
4
4
|
addItemsTo: "back",
|
|
5
5
|
getItemsFrom: "front",
|
|
6
|
-
getPriority: () => 0,
|
|
6
|
+
getPriority: (item) => (item == null ? void 0 : item.priority) ?? 0,
|
|
7
|
+
getIsExpired: () => false,
|
|
8
|
+
expirationDuration: Infinity,
|
|
7
9
|
initialItems: [],
|
|
8
10
|
maxSize: Infinity,
|
|
9
11
|
onGetNextItem: () => {
|
|
10
12
|
},
|
|
11
|
-
|
|
13
|
+
onIsRunningChange: () => {
|
|
14
|
+
},
|
|
15
|
+
onItemsChange: () => {
|
|
16
|
+
},
|
|
17
|
+
onReject: () => {
|
|
18
|
+
},
|
|
19
|
+
onExpire: () => {
|
|
12
20
|
},
|
|
13
21
|
started: false,
|
|
14
22
|
wait: 0
|
|
15
23
|
};
|
|
16
24
|
class Queuer {
|
|
17
25
|
constructor(initialOptions = defaultOptions) {
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
this._items = [];
|
|
27
|
+
this._itemTimestamps = [];
|
|
28
|
+
this._executionCount = 0;
|
|
29
|
+
this._rejectionCount = 0;
|
|
30
|
+
this._expirationCount = 0;
|
|
31
|
+
this._onItemsChanges = [];
|
|
32
|
+
this._pendingTick = false;
|
|
33
|
+
this._options = { ...defaultOptions, ...initialOptions };
|
|
34
|
+
this._running = this._options.started;
|
|
35
|
+
for (let i = 0; i < this._options.initialItems.length; i++) {
|
|
36
|
+
const item = this._options.initialItems[i];
|
|
37
|
+
const isLast = i === this._options.initialItems.length - 1;
|
|
38
|
+
this.addItem(item, this._options.addItemsTo, isLast);
|
|
28
39
|
}
|
|
29
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Updates the queuer options
|
|
43
|
+
* Returns the new options state
|
|
44
|
+
*/
|
|
45
|
+
setOptions(newOptions) {
|
|
46
|
+
this._options = { ...this._options, ...newOptions };
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns the current queuer options
|
|
50
|
+
*/
|
|
51
|
+
getOptions() {
|
|
52
|
+
return this._options;
|
|
53
|
+
}
|
|
30
54
|
/**
|
|
31
55
|
* Processes items in the queuer
|
|
32
56
|
*/
|
|
33
57
|
tick() {
|
|
34
|
-
if (!this.
|
|
35
|
-
this.
|
|
58
|
+
if (!this._running) {
|
|
59
|
+
this._pendingTick = false;
|
|
36
60
|
return;
|
|
37
61
|
}
|
|
38
|
-
|
|
39
|
-
|
|
62
|
+
this.checkExpiredItems();
|
|
63
|
+
while (!this.getIsEmpty()) {
|
|
64
|
+
const nextItem = this.getNextItem(this._options.getItemsFrom);
|
|
40
65
|
if (nextItem === void 0) {
|
|
41
66
|
break;
|
|
42
67
|
}
|
|
43
|
-
this.
|
|
44
|
-
if (this.
|
|
45
|
-
setTimeout(() => this.tick(), this.
|
|
68
|
+
this._onItemsChanges.forEach((cb) => cb(nextItem));
|
|
69
|
+
if (this._options.wait > 0) {
|
|
70
|
+
setTimeout(() => this.tick(), this._options.wait);
|
|
46
71
|
return;
|
|
47
72
|
}
|
|
48
73
|
this.tick();
|
|
49
74
|
}
|
|
50
|
-
this.
|
|
75
|
+
this._pendingTick = false;
|
|
51
76
|
}
|
|
52
77
|
/**
|
|
53
|
-
*
|
|
54
|
-
* Returns the new options state
|
|
78
|
+
* Checks for and removes expired items from the queuer
|
|
55
79
|
*/
|
|
56
|
-
|
|
57
|
-
this.
|
|
58
|
-
|
|
80
|
+
checkExpiredItems() {
|
|
81
|
+
if (this._options.expirationDuration === Infinity && this._options.getIsExpired === defaultOptions.getIsExpired)
|
|
82
|
+
return;
|
|
83
|
+
const now = Date.now();
|
|
84
|
+
const expiredIndices = [];
|
|
85
|
+
for (let i = 0; i < this._items.length; i++) {
|
|
86
|
+
const timestamp = this._itemTimestamps[i];
|
|
87
|
+
if (timestamp === void 0) continue;
|
|
88
|
+
const item = this._items[i];
|
|
89
|
+
if (item === void 0) continue;
|
|
90
|
+
const isExpired = this._options.getIsExpired !== defaultOptions.getIsExpired ? this._options.getIsExpired(item, timestamp) : now - timestamp > this._options.expirationDuration;
|
|
91
|
+
if (isExpired) {
|
|
92
|
+
expiredIndices.push(i);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
for (let i = expiredIndices.length - 1; i >= 0; i--) {
|
|
96
|
+
const index = expiredIndices[i];
|
|
97
|
+
if (index === void 0) continue;
|
|
98
|
+
const expiredItem = this._items[index];
|
|
99
|
+
if (expiredItem === void 0) continue;
|
|
100
|
+
this._items.splice(index, 1);
|
|
101
|
+
this._itemTimestamps.splice(index, 1);
|
|
102
|
+
this._expirationCount++;
|
|
103
|
+
this._options.onExpire(expiredItem, this);
|
|
104
|
+
}
|
|
105
|
+
if (expiredIndices.length > 0) {
|
|
106
|
+
this._options.onItemsChange(this);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Stops the queuer from processing items
|
|
111
|
+
*/
|
|
112
|
+
stop() {
|
|
113
|
+
this._running = false;
|
|
114
|
+
this._pendingTick = false;
|
|
115
|
+
this._options.onIsRunningChange(this);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Starts the queuer and processes items
|
|
119
|
+
*/
|
|
120
|
+
start() {
|
|
121
|
+
this._running = true;
|
|
122
|
+
if (!this._pendingTick && !this.getIsEmpty()) {
|
|
123
|
+
this._pendingTick = true;
|
|
124
|
+
this.tick();
|
|
125
|
+
}
|
|
126
|
+
this._options.onIsRunningChange(this);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Removes all items from the queuer
|
|
130
|
+
*/
|
|
131
|
+
clear() {
|
|
132
|
+
this._items = [];
|
|
133
|
+
this._options.onItemsChange(this);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Resets the queuer to its initial state
|
|
137
|
+
*/
|
|
138
|
+
reset(withInitialItems) {
|
|
139
|
+
this.clear();
|
|
140
|
+
this._executionCount = 0;
|
|
141
|
+
if (withInitialItems) {
|
|
142
|
+
this._items = [...this._options.initialItems];
|
|
143
|
+
}
|
|
144
|
+
this._running = this._options.started;
|
|
59
145
|
}
|
|
60
146
|
/**
|
|
61
147
|
* Adds an item to the queuer and starts processing if not already running
|
|
62
148
|
* @returns true if item was added, false if queuer is full
|
|
63
149
|
*/
|
|
64
|
-
addItem(item, position = this.
|
|
65
|
-
if (this.
|
|
150
|
+
addItem(item, position = this._options.addItemsTo, runOnUpdate = true) {
|
|
151
|
+
if (this.getIsFull()) {
|
|
152
|
+
this._rejectionCount++;
|
|
153
|
+
this._options.onReject(item, this);
|
|
66
154
|
return false;
|
|
67
155
|
}
|
|
68
|
-
if (this.
|
|
69
|
-
const priority = this.
|
|
70
|
-
const insertIndex = this.
|
|
71
|
-
(existing) => this.
|
|
156
|
+
if (this._options.getPriority !== defaultOptions.getPriority) {
|
|
157
|
+
const priority = this._options.getPriority(item);
|
|
158
|
+
const insertIndex = this._items.findIndex(
|
|
159
|
+
(existing) => this._options.getPriority(existing) > priority
|
|
72
160
|
);
|
|
73
161
|
if (insertIndex === -1) {
|
|
74
|
-
this.
|
|
162
|
+
this._items.push(item);
|
|
163
|
+
this._itemTimestamps.push(Date.now());
|
|
75
164
|
} else {
|
|
76
|
-
this.
|
|
165
|
+
this._items.splice(insertIndex, 0, item);
|
|
166
|
+
this._itemTimestamps.splice(insertIndex, 0, Date.now());
|
|
77
167
|
}
|
|
78
168
|
} else {
|
|
79
169
|
if (position === "front") {
|
|
80
|
-
this.
|
|
170
|
+
this._items.unshift(item);
|
|
171
|
+
this._itemTimestamps.unshift(Date.now());
|
|
81
172
|
} else {
|
|
82
|
-
this.
|
|
173
|
+
this._items.push(item);
|
|
174
|
+
this._itemTimestamps.push(Date.now());
|
|
83
175
|
}
|
|
84
176
|
}
|
|
85
|
-
if (this.
|
|
86
|
-
this.
|
|
177
|
+
if (this._running && !this._pendingTick) {
|
|
178
|
+
this._pendingTick = true;
|
|
87
179
|
this.tick();
|
|
88
180
|
}
|
|
89
181
|
if (runOnUpdate) {
|
|
90
|
-
this.
|
|
182
|
+
this._options.onItemsChange(this);
|
|
91
183
|
}
|
|
92
184
|
return true;
|
|
93
185
|
}
|
|
@@ -102,17 +194,19 @@ class Queuer {
|
|
|
102
194
|
* queuer.getNextItem('back')
|
|
103
195
|
* ```
|
|
104
196
|
*/
|
|
105
|
-
getNextItem(position = this.
|
|
197
|
+
getNextItem(position = this._options.getItemsFrom) {
|
|
106
198
|
let item;
|
|
107
199
|
if (position === "front") {
|
|
108
|
-
item = this.
|
|
200
|
+
item = this._items.shift();
|
|
201
|
+
this._itemTimestamps.shift();
|
|
109
202
|
} else {
|
|
110
|
-
item = this.
|
|
203
|
+
item = this._items.pop();
|
|
204
|
+
this._itemTimestamps.pop();
|
|
111
205
|
}
|
|
112
206
|
if (item !== void 0) {
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
this.
|
|
207
|
+
this._executionCount++;
|
|
208
|
+
this._options.onItemsChange(this);
|
|
209
|
+
this._options.onGetNextItem(item, this);
|
|
116
210
|
}
|
|
117
211
|
return item;
|
|
118
212
|
}
|
|
@@ -122,104 +216,70 @@ class Queuer {
|
|
|
122
216
|
* @example
|
|
123
217
|
* ```ts
|
|
124
218
|
* // Look at next item to getNextItem
|
|
125
|
-
* queuer.
|
|
219
|
+
* queuer.getPeek()
|
|
126
220
|
* // Look at last item (like stack top)
|
|
127
|
-
* queuer.
|
|
221
|
+
* queuer.getPeek('back')
|
|
128
222
|
* ```
|
|
129
223
|
*/
|
|
130
|
-
|
|
224
|
+
getPeek(position = this._options.getItemsFrom) {
|
|
131
225
|
if (position === "front") {
|
|
132
|
-
return this.
|
|
226
|
+
return this._items[0];
|
|
133
227
|
}
|
|
134
|
-
return this.
|
|
228
|
+
return this._items[this._items.length - 1];
|
|
135
229
|
}
|
|
136
230
|
/**
|
|
137
231
|
* Returns true if the queuer is empty
|
|
138
232
|
*/
|
|
139
|
-
|
|
140
|
-
return this.
|
|
233
|
+
getIsEmpty() {
|
|
234
|
+
return this._items.length === 0;
|
|
141
235
|
}
|
|
142
236
|
/**
|
|
143
237
|
* Returns true if the queuer is full
|
|
144
238
|
*/
|
|
145
|
-
|
|
146
|
-
return this.
|
|
239
|
+
getIsFull() {
|
|
240
|
+
return this._items.length >= this._options.maxSize;
|
|
147
241
|
}
|
|
148
242
|
/**
|
|
149
243
|
* Returns the current size of the queuer
|
|
150
244
|
*/
|
|
151
|
-
|
|
152
|
-
return this.
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Removes all items from the queuer
|
|
156
|
-
*/
|
|
157
|
-
clear() {
|
|
158
|
-
this.items = [];
|
|
159
|
-
this.options.onUpdate(this);
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Resets the queuer to its initial state
|
|
163
|
-
*/
|
|
164
|
-
reset(withInitialItems) {
|
|
165
|
-
this.clear();
|
|
166
|
-
this.executionCount = 0;
|
|
167
|
-
if (withInitialItems) {
|
|
168
|
-
this.items = [...this.options.initialItems];
|
|
169
|
-
}
|
|
170
|
-
this.running = this.options.started;
|
|
245
|
+
getSize() {
|
|
246
|
+
return this._items.length;
|
|
171
247
|
}
|
|
172
248
|
/**
|
|
173
249
|
* Returns a copy of all items in the queuer
|
|
174
250
|
*/
|
|
175
251
|
getAllItems() {
|
|
176
|
-
return [...this.
|
|
252
|
+
return [...this._items];
|
|
177
253
|
}
|
|
178
254
|
/**
|
|
179
255
|
* Returns the number of items that have been removed from the queuer
|
|
180
256
|
*/
|
|
181
257
|
getExecutionCount() {
|
|
182
|
-
return this.
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Adds a callback to be called when an item is processed
|
|
186
|
-
*/
|
|
187
|
-
onUpdate(cb) {
|
|
188
|
-
this.onUpdates.push(cb);
|
|
189
|
-
return () => {
|
|
190
|
-
this.onUpdates = this.onUpdates.filter((d) => d !== cb);
|
|
191
|
-
};
|
|
258
|
+
return this._executionCount;
|
|
192
259
|
}
|
|
193
260
|
/**
|
|
194
|
-
*
|
|
261
|
+
* Returns the number of items that have been rejected from the queuer
|
|
195
262
|
*/
|
|
196
|
-
|
|
197
|
-
this.
|
|
198
|
-
this.pendingTick = false;
|
|
199
|
-
this.options.onUpdate(this);
|
|
263
|
+
getRejectionCount() {
|
|
264
|
+
return this._rejectionCount;
|
|
200
265
|
}
|
|
201
266
|
/**
|
|
202
|
-
*
|
|
267
|
+
* Returns the number of items that have expired from the queuer
|
|
203
268
|
*/
|
|
204
|
-
|
|
205
|
-
this.
|
|
206
|
-
if (!this.pendingTick && !this.isEmpty()) {
|
|
207
|
-
this.pendingTick = true;
|
|
208
|
-
this.tick();
|
|
209
|
-
}
|
|
210
|
-
this.options.onUpdate(this);
|
|
269
|
+
getExpirationCount() {
|
|
270
|
+
return this._expirationCount;
|
|
211
271
|
}
|
|
212
272
|
/**
|
|
213
273
|
* Returns true if the queuer is running
|
|
214
274
|
*/
|
|
215
|
-
|
|
216
|
-
return this.
|
|
275
|
+
getIsRunning() {
|
|
276
|
+
return this._running;
|
|
217
277
|
}
|
|
218
278
|
/**
|
|
219
279
|
* Returns true if the queuer is running but has no items to process
|
|
220
280
|
*/
|
|
221
|
-
|
|
222
|
-
return this.
|
|
281
|
+
getIsIdle() {
|
|
282
|
+
return this._running && this.getIsEmpty();
|
|
223
283
|
}
|
|
224
284
|
}
|
|
225
285
|
function queue(options = {}) {
|
package/dist/cjs/queuer.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queuer.cjs","sources":["../../src/queuer.ts"],"sourcesContent":["/**\n * Options for configuring a Queuer instance\n */\nexport interface QueuerOptions<TValue> {\n /**\n * Default position to add items to the queuer\n * @default 'back'\n */\n addItemsTo?: QueuePosition\n /**\n * Default position to get items from during processing\n * @default 'front'\n */\n getItemsFrom?: QueuePosition\n /**\n * Function to determine priority of items in the queuer\n * Higher priority items will be processed first\n */\n getPriority?: (item: TValue) => number\n /**\n * Initial items to populate the queuer with\n */\n initialItems?: Array<TValue>\n /**\n * Maximum number of items allowed in the queuer\n */\n maxSize?: number\n /**\n * Callback fired whenever an item is removed from the queuer\n */\n onGetNextItem?: (item: TValue, queuer: Queuer<TValue>) => void\n /**\n * Callback fired whenever an item is added or removed from the queuer\n */\n onUpdate?: (queuer: Queuer<TValue>) => void\n /**\n * Whether the queuer should start processing tasks immediately\n */\n started?: boolean\n /**\n * Time in milliseconds to wait between processing items\n */\n wait?: number\n}\n\nconst defaultOptions: Required<QueuerOptions<any>> = {\n addItemsTo: 'back',\n getItemsFrom: 'front',\n getPriority: () => 0,\n initialItems: [],\n maxSize: Infinity,\n onGetNextItem: () => {},\n onUpdate: () => {},\n started: false,\n wait: 0,\n}\n\n/**\n * Position type for addItem and getNextItem operations\n */\nexport type QueuePosition = 'front' | 'back'\n\n/**\n * A flexible queue data structure that defaults to FIFO (First In First Out) behavior\n * with optional position overrides for stack-like or double-ended operations.\n *\n * The queuer can automatically process items as they are added, with configurable\n * wait times between processing each item. Processing can be started/stopped\n * and the queuer will maintain its state.\n *\n * Supports priority-based ordering when a getPriority function is provided.\n * Items with higher priority values will be processed first.\n *\n * Default queue behavior:\n * - addItem(item): adds to back of queuer\n * - getNextItem(): removes and returns from front of queuer\n *\n * Stack (LIFO) behavior:\n * - addItem(item, 'back'): adds to back\n * - getNextItem('back'): removes and returns from back\n *\n * Double-ended queuer behavior:\n * - addItem(item, position): adds to specified position ('front' or 'back')\n * - getNextItem(position): removes and returns from specified position\n *\n * Processing behavior:\n * - start(): begins processing items in the queuer\n * - stop(): pauses processing\n * - wait: configurable delay between processing items\n * - onUpdate/onGetNextItem: callbacks for monitoring queuer state\n *\n * @example\n * ```ts\n * // FIFO queuer\n * const queuer = new Queuer<number>();\n * queuer.addItem(1); // [1]\n * queuer.addItem(2); // [1, 2]\n * queuer.getNextItem(); // returns 1, queuer is [2]\n *\n * // Priority queuer with processing\n * const priorityQueue = new Queuer<number>({\n * getPriority: (n) => n, // Higher numbers have priority\n * started: true, // Begin processing immediately\n * wait: 1000, // Wait 1s between items\n * onGetNextItem: (item) => console.log(item)\n * });\n * priorityQueue.addItem(1); // [1]\n * priorityQueue.addItem(3); // [3, 1] - 3 processed first\n * priorityQueue.addItem(2); // [3, 2, 1]\n * ```\n */\nexport class Queuer<TValue> {\n protected options: Required<QueuerOptions<TValue>>\n private items: Array<TValue> = []\n private executionCount = 0\n private onUpdates: Array<(item: TValue) => void> = []\n private running: boolean\n private pendingTick = false\n\n constructor(initialOptions: QueuerOptions<TValue> = defaultOptions) {\n this.options = { ...defaultOptions, ...initialOptions }\n this.running = this.options.started\n\n for (let i = 0; i < this.options.initialItems.length; i++) {\n const item = this.options.initialItems[i]!\n const isLast = i === this.options.initialItems.length - 1\n this.addItem(item, this.options.addItemsTo, isLast)\n }\n }\n\n /**\n * Processes items in the queuer\n */\n protected tick() {\n if (!this.running) {\n this.pendingTick = false\n return\n }\n while (!this.isEmpty()) {\n const nextItem = this.getNextItem(this.options.getItemsFrom)\n if (nextItem === undefined) {\n break\n }\n this.onUpdates.forEach((cb) => cb(nextItem))\n\n if (this.options.wait > 0) {\n // Use setTimeout to wait before processing next item\n setTimeout(() => this.tick(), this.options.wait)\n return\n }\n\n this.tick()\n }\n this.pendingTick = false\n }\n\n /**\n * Updates the queuer options\n * Returns the new options state\n */\n setOptions(\n newOptions: Partial<QueuerOptions<TValue>>,\n ): QueuerOptions<TValue> {\n this.options = { ...this.options, ...newOptions }\n return this.options\n }\n\n /**\n * Adds an item to the queuer and starts processing if not already running\n * @returns true if item was added, false if queuer is full\n */\n addItem(\n item: TValue,\n position: QueuePosition = this.options.addItemsTo,\n runOnUpdate: boolean = true,\n ): boolean {\n if (this.isFull()) {\n return false\n }\n\n if (this.options.getPriority !== defaultOptions.getPriority) {\n // If custom priority function is provided, insert based on priority\n const priority = this.options.getPriority(item)\n const insertIndex = this.items.findIndex(\n (existing) => this.options.getPriority(existing) > priority,\n )\n\n if (insertIndex === -1) {\n this.items.push(item)\n } else {\n this.items.splice(insertIndex, 0, item)\n }\n } else {\n // Default FIFO/LIFO behavior\n if (position === 'front') {\n this.items.unshift(item)\n } else {\n this.items.push(item)\n }\n }\n\n if (this.running && !this.pendingTick) {\n this.pendingTick = true\n this.tick()\n }\n if (runOnUpdate) {\n this.options.onUpdate(this)\n }\n return true\n }\n\n /**\n * Removes and returns an item from the queuer using shift (default) or pop\n *\n * @example\n * ```ts\n * // Standard FIFO queuer\n * queuer.getNextItem()\n * // Stack-like behavior (LIFO)\n * queuer.getNextItem('back')\n * ```\n */\n getNextItem(\n position: QueuePosition = this.options.getItemsFrom,\n ): TValue | undefined {\n let item: TValue | undefined\n\n if (position === 'front') {\n item = this.items.shift()\n } else {\n item = this.items.pop()\n }\n\n if (item !== undefined) {\n this.executionCount++\n this.options.onUpdate(this)\n this.options.onGetNextItem(item, this)\n }\n return item\n }\n\n /**\n * Returns an item without removing it\n *\n * @example\n * ```ts\n * // Look at next item to getNextItem\n * queuer.peek()\n * // Look at last item (like stack top)\n * queuer.peek('back')\n * ```\n */\n peek(\n position: QueuePosition = this.options.getItemsFrom,\n ): TValue | undefined {\n if (position === 'front') {\n return this.items[0]\n }\n return this.items[this.items.length - 1]\n }\n\n /**\n * Returns true if the queuer is empty\n */\n isEmpty(): boolean {\n return this.items.length === 0\n }\n\n /**\n * Returns true if the queuer is full\n */\n isFull(): boolean {\n return this.items.length >= this.options.maxSize\n }\n\n /**\n * Returns the current size of the queuer\n */\n size(): number {\n return this.items.length\n }\n\n /**\n * Removes all items from the queuer\n */\n clear(): void {\n this.items = []\n this.options.onUpdate(this)\n }\n\n /**\n * Resets the queuer to its initial state\n */\n reset(withInitialItems?: boolean): void {\n this.clear()\n this.executionCount = 0\n if (withInitialItems) {\n this.items = [...this.options.initialItems]\n }\n this.running = this.options.started\n }\n\n /**\n * Returns a copy of all items in the queuer\n */\n getAllItems(): Array<TValue> {\n return [...this.items]\n }\n\n /**\n * Returns the number of items that have been removed from the queuer\n */\n getExecutionCount(): number {\n return this.executionCount\n }\n\n /**\n * Adds a callback to be called when an item is processed\n */\n onUpdate(cb: (item: TValue) => void) {\n this.onUpdates.push(cb)\n return () => {\n this.onUpdates = this.onUpdates.filter((d) => d !== cb)\n }\n }\n\n /**\n * Stops the queuer from processing items\n */\n stop() {\n this.running = false\n this.pendingTick = false\n this.options.onUpdate(this)\n }\n\n /**\n * Starts the queuer and processes items\n */\n start() {\n this.running = true\n if (!this.pendingTick && !this.isEmpty()) {\n this.pendingTick = true\n this.tick()\n }\n this.options.onUpdate(this)\n }\n\n /**\n * Returns true if the queuer is running\n */\n isRunning() {\n return this.running\n }\n\n /**\n * Returns true if the queuer is running but has no items to process\n */\n isIdle() {\n return this.running && this.isEmpty()\n }\n}\n\n/**\n * Creates a queue that processes items in a queuer immediately upon addition.\n * Items are processed sequentially in FIFO order by default.\n *\n * This is a simplified wrapper around the Queuer class that only exposes the\n * `addItem` method. This queue is always running and will process items as they are added.\n * For more control over queuer processing, use the Queuer class\n * directly which provides methods like `start`, `stop`, `reset`, and more.\n *\n * @example\n * ```ts\n * // Basic sequential processing\n * const processItems = queuer<number>({\n * wait: 1000,\n * onUpdate: (queuer) => console.log(queuer.getAllItems())\n * })\n * processItems(1) // Logs: 1\n * processItems(2) // Logs: 2 after 1 completes\n *\n * // Priority queuer\n * const processPriority = queuer<number>({\n * process: async (n) => console.log(n),\n * getPriority: n => n // Higher numbers processed first\n * })\n * processPriority(1)\n * processPriority(3) // Processed before 1\n * ```\n */\nexport function queue<TValue>(options: QueuerOptions<TValue> = {}) {\n const queuer = new Queuer<TValue>({ ...options, started: true })\n return queuer.addItem.bind(queuer)\n}\n"],"names":[],"mappings":";;AA6CA,MAAM,iBAA+C;AAAA,EACnD,YAAY;AAAA,EACZ,cAAc;AAAA,EACd,aAAa,MAAM;AAAA,EACnB,c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cAAc;AACnB,WAAK,KAAK;AAAA,IAAA;AAEP,SAAA,QAAQ,SAAS,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM5B,YAAY;AACV,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,SAAS;AACA,WAAA,KAAK,WAAW,KAAK,QAAQ;AAAA,EAAA;AAExC;AA8BgB,SAAA,MAAc,UAAiC,IAAI;AAC3D,QAAA,SAAS,IAAI,OAAe,EAAE,GAAG,SAAS,SAAS,MAAM;AACxD,SAAA,OAAO,QAAQ,KAAK,MAAM;AACnC;;;"}
|
|
1
|
+
{"version":3,"file":"queuer.cjs","sources":["../../src/queuer.ts"],"sourcesContent":["/**\n * Options for configuring a Queuer instance\n */\nexport interface QueuerOptions<TValue> {\n /**\n * Default position to add items to the queuer\n * @default 'back'\n */\n addItemsTo?: QueuePosition\n /**\n * Maximum time in milliseconds that an item can stay in the queue\n * If not provided, items will never expire\n */\n expirationDuration?: number\n /**\n * Function to determine if an item has expired\n * If provided, this overrides the expirationDuration behavior\n */\n getIsExpired?: (item: TValue, addedAt: number) => boolean\n /**\n * Default position to get items from during processing\n * @default 'front'\n */\n getItemsFrom?: QueuePosition\n /**\n * Function to determine priority of items in the queuer\n * Higher priority items will be processed first\n */\n getPriority?: (item: TValue) => number\n /**\n * Initial items to populate the queuer with\n */\n initialItems?: Array<TValue>\n /**\n * Maximum number of items allowed in the queuer\n */\n maxSize?: number\n /**\n * Callback fired whenever an item expires in the queuer\n */\n onExpire?: (item: TValue, queuer: Queuer<TValue>) => void\n /**\n * Callback fired whenever an item is removed from the queuer\n */\n onGetNextItem?: (item: TValue, queuer: Queuer<TValue>) => void\n /**\n * Callback fired whenever the queuer's running state changes\n */\n onIsRunningChange?: (queuer: Queuer<TValue>) => void\n /**\n * Callback fired whenever an item is added or removed from the queuer\n */\n onItemsChange?: (queuer: Queuer<TValue>) => void\n /**\n * Callback fired whenever an item is rejected from being added to the queuer\n */\n onReject?: (item: TValue, queuer: Queuer<TValue>) => void\n /**\n * Whether the queuer should start processing tasks immediately\n */\n started?: boolean\n /**\n * Time in milliseconds to wait between processing items\n */\n wait?: number\n}\n\nconst defaultOptions: Required<QueuerOptions<any>> = {\n addItemsTo: 'back',\n getItemsFrom: 'front',\n getPriority: (item) => item?.priority ?? 0,\n getIsExpired: () => false,\n expirationDuration: Infinity,\n initialItems: [],\n maxSize: Infinity,\n onGetNextItem: () => {},\n onIsRunningChange: () => {},\n onItemsChange: () => {},\n onReject: () => {},\n onExpire: () => {},\n started: false,\n wait: 0,\n}\n\n/**\n * Position type for addItem and getNextItem operations\n */\nexport type QueuePosition = 'front' | 'back'\n\n/**\n * A flexible queue data structure that defaults to FIFO (First In First Out) behavior\n * with optional position overrides for stack-like or double-ended operations.\n *\n * The queuer can automatically process items as they are added, with configurable\n * wait times between processing each item. Processing can be started/stopped\n * and the queuer will maintain its state.\n *\n * Supports priority-based ordering when a getPriority function is provided.\n * Items with higher priority values will be processed first.\n *\n * Default queue behavior:\n * - addItem(item): adds to back of queuer\n * - getNextItem(): removes and returns from front of queuer\n *\n * Stack (LIFO) behavior:\n * - addItem(item, 'back'): adds to back\n * - getNextItem('back'): removes and returns from back\n *\n * Double-ended queuer behavior:\n * - addItem(item, position): adds to specified position ('front' or 'back')\n * - getNextItem(position): removes and returns from specified position\n *\n * Processing behavior:\n * - start(): begins processing items in the queuer\n * - stop(): pauses processing\n * - wait: configurable delay between processing items\n * - onItemsChange/onGetNextItem: callbacks for monitoring queuer state\n *\n * Supports item expiration to clear stale items from the queuer\n * - expirationDuration: maximum time in milliseconds that an item can stay in the queue\n * - getIsExpired: function to override default expiration behavior\n * - onExpire: callback for when an item expires\n *\n * @example\n * ```ts\n * // FIFO queuer\n * const queuer = new Queuer<number>();\n * queuer.addItem(1); // [1]\n * queuer.addItem(2); // [1, 2]\n * queuer.getNextItem(); // returns 1, queuer is [2]\n *\n * // Priority queuer with processing\n * const priorityQueue = new Queuer<number>({\n * getPriority: (n) => n, // Higher numbers have priority\n * started: true, // Begin processing immediately\n * wait: 1000, // Wait 1s between items\n * onGetNextItem: (item, queuer) => console.log(item)\n * });\n * priorityQueue.addItem(1); // [1]\n * priorityQueue.addItem(3); // [3, 1] - 3 processed first\n * priorityQueue.addItem(2); // [3, 2, 1]\n * ```\n */\nexport class Queuer<TValue> {\n private _options: Required<QueuerOptions<TValue>>\n private _items: Array<TValue> = []\n private _itemTimestamps: Array<number> = []\n private _executionCount = 0\n private _rejectionCount = 0\n private _expirationCount = 0\n private _onItemsChanges: Array<(item: TValue) => void> = []\n private _running: boolean\n private _pendingTick = false\n\n constructor(initialOptions: QueuerOptions<TValue> = defaultOptions) {\n this._options = { ...defaultOptions, ...initialOptions }\n this._running = this._options.started\n\n for (let i = 0; i < this._options.initialItems.length; i++) {\n const item = this._options.initialItems[i]!\n const isLast = i === this._options.initialItems.length - 1\n this.addItem(item, this._options.addItemsTo, isLast)\n }\n }\n\n /**\n * Updates the queuer options\n * Returns the new options state\n */\n setOptions(newOptions: Partial<QueuerOptions<TValue>>): void {\n this._options = { ...this._options, ...newOptions }\n }\n\n /**\n * Returns the current queuer options\n */\n getOptions(): Required<QueuerOptions<TValue>> {\n return this._options\n }\n\n /**\n * Processes items in the queuer\n */\n private tick() {\n if (!this._running) {\n this._pendingTick = false\n return\n }\n\n // Check for expired items\n this.checkExpiredItems()\n\n while (!this.getIsEmpty()) {\n const nextItem = this.getNextItem(this._options.getItemsFrom)\n if (nextItem === undefined) {\n break\n }\n this._onItemsChanges.forEach((cb) => cb(nextItem))\n\n if (this._options.wait > 0) {\n // Use setTimeout to wait before processing next item\n setTimeout(() => this.tick(), this._options.wait)\n return\n }\n\n this.tick()\n }\n this._pendingTick = false\n }\n\n /**\n * Checks for and removes expired items from the queuer\n */\n private checkExpiredItems() {\n if (\n this._options.expirationDuration === Infinity &&\n this._options.getIsExpired === defaultOptions.getIsExpired\n )\n return\n\n const now = Date.now()\n const expiredIndices: Array<number> = []\n\n // Find indices of expired items\n for (let i = 0; i < this._items.length; i++) {\n const timestamp = this._itemTimestamps[i]\n if (timestamp === undefined) continue\n\n const item = this._items[i]\n if (item === undefined) continue\n\n const isExpired =\n this._options.getIsExpired !== defaultOptions.getIsExpired\n ? this._options.getIsExpired(item, timestamp)\n : now - timestamp > this._options.expirationDuration\n\n if (isExpired) {\n expiredIndices.push(i)\n }\n }\n\n // Remove expired items from back to front to maintain indices\n for (let i = expiredIndices.length - 1; i >= 0; i--) {\n const index = expiredIndices[i]\n if (index === undefined) continue\n\n const expiredItem = this._items[index]\n if (expiredItem === undefined) continue\n\n this._items.splice(index, 1)\n this._itemTimestamps.splice(index, 1)\n this._expirationCount++\n this._options.onExpire(expiredItem, this)\n }\n\n if (expiredIndices.length > 0) {\n this._options.onItemsChange(this)\n }\n }\n\n /**\n * Stops the queuer from processing items\n */\n stop() {\n this._running = false\n this._pendingTick = false\n this._options.onIsRunningChange(this)\n }\n\n /**\n * Starts the queuer and processes items\n */\n start() {\n this._running = true\n if (!this._pendingTick && !this.getIsEmpty()) {\n this._pendingTick = true\n this.tick()\n }\n this._options.onIsRunningChange(this)\n }\n\n /**\n * Removes all items from the queuer\n */\n clear(): void {\n this._items = []\n this._options.onItemsChange(this)\n }\n\n /**\n * Resets the queuer to its initial state\n */\n reset(withInitialItems?: boolean): void {\n this.clear()\n this._executionCount = 0\n if (withInitialItems) {\n this._items = [...this._options.initialItems]\n }\n this._running = this._options.started\n }\n\n /**\n * Adds an item to the queuer and starts processing if not already running\n * @returns true if item was added, false if queuer is full\n */\n addItem(\n item: TValue,\n position: QueuePosition = this._options.addItemsTo,\n runOnUpdate: boolean = true,\n ): boolean {\n if (this.getIsFull()) {\n this._rejectionCount++\n this._options.onReject(item, this)\n return false\n }\n\n if (this._options.getPriority !== defaultOptions.getPriority) {\n // If custom priority function is provided, insert based on priority\n const priority = this._options.getPriority(item)\n const insertIndex = this._items.findIndex(\n (existing) => this._options.getPriority(existing) > priority,\n )\n\n if (insertIndex === -1) {\n this._items.push(item)\n this._itemTimestamps.push(Date.now())\n } else {\n this._items.splice(insertIndex, 0, item)\n this._itemTimestamps.splice(insertIndex, 0, Date.now())\n }\n } else {\n // Default FIFO/LIFO behavior\n if (position === 'front') {\n this._items.unshift(item)\n this._itemTimestamps.unshift(Date.now())\n } else {\n this._items.push(item)\n this._itemTimestamps.push(Date.now())\n }\n }\n\n if (this._running && !this._pendingTick) {\n this._pendingTick = true\n this.tick()\n }\n if (runOnUpdate) {\n this._options.onItemsChange(this)\n }\n return true\n }\n\n /**\n * Removes and returns an item from the queuer using shift (default) or pop\n *\n * @example\n * ```ts\n * // Standard FIFO queuer\n * queuer.getNextItem()\n * // Stack-like behavior (LIFO)\n * queuer.getNextItem('back')\n * ```\n */\n getNextItem(\n position: QueuePosition = this._options.getItemsFrom,\n ): TValue | undefined {\n let item: TValue | undefined\n\n if (position === 'front') {\n item = this._items.shift()\n this._itemTimestamps.shift()\n } else {\n item = this._items.pop()\n this._itemTimestamps.pop()\n }\n\n if (item !== undefined) {\n this._executionCount++\n this._options.onItemsChange(this)\n this._options.onGetNextItem(item, this)\n }\n return item\n }\n\n /**\n * Returns an item without removing it\n *\n * @example\n * ```ts\n * // Look at next item to getNextItem\n * queuer.getPeek()\n * // Look at last item (like stack top)\n * queuer.getPeek('back')\n * ```\n */\n getPeek(\n position: QueuePosition = this._options.getItemsFrom,\n ): TValue | undefined {\n if (position === 'front') {\n return this._items[0]\n }\n return this._items[this._items.length - 1]\n }\n\n /**\n * Returns true if the queuer is empty\n */\n getIsEmpty(): boolean {\n return this._items.length === 0\n }\n\n /**\n * Returns true if the queuer is full\n */\n getIsFull(): boolean {\n return this._items.length >= this._options.maxSize\n }\n\n /**\n * Returns the current size of the queuer\n */\n getSize(): number {\n return this._items.length\n }\n\n /**\n * Returns a copy of all items in the queuer\n */\n getAllItems(): Array<TValue> {\n return [...this._items]\n }\n\n /**\n * Returns the number of items that have been removed from the queuer\n */\n getExecutionCount(): number {\n return this._executionCount\n }\n\n /**\n * Returns the number of items that have been rejected from the queuer\n */\n getRejectionCount(): number {\n return this._rejectionCount\n }\n\n /**\n * Returns the number of items that have expired from the queuer\n */\n getExpirationCount(): number {\n return this._expirationCount\n }\n\n /**\n * Returns true if the queuer is running\n */\n getIsRunning() {\n return this._running\n }\n\n /**\n * Returns true if the queuer is running but has no items to process\n */\n getIsIdle() {\n return this._running && this.getIsEmpty()\n }\n}\n\n/**\n * Creates a queue that processes items in a queuer immediately upon addition.\n * Items are processed sequentially in FIFO order by default.\n *\n * This is a simplified wrapper around the Queuer class that only exposes the\n * `addItem` method. This queue is always running and will process items as they are added.\n * For more control over queuer processing, use the Queuer class\n * directly which provides methods like `start`, `stop`, `reset`, and more.\n *\n * @example\n * ```ts\n * // Basic sequential processing\n * const processItems = queuer<number>({\n * wait: 1000,\n * onItemsChange: (queuer) => console.log(queuer.getAllItems())\n * })\n * processItems(1) // Logs: 1\n * processItems(2) // Logs: 2 after 1 completes\n *\n * // Priority queuer\n * const processPriority = queuer<number>({\n * process: async (n) => console.log(n),\n * getPriority: n => n // Higher numbers processed first\n * })\n * processPriority(1)\n * processPriority(3) // Processed before 1\n * ```\n */\nexport function queue<TValue>(options: QueuerOptions<TValue> = {}) {\n const queuer = new Queuer<TValue>({ ...options, started: true })\n return queuer.addItem.bind(queuer)\n}\n"],"names":[],"mappings":";;AAmEA,MAAM,iBAA+C;AAAA,EACnD,YAAY;AAAA,EACZ,cAAc;AAAA,EACd,aAAa,CAAC,UAAS,6BAAM,aAAY;AAAA,EACzC,cAAc,MAAM;AAAA,EACpB,oBAAoB;AAAA,EACpB,cAAc,CAAC;AAAA,EACf,SAAS;AAAA,EACT,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,mBAAmB,MAAM;AAAA,EAAC;AAAA,EAC1B,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,SAAS;AAAA,E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Md,qBAA6B;AAC3B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,eAAe;AACb,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,YAAY;AACH,WAAA,KAAK,YAAY,KAAK,WAAW;AAAA,EAAA;AAE5C;AA8BgB,SAAA,MAAc,UAAiC,IAAI;AAC3D,QAAA,SAAS,IAAI,OAAe,EAAE,GAAG,SAAS,SAAS,MAAM;AACxD,SAAA,OAAO,QAAQ,KAAK,MAAM;AACnC;;;"}
|
package/dist/cjs/queuer.d.cts
CHANGED
|
@@ -7,6 +7,16 @@ export interface QueuerOptions<TValue> {
|
|
|
7
7
|
* @default 'back'
|
|
8
8
|
*/
|
|
9
9
|
addItemsTo?: QueuePosition;
|
|
10
|
+
/**
|
|
11
|
+
* Maximum time in milliseconds that an item can stay in the queue
|
|
12
|
+
* If not provided, items will never expire
|
|
13
|
+
*/
|
|
14
|
+
expirationDuration?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Function to determine if an item has expired
|
|
17
|
+
* If provided, this overrides the expirationDuration behavior
|
|
18
|
+
*/
|
|
19
|
+
getIsExpired?: (item: TValue, addedAt: number) => boolean;
|
|
10
20
|
/**
|
|
11
21
|
* Default position to get items from during processing
|
|
12
22
|
* @default 'front'
|
|
@@ -25,14 +35,26 @@ export interface QueuerOptions<TValue> {
|
|
|
25
35
|
* Maximum number of items allowed in the queuer
|
|
26
36
|
*/
|
|
27
37
|
maxSize?: number;
|
|
38
|
+
/**
|
|
39
|
+
* Callback fired whenever an item expires in the queuer
|
|
40
|
+
*/
|
|
41
|
+
onExpire?: (item: TValue, queuer: Queuer<TValue>) => void;
|
|
28
42
|
/**
|
|
29
43
|
* Callback fired whenever an item is removed from the queuer
|
|
30
44
|
*/
|
|
31
45
|
onGetNextItem?: (item: TValue, queuer: Queuer<TValue>) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Callback fired whenever the queuer's running state changes
|
|
48
|
+
*/
|
|
49
|
+
onIsRunningChange?: (queuer: Queuer<TValue>) => void;
|
|
32
50
|
/**
|
|
33
51
|
* Callback fired whenever an item is added or removed from the queuer
|
|
34
52
|
*/
|
|
35
|
-
|
|
53
|
+
onItemsChange?: (queuer: Queuer<TValue>) => void;
|
|
54
|
+
/**
|
|
55
|
+
* Callback fired whenever an item is rejected from being added to the queuer
|
|
56
|
+
*/
|
|
57
|
+
onReject?: (item: TValue, queuer: Queuer<TValue>) => void;
|
|
36
58
|
/**
|
|
37
59
|
* Whether the queuer should start processing tasks immediately
|
|
38
60
|
*/
|
|
@@ -73,7 +95,12 @@ export type QueuePosition = 'front' | 'back';
|
|
|
73
95
|
* - start(): begins processing items in the queuer
|
|
74
96
|
* - stop(): pauses processing
|
|
75
97
|
* - wait: configurable delay between processing items
|
|
76
|
-
* -
|
|
98
|
+
* - onItemsChange/onGetNextItem: callbacks for monitoring queuer state
|
|
99
|
+
*
|
|
100
|
+
* Supports item expiration to clear stale items from the queuer
|
|
101
|
+
* - expirationDuration: maximum time in milliseconds that an item can stay in the queue
|
|
102
|
+
* - getIsExpired: function to override default expiration behavior
|
|
103
|
+
* - onExpire: callback for when an item expires
|
|
77
104
|
*
|
|
78
105
|
* @example
|
|
79
106
|
* ```ts
|
|
@@ -88,7 +115,7 @@ export type QueuePosition = 'front' | 'back';
|
|
|
88
115
|
* getPriority: (n) => n, // Higher numbers have priority
|
|
89
116
|
* started: true, // Begin processing immediately
|
|
90
117
|
* wait: 1000, // Wait 1s between items
|
|
91
|
-
* onGetNextItem: (item) => console.log(item)
|
|
118
|
+
* onGetNextItem: (item, queuer) => console.log(item)
|
|
92
119
|
* });
|
|
93
120
|
* priorityQueue.addItem(1); // [1]
|
|
94
121
|
* priorityQueue.addItem(3); // [3, 1] - 3 processed first
|
|
@@ -96,22 +123,49 @@ export type QueuePosition = 'front' | 'back';
|
|
|
96
123
|
* ```
|
|
97
124
|
*/
|
|
98
125
|
export declare class Queuer<TValue> {
|
|
99
|
-
|
|
100
|
-
private
|
|
101
|
-
private
|
|
102
|
-
private
|
|
103
|
-
private
|
|
104
|
-
private
|
|
126
|
+
private _options;
|
|
127
|
+
private _items;
|
|
128
|
+
private _itemTimestamps;
|
|
129
|
+
private _executionCount;
|
|
130
|
+
private _rejectionCount;
|
|
131
|
+
private _expirationCount;
|
|
132
|
+
private _onItemsChanges;
|
|
133
|
+
private _running;
|
|
134
|
+
private _pendingTick;
|
|
105
135
|
constructor(initialOptions?: QueuerOptions<TValue>);
|
|
136
|
+
/**
|
|
137
|
+
* Updates the queuer options
|
|
138
|
+
* Returns the new options state
|
|
139
|
+
*/
|
|
140
|
+
setOptions(newOptions: Partial<QueuerOptions<TValue>>): void;
|
|
141
|
+
/**
|
|
142
|
+
* Returns the current queuer options
|
|
143
|
+
*/
|
|
144
|
+
getOptions(): Required<QueuerOptions<TValue>>;
|
|
106
145
|
/**
|
|
107
146
|
* Processes items in the queuer
|
|
108
147
|
*/
|
|
109
|
-
|
|
148
|
+
private tick;
|
|
110
149
|
/**
|
|
111
|
-
*
|
|
112
|
-
* Returns the new options state
|
|
150
|
+
* Checks for and removes expired items from the queuer
|
|
113
151
|
*/
|
|
114
|
-
|
|
152
|
+
private checkExpiredItems;
|
|
153
|
+
/**
|
|
154
|
+
* Stops the queuer from processing items
|
|
155
|
+
*/
|
|
156
|
+
stop(): void;
|
|
157
|
+
/**
|
|
158
|
+
* Starts the queuer and processes items
|
|
159
|
+
*/
|
|
160
|
+
start(): void;
|
|
161
|
+
/**
|
|
162
|
+
* Removes all items from the queuer
|
|
163
|
+
*/
|
|
164
|
+
clear(): void;
|
|
165
|
+
/**
|
|
166
|
+
* Resets the queuer to its initial state
|
|
167
|
+
*/
|
|
168
|
+
reset(withInitialItems?: boolean): void;
|
|
115
169
|
/**
|
|
116
170
|
* Adds an item to the queuer and starts processing if not already running
|
|
117
171
|
* @returns true if item was added, false if queuer is full
|
|
@@ -135,32 +189,24 @@ export declare class Queuer<TValue> {
|
|
|
135
189
|
* @example
|
|
136
190
|
* ```ts
|
|
137
191
|
* // Look at next item to getNextItem
|
|
138
|
-
* queuer.
|
|
192
|
+
* queuer.getPeek()
|
|
139
193
|
* // Look at last item (like stack top)
|
|
140
|
-
* queuer.
|
|
194
|
+
* queuer.getPeek('back')
|
|
141
195
|
* ```
|
|
142
196
|
*/
|
|
143
|
-
|
|
197
|
+
getPeek(position?: QueuePosition): TValue | undefined;
|
|
144
198
|
/**
|
|
145
199
|
* Returns true if the queuer is empty
|
|
146
200
|
*/
|
|
147
|
-
|
|
201
|
+
getIsEmpty(): boolean;
|
|
148
202
|
/**
|
|
149
203
|
* Returns true if the queuer is full
|
|
150
204
|
*/
|
|
151
|
-
|
|
205
|
+
getIsFull(): boolean;
|
|
152
206
|
/**
|
|
153
207
|
* Returns the current size of the queuer
|
|
154
208
|
*/
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Removes all items from the queuer
|
|
158
|
-
*/
|
|
159
|
-
clear(): void;
|
|
160
|
-
/**
|
|
161
|
-
* Resets the queuer to its initial state
|
|
162
|
-
*/
|
|
163
|
-
reset(withInitialItems?: boolean): void;
|
|
209
|
+
getSize(): number;
|
|
164
210
|
/**
|
|
165
211
|
* Returns a copy of all items in the queuer
|
|
166
212
|
*/
|
|
@@ -170,25 +216,21 @@ export declare class Queuer<TValue> {
|
|
|
170
216
|
*/
|
|
171
217
|
getExecutionCount(): number;
|
|
172
218
|
/**
|
|
173
|
-
*
|
|
219
|
+
* Returns the number of items that have been rejected from the queuer
|
|
174
220
|
*/
|
|
175
|
-
|
|
221
|
+
getRejectionCount(): number;
|
|
176
222
|
/**
|
|
177
|
-
*
|
|
178
|
-
*/
|
|
179
|
-
stop(): void;
|
|
180
|
-
/**
|
|
181
|
-
* Starts the queuer and processes items
|
|
223
|
+
* Returns the number of items that have expired from the queuer
|
|
182
224
|
*/
|
|
183
|
-
|
|
225
|
+
getExpirationCount(): number;
|
|
184
226
|
/**
|
|
185
227
|
* Returns true if the queuer is running
|
|
186
228
|
*/
|
|
187
|
-
|
|
229
|
+
getIsRunning(): boolean;
|
|
188
230
|
/**
|
|
189
231
|
* Returns true if the queuer is running but has no items to process
|
|
190
232
|
*/
|
|
191
|
-
|
|
233
|
+
getIsIdle(): boolean;
|
|
192
234
|
}
|
|
193
235
|
/**
|
|
194
236
|
* Creates a queue that processes items in a queuer immediately upon addition.
|
|
@@ -204,7 +246,7 @@ export declare class Queuer<TValue> {
|
|
|
204
246
|
* // Basic sequential processing
|
|
205
247
|
* const processItems = queuer<number>({
|
|
206
248
|
* wait: 1000,
|
|
207
|
-
*
|
|
249
|
+
* onItemsChange: (queuer) => console.log(queuer.getAllItems())
|
|
208
250
|
* })
|
|
209
251
|
* processItems(1) // Logs: 1
|
|
210
252
|
* processItems(2) // Logs: 2 after 1 completes
|