0

I am trying to use switchMap in rxjs to only emit the most recent observable and only once(the other observables should be cancelled). The goal to use this with the debounce operator, this is my start point. Now trying to make it so if I click the button multiple times I only get one emit as currently I will get as many emits as times I click the button without a > 1 second delay.(5 clicks then a 1 second wait is 5 console.logs at the same time)

Could someone point out to me how I can change this to get the desired result? I am trying to read through the posts here on switchMap but I find myself lacking =/ reading/watching more on switchMap, I am not even sure if this is the right way to accomplish what I am trying to.

const button = document.getElementById('debounce');
const click$ = fromEvent(button!, 'click');
click$.pipe(
  startWith(null),
  debounceTime(1000)
  )
  .subscribe(() => {
    console.log('Button clicked after 1 second of inactivity')
    
    // Emit your desired value or trigger your desired action here
  });

So I tried.

const button = document.getElementById('debounce');
const click$ = fromEvent(button!, 'click');
click$.pipe(
  startWith(null),
  debounceTime(1000),
  switchMap(event =>{
    return timer(1000)
  })
  )
  .subscribe(() => {
    console.log('Button clicked after 1 second of inactivity')
    
    // Emit your desired value or trigger your desired action here
  });

but I can see that the switchMap here should be canceling previous click$ calls but not the inner timer calls....i think.

The results of this is that for each click the console log increments in increasing values. Button clicked after 1 second of inactivity 3 Button clicked after 1 second of inactivity 6 Button clicked after 1 second of inactivity 10 Button clicked after 1 second of inactivity ect...

the goal is for each click to always just return one log.(trying to get this right so I can use switchMap for searching other debounce things. :D)

Thank you for your time.

Nevaden
  • 1
  • 2
  • "*5 clicks then a 1 second wait is 5 console.logs at the same time*" - your first code sample should not produce this result. It should emit immediately (*due to the startWith*), then will emit once more after some clicks after the duration (1000ms) has passed. – BizzyBob Mar 10 '23 at 14:39
  • for the first example if I do not have the startsWith then I do not get a console for the first click and only subsequent clicks. No idea why but after looking that issue up startsWith was recommended. So for me the first example as shown is only giving me one console.log. – Nevaden Mar 10 '23 at 14:59
  • is [this](https://stackblitz.com/edit/rxjs-ba5a6n?devtoolsheight=60&file=index.ts) the behavior you want? – BizzyBob Mar 10 '23 at 15:38
  • Yes! I replicated your code exactly and I am still getting a growing number of reports with each click(with the first click being completely silent). I imagine I must have some type of version issue or something, I am not sure. @angular-devkit/architect 0.1502.1 @angular-devkit/build-angular 15.2.1 @angular-devkit/core 15.2.1 @angular-devkit/schematics 15.1.6 @angular/cli 15.1.6 @schematics/angular 15.1.6 rxjs 7.8.0 typescript 4.9.5 "rxjs": "~7.8.0", – Nevaden Mar 10 '23 at 16:26
  • Please share more of your code or recreate your issue on [StackBlitz](https://stackblitz.com/fork/angular). I'm guessing you are ending up with multiple subscriptions that are causing your logic to be executed multiple times, but it's hard to say exactly without seeing more code. – BizzyBob Mar 10 '23 at 18:18
  • OK I am working on that now. I also think I narrowed it down. In my usage I am calling a function that has that code ie: (click)="debounce()" , if I just throw the code into the NgInit then I do not get the ever increasing issue. I will edit this comment as soon as I complete it. – Nevaden Mar 11 '23 at 03:01
  • [link](https://stackblitz.com/edit/angular-h4skkr?file=src%2Fmain.ts,src%2Fhelper.ts,src%2Fglobal_styles.css) Here we go. Debounce being the focus. So yeah, I guess calling my debounce as a function is the issue. So the function is triggering the subscription as well as...the code in the function? So is a function creating a subscription not a ok way to approach this and I should add click events? I also noticed basically all of my functions are doing this I just did not notice...or maybe the others should just debounce should not? =/ sorry for all the questions. – Nevaden Mar 11 '23 at 03:44

0 Answers0