82 lines
2.0 KiB
JavaScript
82 lines
2.0 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.map = map;
|
||
|
exports.constant = constant;
|
||
|
exports.tap = tap;
|
||
|
|
||
|
var _Stream = require('../Stream');
|
||
|
|
||
|
var _Stream2 = _interopRequireDefault(_Stream);
|
||
|
|
||
|
var _Map = require('../fusion/Map');
|
||
|
|
||
|
var _Map2 = _interopRequireDefault(_Map);
|
||
|
|
||
|
var _Pipe = require('../sink/Pipe');
|
||
|
|
||
|
var _Pipe2 = _interopRequireDefault(_Pipe);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* Transform each value in the stream by applying f to each
|
||
|
* @param {function(*):*} f mapping function
|
||
|
* @param {Stream} stream stream to map
|
||
|
* @returns {Stream} stream containing items transformed by f
|
||
|
*/
|
||
|
function map(f, stream) {
|
||
|
return new _Stream2.default(_Map2.default.create(f, stream.source));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Replace each value in the stream with x
|
||
|
* @param {*} x
|
||
|
* @param {Stream} stream
|
||
|
* @returns {Stream} stream containing items replaced with x
|
||
|
*/
|
||
|
/** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
function constant(x, stream) {
|
||
|
return map(function () {
|
||
|
return x;
|
||
|
}, stream);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Perform a side effect for each item in the stream
|
||
|
* @param {function(x:*):*} f side effect to execute for each item. The
|
||
|
* return value will be discarded.
|
||
|
* @param {Stream} stream stream to tap
|
||
|
* @returns {Stream} new stream containing the same items as this stream
|
||
|
*/
|
||
|
function tap(f, stream) {
|
||
|
return new _Stream2.default(new Tap(f, stream.source));
|
||
|
}
|
||
|
|
||
|
function Tap(f, source) {
|
||
|
this.source = source;
|
||
|
this.f = f;
|
||
|
}
|
||
|
|
||
|
Tap.prototype.run = function (sink, scheduler) {
|
||
|
return this.source.run(new TapSink(this.f, sink), scheduler);
|
||
|
};
|
||
|
|
||
|
function TapSink(f, sink) {
|
||
|
this.sink = sink;
|
||
|
this.f = f;
|
||
|
}
|
||
|
|
||
|
TapSink.prototype.end = _Pipe2.default.prototype.end;
|
||
|
TapSink.prototype.error = _Pipe2.default.prototype.error;
|
||
|
|
||
|
TapSink.prototype.event = function (t, x) {
|
||
|
var f = this.f;
|
||
|
f(x);
|
||
|
this.sink.event(t, x);
|
||
|
};
|