UNPKG

pnpm

Version:

Fast, disk space efficient package manager

93 lines (72 loc) 1.77 kB
/** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ import { defer } from '../task' export default function DeferredSink (sink) { this.sink = sink this.events = [] this.active = true } DeferredSink.prototype.event = function (t, x) { if (!this.active) { return } if (this.events.length === 0) { defer(new PropagateAllTask(this.sink, t, this.events)) } this.events.push({ time: t, value: x }) } DeferredSink.prototype.end = function (t, x) { if (!this.active) { return } this._end(new EndTask(t, x, this.sink)) } DeferredSink.prototype.error = function (t, e) { this._end(new ErrorTask(t, e, this.sink)) } DeferredSink.prototype._end = function (task) { this.active = false defer(task) } function PropagateAllTask (sink, time, events) { this.sink = sink this.events = events this.time = time } PropagateAllTask.prototype.run = function () { var events = this.events var sink = this.sink var event for (var i = 0, l = events.length; i < l; ++i) { event = events[i] this.time = event.time sink.event(event.time, event.value) } events.length = 0 } PropagateAllTask.prototype.error = function (e) { this.sink.error(this.time, e) } function EndTask (t, x, sink) { this.time = t this.value = x this.sink = sink } EndTask.prototype.run = function () { this.sink.end(this.time, this.value) } EndTask.prototype.error = function (e) { this.sink.error(this.time, e) } function ErrorTask (t, e, sink) { this.time = t this.value = e this.sink = sink } ErrorTask.prototype.run = function () { this.sink.error(this.time, this.value) } ErrorTask.prototype.error = function (e) { throw e }