80 lines
2.1 KiB
JavaScript
80 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.unfold = unfold;
|
|
|
|
var _Stream = require('../Stream');
|
|
|
|
var _Stream2 = _interopRequireDefault(_Stream);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
/**
|
|
* Compute a stream by unfolding tuples of future values from a seed value
|
|
* Event times may be controlled by returning a Promise from f
|
|
* @param {function(seed:*):{value:*, seed:*, done:boolean}|Promise<{value:*, seed:*, done:boolean}>} f unfolding function accepts
|
|
* a seed and returns a new tuple with a value, new seed, and boolean done flag.
|
|
* If tuple.done is true, the stream will end.
|
|
* @param {*} seed seed value
|
|
* @returns {Stream} stream containing all value of all tuples produced by the
|
|
* unfolding function.
|
|
*/
|
|
function unfold(f, seed) {
|
|
return new _Stream2.default(new UnfoldSource(f, seed));
|
|
} /** @license MIT License (c) copyright 2010-2016 original author or authors */
|
|
/** @author Brian Cavalier */
|
|
/** @author John Hann */
|
|
|
|
function UnfoldSource(f, seed) {
|
|
this.f = f;
|
|
this.value = seed;
|
|
}
|
|
|
|
UnfoldSource.prototype.run = function (sink, scheduler) {
|
|
return new Unfold(this.f, this.value, sink, scheduler);
|
|
};
|
|
|
|
function Unfold(f, x, sink, scheduler) {
|
|
this.f = f;
|
|
this.sink = sink;
|
|
this.scheduler = scheduler;
|
|
this.active = true;
|
|
|
|
var self = this;
|
|
function err(e) {
|
|
self.sink.error(self.scheduler.now(), e);
|
|
}
|
|
|
|
function start(unfold) {
|
|
return stepUnfold(unfold, x);
|
|
}
|
|
|
|
Promise.resolve(this).then(start).catch(err);
|
|
}
|
|
|
|
Unfold.prototype.dispose = function () {
|
|
this.active = false;
|
|
};
|
|
|
|
function stepUnfold(unfold, x) {
|
|
var f = unfold.f;
|
|
return Promise.resolve(f(x)).then(function (tuple) {
|
|
return continueUnfold(unfold, tuple);
|
|
});
|
|
}
|
|
|
|
function continueUnfold(unfold, tuple) {
|
|
if (tuple.done) {
|
|
unfold.sink.end(unfold.scheduler.now(), tuple.value);
|
|
return tuple.value;
|
|
}
|
|
|
|
unfold.sink.event(unfold.scheduler.now(), tuple.value);
|
|
|
|
if (!unfold.active) {
|
|
return tuple.value;
|
|
}
|
|
return stepUnfold(unfold, tuple.seed);
|
|
} |