rafa
Version:
Rafa.js is a Javascript framework for building concurrent applications.
32 lines (29 loc) • 1.12 kB
JavaScript
// # constructor(generator: () => A, stopValue: A): UntilEnumerator
// An Enumerator that calls a generator function for each value and assumes the
// generator is done producing values when the value returned equals stopValue.
// The default for stopValue is undefined (essentially call this without
// providing a stopValue argument).
function UntilEnumerator(generator, stopValue) {
this.generate = generator;
this.stop = stopValue;
}
inherit(UntilEnumerator, Enumerator, {
// # next(callback: A => _)
// Execute a callback function, passing the next value wrapped in a Message.
// The next value is created by calling the generate method. If the value
// returned from the generate method is equal to the stop property, then
// a DoneMessage is sent to the callback.
next(callback) {
if (!this.generate) throw this.CompleteError;
const a = this.generate();
let M = this.Message;
if (a === this.stop) {
this.generate = null;
this.stop = null;
M = this.DoneMessage;
}
else if (a instanceof Error)
M = this.ErrorMessage;
callback(new M(a));
}
});