-
Notifications
You must be signed in to change notification settings - Fork 0
/
spique.js
295 lines (251 loc) · 9.94 KB
/
spique.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* ISC License
*
* Copyright (c) 2016-2020, Erayd LTD
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
"use strict";
const EventEmitter = require("events").EventEmitter;
const RingBuffer = require("./ringbuffer.js");
const GeneratorFunction = Object.getPrototypeOf(function*() {});
module.exports = class Spique extends EventEmitter {
constructor(size = 0, ringSize = 1024) {
super();
var headRing = new RingBuffer(ringSize);
var tailRing = headRing;
var rings = 1;
var items = 0;
var closed = false;
var transforms = [];
// basic properties
Object.defineProperties(this, {
// get the size of the queue
size: { value: size, writable: false, enumerable: true },
// get the current number of items in the queue
length: { get: () => items, enumerable: true },
// get the current number of free slots in the queue
free: {
get: () => (size ? size - items : Number.MAX_SAFE_INTEGER - items),
enumerable: true
},
// get the current closed status of the queue
closed: { get: () => closed && !items, enumerable: true },
// get the ring size
ringSize: { value: ringSize, writable: false, enumerable: true },
// methods
enqueue: { value: enqueue, writable: false },
enqueueHead: { value: enqueueHead, writable: false },
dequeue: { value: dequeue, writable: false },
dequeueTail: { value: dequeueTail, writable: false },
peek: { value: peek, writable: false },
peekTail: { value: peekTail, writable: false },
transform: { value: t => transforms.push(t), writable: false },
close: { value: close, writable: false },
// iterator
[Symbol.iterator]: {
value: function*() {
while (items) yield this.dequeue();
},
writable: false
}
});
// call listener if event state is valid on attachment
this.on("newListener", (ev, listener) => {
if (
(ev === "data" && items) ||
(ev === "empty" && !items) ||
(ev === "full" && !this.free) ||
(ev === "free" && this.free) ||
(ev === "close" && this.closed)
) {
listener(this);
}
});
// attach chained source (iterator | generator | Spique)
async function attachSource(source, forward = true, applyTransforms = true) {
let insert = forward ? "enqueue" : "enqueueHead";
if (source instanceof Spique) {
source.on("data", s => {
this[insert](s[Symbol.iterator](), true, applyTransforms);
});
source.on("close", () => this.close());
return;
} else if (Symbol.iterator in source) source = source[Symbol.iterator]();
if (Symbol.asyncIterator in source) {
for await (let next of source) {
await new Promise(async resolve => {
let feed = target => {
if (target.free) {
target[insert](next, false, applyTransforms);
resolve();
} else this.once("free", feed);
};
feed(this);
});
}
} else {
let feed = target => {
let again = true;
while (target.free) {
let next = source.next();
if (next.done) {
again = false;
break;
} else target[insert](next.value, false, applyTransforms);
}
if (again) this.once("free", feed);
};
feed(this);
}
}
// apply transforms & return a generator instance
function transform(value) {
let result = (function*() {
yield value;
})();
for (let transform of transforms) {
let input = result;
if (Object.getPrototypeOf(transform) === GeneratorFunction) {
result = (function*() {
for (let r of input) {
for (let r2 of transform(r)) yield r2;
}
})();
} else {
result = (function*() {
for (let r of input) {
let reject = false;
r = transform(r, () => reject = true);
if (!reject) yield r;
}
})();
}
}
return result;
}
// close the queue
function close() {
closed = true;
if (!items) this.emit("close", this);
}
// add an item to the tail of the queue
function enqueue(value, isSource = false, applyTransforms = true) {
// attach source
if (isSource) {
attachSource.call(this, value, true, applyTransforms);
return;
}
// apply transforms
if (applyTransforms && transforms.length) {
this.enqueue(transform.call(this, value), true, false);
return;
}
// check queue is open
if (this.closed) throw new Error("Queue is closed");
// check available space
if (!this.free) throw new Error("Queue is full");
// allocate a new ring
if (!tailRing.free) {
let r = tailRing._below || new RingBuffer(ringSize);
r._above = tailRing;
tailRing._below = r;
tailRing = r;
rings++;
}
// enqueue data
tailRing.push(value);
++items;
// fire events
if (!this.free) this.emit("full", this);
if (items === 1) this.emit("data", this);
}
// add an item to the head of the queue
function enqueueHead(value, isSource = false, applyTransforms = true) {
// attach source
if (isSource) {
attachSource.call(this, value, false, applyTransforms);
return;
}
// apply transforms
if (applyTransforms && transforms.length) {
this.enqueueHead(transform.call(this, value), true, false);
return;
}
// check queue is open
if (this.closed) throw new Error("Queue is closed");
// check available space
if (!this.free) throw new Error("Queue is full");
// allocate a new ring
if (!headRing.free) {
let r = headRing._above || new RingBuffer(ringSize);
r._below = headRing;
headRing._above = r;
headRing = r;
rings++;
}
// enqueue data
headRing.unshift(value);
++items;
// fire events
if (!this.free) this.emit("full", this);
if (items === 1) this.emit("data", this);
}
// peek at the value at the head of the queue
function peek() {
if (!items) throw new Error("Queue is empty");
return headRing.peekStart();
}
// peek at the value at the tail of the queue
function peekTail() {
if (!items) throw new Error("Queue is empty");
return tailRing.peek();
}
// remove an item from the head of the queue
function dequeue() {
if (!items) throw new Error("Queue is empty");
let value = headRing.shift();
--items;
// deallocate unused buffer
if (!headRing.length && rings > 1) {
headRing._above = undefined;
headRing = headRing._below;
rings--;
}
// fire events
if (!items) {
this.emit("empty", this);
if (closed) this.emit("close", this);
}
if (this.free && !closed) this.emit("free", this);
return value;
}
// remove an item from the tail of the queue
function dequeueTail() {
if (!items) throw new Error("Queue is empty");
let value = tailRing.pop();
--items;
// deallocate unused buffer
if (!tailRing.length && rings > 1) {
tailRing._below = undefined;
tailRing = tailRing._above;
rings--;
}
// fire events
if (!items) {
this.emit("empty", this);
if (closed) this.emit("close", this);
}
if (this.free && !closed) this.emit("free", this);
return value;
}
}
};