@borewit/async-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/LICENSE.txt +9 -0
- package/README.md +87 -0
- package/lib/queue.d.ts +6 -0
- package/lib/queue.js +29 -0
- package/package.json +49 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 Borewit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
[](https://github.com/Borewit/await-queue/actions/workflows/ci.yml)
|
|
2
|
+
|
|
3
|
+
# @borewit/async-queue
|
|
4
|
+
|
|
5
|
+
`await-queue` is a lightweight, generic, and type-safe asynchronous queue implemented in TypeScript. It allows you to enqueue items and retrieve them one at a time using promises, making it ideal for processing events, messages, or tasks in a first-in-first-out (FIFO) order. This is especially handy when reading asynchronous items received via events.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Generic & Type-Safe:** Write once and use with any type (e.g., string, number, object).
|
|
10
|
+
- **FIFO Order:** Processes items in the order they were received.
|
|
11
|
+
- **Promise-Based:** Retrieve items asynchronously using promises.
|
|
12
|
+
- **Event Integration:** Perfect for handling asynchronous events like user actions, network messages, or other event-driven data.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install `@borewit/async-queue` from npm:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @borewit/async-queue
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
Below is an example of how to add and read string messages to/from the queue. This example simulates asynchronous events (using setTimeout) that push string messages into the queue:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { AsyncQueue } from '@borewit/async-queue';
|
|
27
|
+
|
|
28
|
+
// Create a new queue for string messages
|
|
29
|
+
const queue = new AsyncQueue<string>();
|
|
30
|
+
|
|
31
|
+
// Example: Simulate incoming messages via asynchronous events
|
|
32
|
+
function simulateIncomingMessages() {
|
|
33
|
+
setTimeout(() => queue.push("Hello"), 1000);
|
|
34
|
+
setTimeout(() => queue.push("World"), 2000);
|
|
35
|
+
setTimeout(() => queue.push("From AsyncQueue!"), 3000);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
simulateIncomingMessages();
|
|
39
|
+
|
|
40
|
+
// Process messages asynchronously, one at a time
|
|
41
|
+
async function processMessages() {
|
|
42
|
+
while (true) {
|
|
43
|
+
const message = await queue.next();
|
|
44
|
+
console.log("Received message:", message);
|
|
45
|
+
// Add additional processing logic here...
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
processMessages();
|
|
50
|
+
```
|
|
51
|
+
In this example, the queue is used to collect string messages that are simulated to arrive at different times. The asynchronous loop in processMessages waits for each message, ensuring they are handled in FIFO order.
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
push(item: T): void
|
|
58
|
+
```
|
|
59
|
+
Adds an item to the queue.
|
|
60
|
+
|
|
61
|
+
- Parameters
|
|
62
|
+
- `item: T` - The item to add.
|
|
63
|
+
|
|
64
|
+
If there are pending consumers waiting for an item (via `next(`)),
|
|
65
|
+
the promise is immediately resolved with the new item.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
next(): Promise<T>
|
|
69
|
+
```
|
|
70
|
+
Returns a promise that resolves with the next item in the queue.
|
|
71
|
+
- Returns:
|
|
72
|
+
- `Promise<T>` - A promise that resolves to the next item.
|
|
73
|
+
|
|
74
|
+
If the queue already contains an item, the promise resolves immediately;
|
|
75
|
+
otherwise, it waits until an item is pushed.
|
|
76
|
+
|
|
77
|
+
## When to Use This Queue
|
|
78
|
+
|
|
79
|
+
This queue is particularly useful when you need to process asynchronous events in a controlled, sequential manner. Instead of handling events directly as they arrive, you can push them into the queue and then process them one by one. This approach is beneficial in scenarios such as:
|
|
80
|
+
|
|
81
|
+
* Handling user input events.
|
|
82
|
+
* Processing network messages.
|
|
83
|
+
* Managing asynchronous tasks or jobs in order.
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
This project is licensed under the MIT License. See the [LICENSE file](LICENSE.txt) for details.
|
package/lib/queue.d.ts
ADDED
package/lib/queue.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class AsyncQueue {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.queue = []; // Holds messages that have arrived
|
|
4
|
+
this.waiters = []; // Holds resolve functions for pending promises
|
|
5
|
+
}
|
|
6
|
+
// Called when a new message arrives (e.g., from an event)
|
|
7
|
+
push(item) {
|
|
8
|
+
// If someone is waiting for a message, resolve their promise immediately.
|
|
9
|
+
if (this.waiters.length > 0) {
|
|
10
|
+
const resolve = this.waiters.shift();
|
|
11
|
+
resolve(item);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
// Otherwise, add the message to the queue.
|
|
15
|
+
this.queue.push(item);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// Returns a promise that resolves with the next message.
|
|
19
|
+
next() {
|
|
20
|
+
// If there is already a message waiting, return it immediately.
|
|
21
|
+
if (this.queue.length > 0) {
|
|
22
|
+
return Promise.resolve(this.queue.shift());
|
|
23
|
+
}
|
|
24
|
+
// Otherwise, return a promise that will resolve when a new message arrives.
|
|
25
|
+
return new Promise(resolve => {
|
|
26
|
+
this.waiters.push(resolve);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@borewit/async-queue",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "async iterable queue",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"types": "./lib/queue/d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/queue.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'test/**/*.js' 'test/**/*.js.map' 'test/**/*.d.ts'",
|
|
12
|
+
"compile": "tsc -p lib && tsc -p test",
|
|
13
|
+
"build": "yarn run clean && yarn run compile",
|
|
14
|
+
"test": "mocha"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"lib/**/*.js",
|
|
18
|
+
"lib/**/*.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Borewit",
|
|
22
|
+
"url": "https://github.com/Borewit"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": "https://github.com/Borewit/await-queue",
|
|
26
|
+
"packageManager": "yarn@4.6.0",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/chai": "^5.0.1",
|
|
29
|
+
"@types/mocha": "^10.0.10",
|
|
30
|
+
"chai": "^5.1.2",
|
|
31
|
+
"del-cli": "^6.0.0",
|
|
32
|
+
"mocha": "^11.1.0",
|
|
33
|
+
"ts-node": "^10.9.2",
|
|
34
|
+
"typescript": "^5.7.3"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"async",
|
|
38
|
+
"queue",
|
|
39
|
+
"await",
|
|
40
|
+
"iterable",
|
|
41
|
+
"FIFO",
|
|
42
|
+
"event-driven",
|
|
43
|
+
"asynchronous",
|
|
44
|
+
"TypeScript",
|
|
45
|
+
"event-queue",
|
|
46
|
+
"task-queue",
|
|
47
|
+
"message-queue"
|
|
48
|
+
]
|
|
49
|
+
}
|