icetea-queue 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/README.md +145 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +66 -0
- package/dist/index.umd.cjs +1 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# icetea-queue
|
|
2
|
+
|
|
3
|
+
A simple, lightweight TypeScript queue implementation for processing items concurrently across multiple slots. Supports both synchronous and asynchronous executors with completion tracking.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install icetea-queue
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Queue } from "icetea-queue";
|
|
15
|
+
|
|
16
|
+
// Create a queue with 3 parallel slots
|
|
17
|
+
const queue = new Queue<number>(3);
|
|
18
|
+
|
|
19
|
+
// Set an executor function (called for each item)
|
|
20
|
+
queue.setExecutor(async (item, allData) => {
|
|
21
|
+
console.log(`Processing: ${item}`);
|
|
22
|
+
return true; // return truthy to continue to next item
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Provide data to process
|
|
26
|
+
queue.setData([1, 2, 3, 4, 5, 6]);
|
|
27
|
+
|
|
28
|
+
// Handle completion
|
|
29
|
+
queue.onCompleted((result) => {
|
|
30
|
+
console.log("All done!", result);
|
|
31
|
+
// { status: true, completed: 6 }
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Start processing
|
|
35
|
+
queue.execute();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API
|
|
39
|
+
|
|
40
|
+
### `Queue<T>(roomSize: number)`
|
|
41
|
+
|
|
42
|
+
Creates a new queue with the specified number of parallel slots.
|
|
43
|
+
|
|
44
|
+
| Parameter | Type | Description |
|
|
45
|
+
| ---------- | -------- | ----------------------------------------- |
|
|
46
|
+
| `roomSize` | `number` | Number of parallel processing slots/rooms |
|
|
47
|
+
|
|
48
|
+
### Methods
|
|
49
|
+
|
|
50
|
+
#### `setData(data: T[]): this`
|
|
51
|
+
|
|
52
|
+
Sets the data to be processed and distributes it across the available slots in a round-robin fashion. Returns `this` for chaining.
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
queue.setData([1, 2, 3, 4, 5, 6]);
|
|
56
|
+
// Slot distribution with roomSize=3:
|
|
57
|
+
// Slot 0: [1, 4]
|
|
58
|
+
// Slot 1: [2, 5]
|
|
59
|
+
// Slot 2: [3, 6]
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### `setExecutor(executor: Function): void`
|
|
63
|
+
|
|
64
|
+
Sets the callback function that processes each item. The executor receives the current item and the full dataset.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// Sync executor
|
|
68
|
+
queue.setExecutor((item, allData) => {
|
|
69
|
+
// process item
|
|
70
|
+
return true; // truthy → proceed to next item, falsy → stop
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Async executor
|
|
74
|
+
queue.setExecutor(async (item, allData) => {
|
|
75
|
+
await someAsyncWork(item);
|
|
76
|
+
return true;
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- **Throws** `Error` if executor is not a function.
|
|
81
|
+
|
|
82
|
+
#### `execute(): void`
|
|
83
|
+
|
|
84
|
+
Starts processing all items across the queue slots. The executor must be set before calling this method.
|
|
85
|
+
|
|
86
|
+
- **Throws** `Error` if no executor has been set.
|
|
87
|
+
- Does nothing if data is empty.
|
|
88
|
+
|
|
89
|
+
#### `onCompleted(callback: Function): void`
|
|
90
|
+
|
|
91
|
+
Registers a callback that fires when all items have been processed.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
queue.onCompleted((result) => {
|
|
95
|
+
console.log(result.status); // true
|
|
96
|
+
console.log(result.completed); // total items processed
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
- **Throws** `Error` if callback is not a function.
|
|
101
|
+
|
|
102
|
+
#### `isPromise(callback: Function): boolean`
|
|
103
|
+
|
|
104
|
+
Checks whether a given callback is an async function.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
queue.isPromise(async () => {}); // true
|
|
108
|
+
queue.isPromise(() => {}); // false
|
|
109
|
+
queue.isPromise(null); // false
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Behavior
|
|
113
|
+
|
|
114
|
+
- **Round-robin distribution**: Data is evenly distributed across the available slots.
|
|
115
|
+
- **Sequential within slots**: Each slot processes its items one after another.
|
|
116
|
+
- **Parallel across slots**: All slots process concurrently.
|
|
117
|
+
- **Completion callback**: Fires once every item across all slots has been processed.
|
|
118
|
+
- **Auto-reset**: After all items are processed, internal state resets automatically.
|
|
119
|
+
- **Falsy return stops progression**: If the executor returns a falsy value, that slot stops processing further items.
|
|
120
|
+
|
|
121
|
+
## Example
|
|
122
|
+
|
|
123
|
+
### Processing with completion tracking
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { Queue } from "icetea-queue";
|
|
127
|
+
|
|
128
|
+
const queue = new Queue<string>(2);
|
|
129
|
+
|
|
130
|
+
queue.onCompleted(({ status, completed }) => {
|
|
131
|
+
console.log(`Completed ${completed} items: ${status}`);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
queue.setExecutor(async (item) => {
|
|
135
|
+
const result = await fetch(`https://api.example.com/${item}`);
|
|
136
|
+
return result.ok;
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
queue.setData(["a", "b", "c", "d"]);
|
|
140
|
+
queue.execute();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//#region src/queue.ts
|
|
2
|
+
var e = class {
|
|
3
|
+
data;
|
|
4
|
+
roomSize;
|
|
5
|
+
queue;
|
|
6
|
+
queueIndex;
|
|
7
|
+
temp;
|
|
8
|
+
callback;
|
|
9
|
+
callbackIsPromise;
|
|
10
|
+
completed;
|
|
11
|
+
completedCallback;
|
|
12
|
+
constructor(e) {
|
|
13
|
+
this.data = [], this.roomSize = e, this.temp = Array(this.roomSize).fill([]), this.queue = Array(this.roomSize).fill(null), this.queueIndex = this.queue.map((e) => 0), this.callback = null, this.callbackIsPromise = !1, this.completed = 0, this.completedCallback = null;
|
|
14
|
+
}
|
|
15
|
+
setData(e = []) {
|
|
16
|
+
this.data = e;
|
|
17
|
+
let t = Array.from(this.data), n = 0;
|
|
18
|
+
for (; t.length !== 0 && (n === this.temp.length && (n = 0), t.length);) this.#t(n, t.splice(0, 1)[0]), n++;
|
|
19
|
+
return this.queue = this.queue.map((e, t) => this.temp[t][0]), this;
|
|
20
|
+
}
|
|
21
|
+
#e() {
|
|
22
|
+
this.data = [], this.temp = Array(this.roomSize).fill([]), this.queue = Array(this.roomSize).fill(null), this.queueIndex = this.queue.map((e) => 0), this.completed = 0;
|
|
23
|
+
}
|
|
24
|
+
#t(e, t) {
|
|
25
|
+
this.temp = this.temp.map((n, r) => r === e ? [...n, t] : n);
|
|
26
|
+
}
|
|
27
|
+
setCompleted() {
|
|
28
|
+
this.completed += 1, this.completedCallback && this.completed === this.data.length && this.completedCallback({
|
|
29
|
+
status: !0,
|
|
30
|
+
completed: this.completed
|
|
31
|
+
}), this.completed === this.data.length && this.#e();
|
|
32
|
+
}
|
|
33
|
+
isPromise(e) {
|
|
34
|
+
return e ? e.constructor.name === "AsyncFunction" : !1;
|
|
35
|
+
}
|
|
36
|
+
#n(e, t) {
|
|
37
|
+
let n = this.temp?.[e]?.[t];
|
|
38
|
+
if (!n) return;
|
|
39
|
+
this.queueIndex[e] += 1;
|
|
40
|
+
let r = this.temp[e]?.[this.queueIndex[e]];
|
|
41
|
+
if (this.callbackIsPromise) return this.callback(n, this.data).then((t) => {
|
|
42
|
+
t && (this.setCompleted(), this.#n(e, this.queueIndex[e]));
|
|
43
|
+
});
|
|
44
|
+
this.callback(n, this.data), this.setCompleted(), r && this.#n(e, this.queueIndex[e]);
|
|
45
|
+
}
|
|
46
|
+
setExecutor(e) {
|
|
47
|
+
if (!e || typeof e != "function") throw Error("executor is not a function");
|
|
48
|
+
this.callback = e, this.callbackIsPromise = this.isPromise(this.callback);
|
|
49
|
+
}
|
|
50
|
+
onCompleted(e) {
|
|
51
|
+
if (!e || typeof e != "function") throw Error("On Compeleted is not a function");
|
|
52
|
+
this.completedCallback = e;
|
|
53
|
+
}
|
|
54
|
+
execute() {
|
|
55
|
+
if (!this.callback) throw Error("callback is not set");
|
|
56
|
+
this.data.length !== 0 && this.queue.map((e, t) => {
|
|
57
|
+
let n = e, r = ++this.queueIndex[t], i;
|
|
58
|
+
if (this.callbackIsPromise) return this.callback(n, this.data).then((e) => {
|
|
59
|
+
e && (this.setCompleted(), this.#n(t, r));
|
|
60
|
+
});
|
|
61
|
+
i = this.callback(n, this.data), this.setCompleted(), i && this.#n(t, r);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
//#endregion
|
|
66
|
+
export { e as Queue };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e[`IceTea Queue`]={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`}),e.Queue=class{data;roomSize;queue;queueIndex;temp;callback;callbackIsPromise;completed;completedCallback;constructor(e){this.data=[],this.roomSize=e,this.temp=Array(this.roomSize).fill([]),this.queue=Array(this.roomSize).fill(null),this.queueIndex=this.queue.map(e=>0),this.callback=null,this.callbackIsPromise=!1,this.completed=0,this.completedCallback=null}setData(e=[]){this.data=e;let t=Array.from(this.data),n=0;for(;t.length!==0&&(n===this.temp.length&&(n=0),t.length);)this.#t(n,t.splice(0,1)[0]),n++;return this.queue=this.queue.map((e,t)=>this.temp[t][0]),this}#e(){this.data=[],this.temp=Array(this.roomSize).fill([]),this.queue=Array(this.roomSize).fill(null),this.queueIndex=this.queue.map(e=>0),this.completed=0}#t(e,t){this.temp=this.temp.map((n,r)=>r===e?[...n,t]:n)}setCompleted(){this.completed+=1,this.completedCallback&&this.completed===this.data.length&&this.completedCallback({status:!0,completed:this.completed}),this.completed===this.data.length&&this.#e()}isPromise(e){return e?e.constructor.name===`AsyncFunction`:!1}#n(e,t){let n=this.temp?.[e]?.[t];if(!n)return;this.queueIndex[e]+=1;let r=this.temp[e]?.[this.queueIndex[e]];if(this.callbackIsPromise)return this.callback(n,this.data).then(t=>{t&&(this.setCompleted(),this.#n(e,this.queueIndex[e]))});this.callback(n,this.data),this.setCompleted(),r&&this.#n(e,this.queueIndex[e])}setExecutor(e){if(!e||typeof e!=`function`)throw Error(`executor is not a function`);this.callback=e,this.callbackIsPromise=this.isPromise(this.callback)}onCompleted(e){if(!e||typeof e!=`function`)throw Error(`On Compeleted is not a function`);this.completedCallback=e}execute(){if(!this.callback)throw Error(`callback is not set`);this.data.length!==0&&this.queue.map((e,t)=>{let n=e,r=++this.queueIndex[t],i;if(this.callbackIsPromise)return this.callback(n,this.data).then(e=>{e&&(this.setCompleted(),this.#n(t,r))});i=this.callback(n,this.data),this.setCompleted(),i&&this.#n(t,r)})}}});
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "icetea-queue",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A simple queue implementation",
|
|
6
|
+
"main": "./dist/index.umd.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.umd.cjs",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"dev": "vite",
|
|
21
|
+
"build": "tsc && vite build",
|
|
22
|
+
"preview": "vite preview",
|
|
23
|
+
"test": "npx vitest run ./src/test/queue.test.ts"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^25.9.3",
|
|
29
|
+
"typescript": "~6.0.2",
|
|
30
|
+
"vite": "^8.0.12",
|
|
31
|
+
"vite-plugin-dts": "^4.5.4"
|
|
32
|
+
}
|
|
33
|
+
}
|