111 lines
3.2 KiB
JavaScript
111 lines
3.2 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.scan = scan;
|
||
|
exports.reduce = reduce;
|
||
|
|
||
|
var _Stream = require('../Stream');
|
||
|
|
||
|
var _Stream2 = _interopRequireDefault(_Stream);
|
||
|
|
||
|
var _Pipe = require('../sink/Pipe');
|
||
|
|
||
|
var _Pipe2 = _interopRequireDefault(_Pipe);
|
||
|
|
||
|
var _runSource = require('../runSource');
|
||
|
|
||
|
var _dispose = require('../disposable/dispose');
|
||
|
|
||
|
var dispose = _interopRequireWildcard(_dispose);
|
||
|
|
||
|
var _PropagateTask = require('../scheduler/PropagateTask');
|
||
|
|
||
|
var _PropagateTask2 = _interopRequireDefault(_PropagateTask);
|
||
|
|
||
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
function scan(f, initial, stream) {
|
||
|
return new _Stream2.default(new Scan(f, initial, stream.source));
|
||
|
} /** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
function Scan(f, z, source) {
|
||
|
this.source = source;
|
||
|
this.f = f;
|
||
|
this.value = z;
|
||
|
}
|
||
|
|
||
|
Scan.prototype.run = function (sink, scheduler) {
|
||
|
var d1 = scheduler.asap(_PropagateTask2.default.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 = _Pipe2.default.prototype.error;
|
||
|
ScanSink.prototype.end = _Pipe2.default.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
|
||
|
*/
|
||
|
function reduce(f, initial, stream) {
|
||
|
return (0, _runSource.withDefaultScheduler)(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 = _Pipe2.default.prototype.error;
|
||
|
|
||
|
ReduceSink.prototype.end = function (t) {
|
||
|
this.sink.end(t, this.value);
|
||
|
};
|