@tempest/core
Version:
The core of the Tempest Stream Library
58 lines • 1.64 kB
JavaScript
import { addSink, removeSink, none } from './sink';
import { MulticastDisposable } from './MulticastDisposable';
const EMPTY = { dispose() { return void 0; } };
const NONE = null;
export class Multicast {
constructor(source) {
this.activeCount = 0;
this.source = source;
this.sink = none();
this.disposable = EMPTY;
this._stopId = NONE;
}
run(sink, scheduler) {
const n = this._add(sink);
if (n === 1) {
if (this._stopId !== NONE) {
scheduler.cancel(this._stopId);
this._stopId = NONE;
}
if (this.disposable === EMPTY) {
this.disposable = this.source.run(this, scheduler);
}
}
return new MulticastDisposable(this, sink, scheduler);
}
_dispose() {
const disposable = this.disposable;
this.disposable = EMPTY;
this._stopId = NONE;
Promise.resolve(disposable).then(dispose);
}
_add(sink) {
this.sink = addSink(sink, this.sink);
this.activeCount += 1;
return this.activeCount;
}
_remove(sink) {
const s = this.sink;
this.sink = removeSink(sink, this.sink);
if (s !== this.sink) {
this.activeCount -= 1;
}
return this.activeCount;
}
event(time, value) {
this.sink.event(time, value);
}
end(time, value) {
this.sink.end(time, value);
}
error(time, err) {
this.sink.error(time, err);
}
}
function dispose(disposable) {
disposable.dispose();
}
//# sourceMappingURL=Multicast.js.map