UNPKG

xstream

Version:

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

101 lines 2.49 kB
"use strict"; var core_1 = require('../core'); var DebounceOperator = (function () { function DebounceOperator(dt, ins) { this.dt = dt; this.ins = ins; this.type = 'debounce'; this.out = null; this.value = null; this.id = null; } DebounceOperator.prototype._start = function (out) { this.out = out; this.ins._add(this); }; DebounceOperator.prototype._stop = function () { this.ins._remove(this); this.out = null; this.value = null; this.id = null; }; DebounceOperator.prototype.clearInterval = function () { var id = this.id; if (id !== null) { clearInterval(id); } this.id = null; }; DebounceOperator.prototype._n = function (t) { var _this = this; var u = this.out; if (!u) return; this.value = t; this.clearInterval(); this.id = setInterval(function () { _this.clearInterval(); u._n(t); }, this.dt); }; DebounceOperator.prototype._e = function (err) { var u = this.out; if (!u) return; this.clearInterval(); u._e(err); }; DebounceOperator.prototype._c = function () { var u = this.out; if (!u) return; this.clearInterval(); u._c(); }; return DebounceOperator; }()); /** * Delays events until a certain amount of silence has passed. If that timespan * of silence is not met the event is dropped. * * Marble diagram: * * ```text * --1----2--3--4----5| * debounce(60) * -----1----------4--| * ``` * * Example: * * ```js * import fromDiagram from 'xstream/extra/fromDiagram' * import debounce from 'xstream/extra/debounce' * * const stream = fromDiagram('--1----2--3--4----5|') * .compose(debounce(60)) * * stream.addListener({ * next: i => console.log(i), * error: err => console.error(err), * complete: () => console.log('completed') * }) * ``` * * ```text * > 1 * > 4 * > completed * ``` * * @param {number} period The amount of silence required in milliseconds. * @return {Stream} */ function debounce(period) { return function debounceOperator(ins) { return new core_1.Stream(new DebounceOperator(period, ins)); }; } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = debounce; //# sourceMappingURL=debounce.js.map