136 lines
3.6 KiB
JavaScript
136 lines
3.6 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.sample = sample;
|
||
|
exports.sampleWith = sampleWith;
|
||
|
exports.sampleArray = sampleArray;
|
||
|
|
||
|
var _Stream = require('../Stream');
|
||
|
|
||
|
var _Stream2 = _interopRequireDefault(_Stream);
|
||
|
|
||
|
var _Pipe = require('../sink/Pipe');
|
||
|
|
||
|
var _Pipe2 = _interopRequireDefault(_Pipe);
|
||
|
|
||
|
var _dispose = require('../disposable/dispose');
|
||
|
|
||
|
var dispose = _interopRequireWildcard(_dispose);
|
||
|
|
||
|
var _prelude = require('@most/prelude');
|
||
|
|
||
|
var base = _interopRequireWildcard(_prelude);
|
||
|
|
||
|
var _invoke = require('../invoke');
|
||
|
|
||
|
var _invoke2 = _interopRequireDefault(_invoke);
|
||
|
|
||
|
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 }; }
|
||
|
|
||
|
/**
|
||
|
* When an event arrives on sampler, emit the result of calling f with the latest
|
||
|
* values of all streams being sampled
|
||
|
* @param {function(...values):*} f function to apply to each set of sampled values
|
||
|
* @param {Stream} sampler streams will be sampled whenever an event arrives
|
||
|
* on sampler
|
||
|
* @returns {Stream} stream of sampled and transformed values
|
||
|
*/
|
||
|
function sample(f, sampler /*, ...streams */) {
|
||
|
return sampleArray(f, sampler, base.drop(2, arguments));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* When an event arrives on sampler, emit the latest event value from stream.
|
||
|
* @param {Stream} sampler stream of events at whose arrival time
|
||
|
* stream's latest value will be propagated
|
||
|
* @param {Stream} stream stream of values
|
||
|
* @returns {Stream} sampled stream of values
|
||
|
*/
|
||
|
/** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
function sampleWith(sampler, stream) {
|
||
|
return new _Stream2.default(new Sampler(base.id, sampler.source, [stream.source]));
|
||
|
}
|
||
|
|
||
|
function sampleArray(f, sampler, streams) {
|
||
|
return new _Stream2.default(new Sampler(f, sampler.source, base.map(getSource, streams)));
|
||
|
}
|
||
|
|
||
|
function getSource(stream) {
|
||
|
return stream.source;
|
||
|
}
|
||
|
|
||
|
function Sampler(f, sampler, sources) {
|
||
|
this.f = f;
|
||
|
this.sampler = sampler;
|
||
|
this.sources = sources;
|
||
|
}
|
||
|
|
||
|
Sampler.prototype.run = function (sink, scheduler) {
|
||
|
var this$1 = this;
|
||
|
|
||
|
var l = this.sources.length;
|
||
|
var disposables = new Array(l + 1);
|
||
|
var sinks = new Array(l);
|
||
|
|
||
|
var sampleSink = new SampleSink(this.f, sinks, sink);
|
||
|
|
||
|
for (var hold, i = 0; i < l; ++i) {
|
||
|
hold = sinks[i] = new Hold(sampleSink);
|
||
|
disposables[i] = this$1.sources[i].run(hold, scheduler);
|
||
|
}
|
||
|
|
||
|
disposables[i] = this.sampler.run(sampleSink, scheduler);
|
||
|
|
||
|
return dispose.all(disposables);
|
||
|
};
|
||
|
|
||
|
function Hold(sink) {
|
||
|
this.sink = sink;
|
||
|
this.hasValue = false;
|
||
|
}
|
||
|
|
||
|
Hold.prototype.event = function (t, x) {
|
||
|
this.value = x;
|
||
|
this.hasValue = true;
|
||
|
this.sink._notify(this);
|
||
|
};
|
||
|
|
||
|
Hold.prototype.end = function () {};
|
||
|
Hold.prototype.error = _Pipe2.default.prototype.error;
|
||
|
|
||
|
function SampleSink(f, sinks, sink) {
|
||
|
this.f = f;
|
||
|
this.sinks = sinks;
|
||
|
this.sink = sink;
|
||
|
this.active = false;
|
||
|
}
|
||
|
|
||
|
SampleSink.prototype._notify = function () {
|
||
|
if (!this.active) {
|
||
|
this.active = this.sinks.every(hasValue);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
SampleSink.prototype.event = function (t) {
|
||
|
if (this.active) {
|
||
|
this.sink.event(t, (0, _invoke2.default)(this.f, base.map(getValue, this.sinks)));
|
||
|
}
|
||
|
};
|
||
|
|
||
|
SampleSink.prototype.end = _Pipe2.default.prototype.end;
|
||
|
SampleSink.prototype.error = _Pipe2.default.prototype.error;
|
||
|
|
||
|
function hasValue(hold) {
|
||
|
return hold.hasValue;
|
||
|
}
|
||
|
|
||
|
function getValue(hold) {
|
||
|
return hold.value;
|
||
|
}
|