|
|
|
@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An async collection / item stream.
|
|
|
|
|
* @param {Array} from Optional array to begin reading from.
|
|
|
|
|
*/
|
|
|
|
|
function Stage(from) {
|
|
|
|
|
var base = this;
|
|
|
|
|
this.array = from || [];
|
|
|
|
@ -20,6 +25,10 @@ function Stage(from) {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Take one item off then end. Function will yield if none available to produce yet.
|
|
|
|
|
* @return {} undefined if there is no more items to produce (`commit` was called), otherwise the next item in the stream.
|
|
|
|
|
*/
|
|
|
|
|
this.take = async function() {
|
|
|
|
|
if(base.over && base.array.length<=0)
|
|
|
|
|
return undefined;
|
|
|
|
@ -32,28 +41,39 @@ function Stage(from) {
|
|
|
|
|
return undefined;
|
|
|
|
|
|
|
|
|
|
return base.array.shift();
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
Get all current items not yet produced.
|
|
|
|
|
*/
|
|
|
|
|
this.poll = function() {
|
|
|
|
|
return base.array;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Take without blocking. Returns `null` if there are no items to take.
|
|
|
|
|
*/
|
|
|
|
|
this.takeNB = function() {
|
|
|
|
|
if(base.array.length<=0)
|
|
|
|
|
return null;
|
|
|
|
|
return base.array.shift();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Await and take the rest of the items in this stream until `commit` is called.
|
|
|
|
|
*/
|
|
|
|
|
this.swallow0 = async function() {
|
|
|
|
|
var ar = [];
|
|
|
|
|
var token;
|
|
|
|
|
while(token = await base.take()) {
|
|
|
|
|
while((token = await base.take())) {
|
|
|
|
|
ar.push(token);
|
|
|
|
|
}
|
|
|
|
|
return ar;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a promise that resolves to `swallow0`. Optionally takes a timeout that will reject the promise if it does not complete before the stream is closed with `commit`.
|
|
|
|
|
*/
|
|
|
|
|
this.swallow = function(timeout) {
|
|
|
|
|
if(!timeout) return base.swallow0();
|
|
|
|
|
else {
|
|
|
|
@ -74,11 +94,17 @@ function Stage(from) {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a collection of values to the stream.
|
|
|
|
|
*/
|
|
|
|
|
this.giveMany= function(values) {
|
|
|
|
|
for(let value of values)
|
|
|
|
|
base.give(value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a single value to the stream
|
|
|
|
|
*/
|
|
|
|
|
this.give = function(value) {
|
|
|
|
|
if(!value && !base.acceptInvalid)
|
|
|
|
|
return;
|
|
|
|
@ -86,6 +112,9 @@ function Stage(from) {
|
|
|
|
|
next.signal();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close the stream. After this is called, once all elements already in the stream have been consumed, `take` will resolve immediately to `undefined` for any more consumers. It cannot be opened again.
|
|
|
|
|
*/
|
|
|
|
|
this.commit = function() {
|
|
|
|
|
base.over = true;
|
|
|
|
|
next.flush();
|
|
|
|
@ -94,4 +123,5 @@ function Stage(from) {
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if(module)
|
|
|
|
|
module.exports = Stage;
|
|
|
|
|