@thi.ng/rstream
Version:
Reactive streams & subscription primitives for constructing dataflow graphs / pipelines
46 lines (45 loc) • 1.04 kB
JavaScript
import { State } from "./api.js";
import { __optsWithID } from "./idgen.js";
import { LOGGER } from "./logger.js";
import { Subscription } from "./subscription.js";
const resolve = (opts) => new Resolver(opts);
class Resolver extends Subscription {
outstanding = 0;
fail;
constructor(opts = {}) {
super(void 0, __optsWithID("resolve"));
this.fail = opts.fail;
}
next(x) {
this.outstanding++;
x.then(
(y) => {
if (this.state < State.DONE) {
this.dispatch(y);
if (--this.outstanding === 0) {
this.done();
}
} else {
LOGGER.warn(`resolved value in state ${this.state} (${x})`);
}
},
(e) => {
if (this.fail) {
this.fail(e);
} else {
this.error(e);
}
}
);
}
done() {
const pstate = this.parent?.getState();
if ((pstate === State.DONE || pstate === State.UNSUBSCRIBED) && this.outstanding === 0) {
super.done();
}
}
}
export {
Resolver,
resolve
};