@hardlydifficult/queue 1.1.0 → 1.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 +194 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# @hardlydifficult/queue
|
|
2
|
+
|
|
3
|
+
A high-performance priority queue with O(1) enqueue and dequeue operations, supporting FIFO ordering within priority levels.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @hardlydifficult/queue
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createPriorityQueue } from "@hardlydifficult/queue";
|
|
15
|
+
|
|
16
|
+
const queue = createPriorityQueue<string>();
|
|
17
|
+
|
|
18
|
+
// Enqueue items with priorities
|
|
19
|
+
queue.enqueue("urgent task", "high");
|
|
20
|
+
queue.enqueue("normal task", "medium");
|
|
21
|
+
queue.enqueue("background task", "low");
|
|
22
|
+
|
|
23
|
+
// Dequeue in priority order (high → medium → low)
|
|
24
|
+
console.log(queue.dequeue()?.data); // "urgent task"
|
|
25
|
+
console.log(queue.dequeue()?.data); // "normal task"
|
|
26
|
+
console.log(queue.dequeue()?.data); // "background task"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Core Operations
|
|
30
|
+
|
|
31
|
+
### Enqueue and Dequeue
|
|
32
|
+
|
|
33
|
+
Add items to the queue with a priority level (defaults to `"medium"`). Items are dequeued in priority order, with FIFO ordering within the same priority level.
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
const queue = createPriorityQueue<string>();
|
|
37
|
+
|
|
38
|
+
// Enqueue with explicit priority
|
|
39
|
+
const item1 = queue.enqueue("task A", "high");
|
|
40
|
+
const item2 = queue.enqueue("task B", "high");
|
|
41
|
+
const item3 = queue.enqueue("task C", "medium");
|
|
42
|
+
|
|
43
|
+
// Dequeue returns highest priority first, FIFO within same priority
|
|
44
|
+
queue.dequeue()?.data; // "task A" (high, enqueued first)
|
|
45
|
+
queue.dequeue()?.data; // "task B" (high, enqueued second)
|
|
46
|
+
queue.dequeue()?.data; // "task C" (medium)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Peek
|
|
50
|
+
|
|
51
|
+
Inspect the next item without removing it from the queue.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
const queue = createPriorityQueue<string>();
|
|
55
|
+
queue.enqueue("first", "high");
|
|
56
|
+
queue.enqueue("second", "low");
|
|
57
|
+
|
|
58
|
+
queue.peek()?.data; // "first" (highest priority)
|
|
59
|
+
queue.size; // Still 2
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Queue Status
|
|
63
|
+
|
|
64
|
+
Check the number of items and whether the queue is empty.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
const queue = createPriorityQueue<string>();
|
|
68
|
+
|
|
69
|
+
queue.isEmpty; // true
|
|
70
|
+
queue.size; // 0
|
|
71
|
+
|
|
72
|
+
queue.enqueue("item");
|
|
73
|
+
queue.isEmpty; // false
|
|
74
|
+
queue.size; // 1
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Item Management
|
|
78
|
+
|
|
79
|
+
### Remove Items
|
|
80
|
+
|
|
81
|
+
Remove a specific item by its ID. Returns `true` if the item was found and removed.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
const queue = createPriorityQueue<string>();
|
|
85
|
+
const item = queue.enqueue("task", "high");
|
|
86
|
+
|
|
87
|
+
queue.remove(item.id); // true
|
|
88
|
+
queue.size; // 0
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Update Priority
|
|
92
|
+
|
|
93
|
+
Change an item's priority level while keeping its data and enqueue timestamp intact.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
const queue = createPriorityQueue<string>();
|
|
97
|
+
const item = queue.enqueue("task", "low");
|
|
98
|
+
|
|
99
|
+
queue.updatePriority(item.id, "high"); // true
|
|
100
|
+
queue.peek()?.data; // "task" (now highest priority)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Reorder Within Priority
|
|
104
|
+
|
|
105
|
+
Move an item before another item in the same priority bucket, or move it to the end of its bucket.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const queue = createPriorityQueue<string>();
|
|
109
|
+
const a = queue.enqueue("A", "high");
|
|
110
|
+
const b = queue.enqueue("B", "high");
|
|
111
|
+
const c = queue.enqueue("C", "high");
|
|
112
|
+
|
|
113
|
+
// Move A before C
|
|
114
|
+
queue.moveBefore(a.id, c.id); // true
|
|
115
|
+
queue.toArray().map(i => i.data); // ["B", "A", "C"]
|
|
116
|
+
|
|
117
|
+
// Move B to the end
|
|
118
|
+
queue.moveToEnd(b.id); // true
|
|
119
|
+
queue.toArray().map(i => i.data); // ["A", "C", "B"]
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Snapshots and Listeners
|
|
123
|
+
|
|
124
|
+
### Get All Items
|
|
125
|
+
|
|
126
|
+
Retrieve all items in dequeue order without modifying the queue.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
const queue = createPriorityQueue<string>();
|
|
130
|
+
queue.enqueue("low", "low");
|
|
131
|
+
queue.enqueue("high", "high");
|
|
132
|
+
queue.enqueue("medium", "medium");
|
|
133
|
+
|
|
134
|
+
const items = queue.toArray();
|
|
135
|
+
items.map(i => i.data); // ["high", "medium", "low"]
|
|
136
|
+
queue.size; // Still 3
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Listen for Enqueues
|
|
140
|
+
|
|
141
|
+
Register a callback that fires whenever an item is enqueued. Returns an unsubscribe function.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
const queue = createPriorityQueue<string>();
|
|
145
|
+
|
|
146
|
+
const unsubscribe = queue.onEnqueue((item) => {
|
|
147
|
+
console.log(`Enqueued: ${item.data} (priority: ${item.priority})`);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
queue.enqueue("task", "high");
|
|
151
|
+
// Logs: "Enqueued: task (priority: high)"
|
|
152
|
+
|
|
153
|
+
unsubscribe();
|
|
154
|
+
queue.enqueue("another"); // No log
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Clear the Queue
|
|
158
|
+
|
|
159
|
+
Remove all items from the queue.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const queue = createPriorityQueue<string>();
|
|
163
|
+
queue.enqueue("a");
|
|
164
|
+
queue.enqueue("b");
|
|
165
|
+
|
|
166
|
+
queue.clear();
|
|
167
|
+
queue.isEmpty; // true
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Item Metadata
|
|
171
|
+
|
|
172
|
+
Each enqueued item includes metadata accessible via the returned `QueueItem` object:
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
const queue = createPriorityQueue<string>();
|
|
176
|
+
const item = queue.enqueue("my task", "high");
|
|
177
|
+
|
|
178
|
+
item.data; // "my task"
|
|
179
|
+
item.priority; // "high"
|
|
180
|
+
item.id; // "q_1" (unique identifier)
|
|
181
|
+
item.enqueuedAt; // 1699564800000 (epoch milliseconds)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Priority Levels
|
|
185
|
+
|
|
186
|
+
The queue supports three priority levels, dequeued in this order:
|
|
187
|
+
|
|
188
|
+
| Priority | Dequeue Order |
|
|
189
|
+
|----------|---------------|
|
|
190
|
+
| `"high"` | 1st |
|
|
191
|
+
| `"medium"` | 2nd |
|
|
192
|
+
| `"low"` | 3rd |
|
|
193
|
+
|
|
194
|
+
When no priority is specified, `"medium"` is used as the default.
|