rafa
Version:
Rafa.js is a Javascript framework for building concurrent applications.
33 lines (26 loc) • 861 B
JavaScript
// # constructor(Array): ArrayEnumerator
// An Enumerator that calls a callback for each item in the provided Array.
function ArrayEnumerator(array) {
let len = array && array.length;
if (!len) return new EmptyEnumerator();
this.array = array;
this.idx = 0;
this.len = len;
}
inherit(ArrayEnumerator, Enumerator, {
// # next(callback: Message => _)
// Execute a callback function, passing the next value wrapped in a Message.
// If the value is the last item in the array, then return the value
// wrapped in a DoneMessage.
next(callback) {
let { idx, len } = this;
let M = this.Message;
if (idx === len) throw this.CompleteError;
let item = this.array[idx++];
if (idx === len) M = this.DoneMessage;
this.idx = idx;
if (item instanceof Error)
M = this.ErrorMessage;
callback(new M(item));
}
});