134 lines
3.5 KiB
JavaScript
134 lines
3.5 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.takeUntil = takeUntil;
|
||
|
exports.skipUntil = skipUntil;
|
||
|
exports.during = during;
|
||
|
|
||
|
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 _flatMap = require('../combinator/flatMap');
|
||
|
|
||
|
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 }; }
|
||
|
|
||
|
/** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
function takeUntil(signal, stream) {
|
||
|
return new _Stream2.default(new Until(signal.source, stream.source));
|
||
|
}
|
||
|
|
||
|
function skipUntil(signal, stream) {
|
||
|
return new _Stream2.default(new Since(signal.source, stream.source));
|
||
|
}
|
||
|
|
||
|
function during(timeWindow, stream) {
|
||
|
return takeUntil((0, _flatMap.join)(timeWindow), skipUntil(timeWindow, stream));
|
||
|
}
|
||
|
|
||
|
function Until(maxSignal, source) {
|
||
|
this.maxSignal = maxSignal;
|
||
|
this.source = source;
|
||
|
}
|
||
|
|
||
|
Until.prototype.run = function (sink, scheduler) {
|
||
|
var min = new Bound(-Infinity, sink);
|
||
|
var max = new UpperBound(this.maxSignal, sink, scheduler);
|
||
|
var disposable = this.source.run(new TimeWindowSink(min, max, sink), scheduler);
|
||
|
|
||
|
return dispose.all([min, max, disposable]);
|
||
|
};
|
||
|
|
||
|
function Since(minSignal, source) {
|
||
|
this.minSignal = minSignal;
|
||
|
this.source = source;
|
||
|
}
|
||
|
|
||
|
Since.prototype.run = function (sink, scheduler) {
|
||
|
var min = new LowerBound(this.minSignal, sink, scheduler);
|
||
|
var max = new Bound(Infinity, sink);
|
||
|
var disposable = this.source.run(new TimeWindowSink(min, max, sink), scheduler);
|
||
|
|
||
|
return dispose.all([min, max, disposable]);
|
||
|
};
|
||
|
|
||
|
function Bound(value, sink) {
|
||
|
this.value = value;
|
||
|
this.sink = sink;
|
||
|
}
|
||
|
|
||
|
Bound.prototype.error = _Pipe2.default.prototype.error;
|
||
|
Bound.prototype.event = noop;
|
||
|
Bound.prototype.end = noop;
|
||
|
Bound.prototype.dispose = noop;
|
||
|
|
||
|
function TimeWindowSink(min, max, sink) {
|
||
|
this.min = min;
|
||
|
this.max = max;
|
||
|
this.sink = sink;
|
||
|
}
|
||
|
|
||
|
TimeWindowSink.prototype.event = function (t, x) {
|
||
|
if (t >= this.min.value && t < this.max.value) {
|
||
|
this.sink.event(t, x);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
TimeWindowSink.prototype.error = _Pipe2.default.prototype.error;
|
||
|
TimeWindowSink.prototype.end = _Pipe2.default.prototype.end;
|
||
|
|
||
|
function LowerBound(signal, sink, scheduler) {
|
||
|
this.value = Infinity;
|
||
|
this.sink = sink;
|
||
|
this.disposable = signal.run(this, scheduler);
|
||
|
}
|
||
|
|
||
|
LowerBound.prototype.event = function (t /*, x */) {
|
||
|
if (t < this.value) {
|
||
|
this.value = t;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
LowerBound.prototype.end = noop;
|
||
|
LowerBound.prototype.error = _Pipe2.default.prototype.error;
|
||
|
|
||
|
LowerBound.prototype.dispose = function () {
|
||
|
return this.disposable.dispose();
|
||
|
};
|
||
|
|
||
|
function UpperBound(signal, sink, scheduler) {
|
||
|
this.value = Infinity;
|
||
|
this.sink = sink;
|
||
|
this.disposable = signal.run(this, scheduler);
|
||
|
}
|
||
|
|
||
|
UpperBound.prototype.event = function (t, x) {
|
||
|
if (t < this.value) {
|
||
|
this.value = t;
|
||
|
this.sink.end(t, x);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
UpperBound.prototype.end = noop;
|
||
|
UpperBound.prototype.error = _Pipe2.default.prototype.error;
|
||
|
|
||
|
UpperBound.prototype.dispose = function () {
|
||
|
return this.disposable.dispose();
|
||
|
};
|
||
|
|
||
|
function noop() {}
|