63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.loop = loop;
|
||
|
|
||
|
var _Stream = require('../Stream');
|
||
|
|
||
|
var _Stream2 = _interopRequireDefault(_Stream);
|
||
|
|
||
|
var _Pipe = require('../sink/Pipe');
|
||
|
|
||
|
var _Pipe2 = _interopRequireDefault(_Pipe);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* Generalized feedback loop. Call a stepper function for each event. The stepper
|
||
|
* will be called with 2 params: the current seed and the an event value. It must
|
||
|
* return a new { seed, value } pair. The `seed` will be fed back into the next
|
||
|
* invocation of stepper, and the `value` will be propagated as the event value.
|
||
|
* @param {function(seed:*, value:*):{seed:*, value:*}} stepper loop step function
|
||
|
* @param {*} seed initial seed value passed to first stepper call
|
||
|
* @param {Stream} stream event stream
|
||
|
* @returns {Stream} new stream whose values are the `value` field of the objects
|
||
|
* returned by the stepper
|
||
|
*/
|
||
|
/** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
function loop(stepper, seed, stream) {
|
||
|
return new _Stream2.default(new Loop(stepper, seed, stream.source));
|
||
|
}
|
||
|
|
||
|
function Loop(stepper, seed, source) {
|
||
|
this.step = stepper;
|
||
|
this.seed = seed;
|
||
|
this.source = source;
|
||
|
}
|
||
|
|
||
|
Loop.prototype.run = function (sink, scheduler) {
|
||
|
return this.source.run(new LoopSink(this.step, this.seed, sink), scheduler);
|
||
|
};
|
||
|
|
||
|
function LoopSink(stepper, seed, sink) {
|
||
|
this.step = stepper;
|
||
|
this.seed = seed;
|
||
|
this.sink = sink;
|
||
|
}
|
||
|
|
||
|
LoopSink.prototype.error = _Pipe2.default.prototype.error;
|
||
|
|
||
|
LoopSink.prototype.event = function (t, x) {
|
||
|
var result = this.step(this.seed, x);
|
||
|
this.seed = result.seed;
|
||
|
this.sink.event(t, result.value);
|
||
|
};
|
||
|
|
||
|
LoopSink.prototype.end = function (t) {
|
||
|
this.sink.end(t, this.seed);
|
||
|
};
|