UNPKG

rafa

Version:

Rafa.js is a Javascript framework for building concurrent applications.

44 lines (38 loc) 1.23 kB
// # constructor(done: () => _): Context // Each time a message is pushed through a stream tree, a context is created // to keep track of any pending tasks (i.e. a stream"s handler returns a // promise). Once all of the stream"s tasks have completes, the function // stored in the `done` parameter is called. function Context(done) { this.done = done || this.noop; this.pending = 0; } inherit(Context, Rafa, { // # finished() // Decrement the number of pending streams we're waiting for and call this // Context's done method if all streams have processed their message. end() { if (!--this.pending) this.done(); }, // # reset(done: () => _) // Reset this context so it can be used to push another message through // a stream. reset(done) { if (done) this.done = done; this.pending = 1; }, wait() { this.pending++; }, // # waitfor(Stream, Message) // Wait for the Promise inside a Message to finish, then call the finished // method of this context. waitfor(stream, message) { this.pending++; const push = m => { this.pending--; stream.push(this, m); }; message.value.then( v => push(message.copy(v)), e => push(this.errorMessage(e)) ); } });