theya
theya

Reputation: 33

rxjs debounceTime on Subject

Have a piece of code like this:

const hoverQueue$ = new Rx.Subject()
    .debounceTime(1000)
    .subscribe(() => {
        console.log('subject')
});

const callback = () => {
    hoverQueue$.next(true)
}

Which doesn't seem to work. Instead of having my events being delayed by 1s and getting only the last value emitted, they all pass through as if debounceTime doesn't exist. the same behaviour happens with delay, doesn't seem to work. Seems to work as intended on observables. callback is a callback called by another library i am using.

Not sure if i have missed something or if im doing something that's not supposed to be done, cant find much about this online.

have a fiddle below with an example to demonstrate

here is a fiddle with rxjs 5.4.1 which is what im running https://jsfiddle.net/theya222/2nhu1ka7/

and here is one with the latest version of rxjs https://jsfiddle.net/theya222/j1uLw80d/

thanks

Upvotes: 1

Views: 3492

Answers (1)

Picci
Picci

Reputation: 17752

hoverQueue$ is the result of executing subscribe on an Observable. Therefore hoverQueue$ is a Subcription not an Observable and a Subscription does not have the method next.

What you have to do is this

const subj = new Rx.Subject();
const hoverQueue$ = subj.debounceTime(1000);

hoverQueue$.subscribe(() => {
        console.log('subject')
});

const callback = () => {
    subj.next(true)
}

if callback is a

callback called by another library

take a look at bindCallback method/function defined with Observable.

Upvotes: 3

Related Questions