UNPKG

pnpm

Version:

Fast, disk space efficient package manager

90 lines (75 loc) 2.48 kB
/** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ import Stream from '../Stream' import Pipe from '../sink/Pipe' import { withDefaultScheduler as runSource } from '../runSource' import * as dispose from '../disposable/dispose' import PropagateTask from '../scheduler/PropagateTask' /** * Create a stream containing successive reduce results of applying f to * the previous reduce result and the current stream item. * @param {function(result:*, x:*):*} f reducer function * @param {*} initial initial value * @param {Stream} stream stream to scan * @returns {Stream} new stream containing successive reduce results */ export function scan (f, initial, stream) { return new Stream(new Scan(f, initial, stream.source)) } function Scan (f, z, source) { this.source = source this.f = f this.value = z } Scan.prototype.run = function (sink, scheduler) { var d1 = scheduler.asap(PropagateTask.event(this.value, sink)) var d2 = this.source.run(new ScanSink(this.f, this.value, sink), scheduler) return dispose.all([d1, d2]) } function ScanSink (f, z, sink) { this.f = f this.value = z this.sink = sink } ScanSink.prototype.event = function (t, x) { var f = this.f this.value = f(this.value, x) this.sink.event(t, this.value) } ScanSink.prototype.error = Pipe.prototype.error ScanSink.prototype.end = Pipe.prototype.end /** * Reduce a stream to produce a single result. Note that reducing an infinite * stream will return a Promise that never fulfills, but that may reject if an error * occurs. * @param {function(result:*, x:*):*} f reducer function * @param {*} initial initial value * @param {Stream} stream to reduce * @returns {Promise} promise for the file result of the reduce */ export function reduce (f, initial, stream) { return runSource(new Reduce(f, initial, stream.source)) } function Reduce (f, z, source) { this.source = source this.f = f this.value = z } Reduce.prototype.run = function (sink, scheduler) { return this.source.run(new ReduceSink(this.f, this.value, sink), scheduler) } function ReduceSink (f, z, sink) { this.f = f this.value = z this.sink = sink } ReduceSink.prototype.event = function (t, x) { var f = this.f this.value = f(this.value, x) this.sink.event(t, this.value) } ReduceSink.prototype.error = Pipe.prototype.error ReduceSink.prototype.end = function (t) { this.sink.end(t, this.value) }