UNPKG

xstream

Version:

An extremely intuitive, small, and fast functional reactive stream library for JavaScript

93 lines 2.22 kB
"use strict"; var core_1 = require('../core'); var DelayOperator = (function () { function DelayOperator(dt, ins) { this.dt = dt; this.ins = ins; this.type = 'delay'; this.out = null; } DelayOperator.prototype._start = function (out) { this.out = out; this.ins._add(this); }; DelayOperator.prototype._stop = function () { this.ins._remove(this); this.out = null; }; DelayOperator.prototype._n = function (t) { var u = this.out; if (!u) return; var id = setInterval(function () { u._n(t); clearInterval(id); }, this.dt); }; DelayOperator.prototype._e = function (err) { var u = this.out; if (!u) return; var id = setInterval(function () { u._e(err); clearInterval(id); }, this.dt); }; DelayOperator.prototype._c = function () { var u = this.out; if (!u) return; var id = setInterval(function () { u._c(); clearInterval(id); }, this.dt); }; return DelayOperator; }()); /** * Delays periodic events by a given time period. * * Marble diagram: * * ```text * 1----2--3--4----5| * delay(60) * ---1----2--3--4----5| * ``` * * Example: * * ```js * import fromDiagram from 'xstream/extra/fromDiagram' * import delay from 'xstream/extra/delay' * * const stream = fromDiagram('1----2--3--4----5|') * .compose(delay(60)) * * stream.addListener({ * next: i => console.log(i), * error: err => console.error(err), * complete: () => console.log('completed') * }) * ``` * * ```text * > 1 (after 60 ms) * > 2 (after 160 ms) * > 3 (after 220 ms) * > 4 (after 280 ms) * > 5 (after 380 ms) * > completed * ``` * * @param {number} period The amount of silence required in milliseconds. * @return {Stream} */ function delay(period) { return function delayOperator(ins) { return new core_1.Stream(new DelayOperator(period, ins)); }; } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = delay; //# sourceMappingURL=delay.js.map