Debounce without initial delay

Is there an operator in RxJS that delays without delay the "first event in the packet" but delays (and always emits) the "last event in the packet"?

Something like that:

---a----b-c-d-----e-f---

after awesome-debounce(2 dashes)it becomes:

---a----b------d--e----f

while normal debounce will be:

-----a---------d-------f

This is a kind of mixture between the throttle and debounce ...

+6
source share
2 answers

Hmm, this is the easiest solution I can come up with. The interesting part for you is awesomeDebounce()which creates awesomeDebounce().

It basically just combines operators throttle()and debounceTime():

const Rx = require('rxjs');
const chai = require('chai');

let scheduler = new Rx.TestScheduler((actual, expected) => {
  chai.assert.deepEqual(actual, expected);
  console.log(actual);
});

function awesomeDebounce(source, timeWindow = 1000, scheduler = Rx.Scheduler.async) {
  let shared = source.share();
  let notification = shared
      .switchMap(val => Rx.Observable.of(val).delay(timeWindow, scheduler))
      .publish();

  notification.connect();

  return shared
    .throttle(() => notification)
    .merge(shared.debounceTime(timeWindow, scheduler))
    .distinctUntilChanged();
}

let sourceMarbles =   '---a----b-c-d-----e-f---';
let expectedMarbles = '---a----b------d--e----f';

// Create the test Observable
let observable = scheduler
  .createHotObservable(sourceMarbles)
  .let(source => awesomeDebounce(source, 30, scheduler));

scheduler.expectObservable(observable).toBe(expectedMarbles);
scheduler.flush();

notification Observable throttle() , . Observable "", throttle().

+2

debounce . merge, throttleTime debounceTime :

Rx.Observable.merge(source.throttleTime(1000), source.debounceTime(2000))

http://jsbin.com/godocuqiwo/edit?js,console

: , , ( , , , ).

-1

Source: https://habr.com/ru/post/1016226/


All Articles