javascript - What does RxJS.Observable debounce do? -
can explain in plain english rxjs observavle debounce function does?
i imagine emits event once in while depending on parameters, code below doesn't work expected.
var x$ = rx.observable.fromevent(window, 'click') .map(function(e) {return {x:e.x, y:e.y};}) .debounce(1000) .subscribe(function(el) { console.log(el); });
and the jsbin version.
i expected code print 1 click once per second, no matter how fast clicking. instead prints click @ think random intervals.
debounce emit value after specified time interval has passed without value being emitted.
using simple diagrams following may provide greater help:
stream 1 | ---1-------2-3-4-5---------6---- after debounce, emitted stream looks follows: stream 2 | ------1-------------5---------6-
the intermediate items (in case, 2,3,4) ignored.
an example illustrated below:
var rx = require('rx-node'); var source = rx.fromstream(process.stdin).debounce(500); var subscription = source.subscribe( function (x) { console.log('next: %s', x); } );
i used node illustrate this... assuming have node installed, can run typing
$node myfile.js (where aforementioned code in myfile.js)
once node program started can type values @ console -- if type items ignored, , if type intermittently fast , slow items appear after gap in typing (in example above have 500ms) @ console ("next: ")
there excellent reference material @ https://github.com/reactive-extensions/rxjs/blob/master/doc/api/core/operators/debounce.md
Comments
Post a Comment