Angular click debounce
Use the takeUntil
operator :
export class AppComponent {
name = 'Angular';
calls = new Subject();
service = {
getData: () => of({ id: 1 }).pipe(delay(500)),
};
click() {
this.calls.next(true);
this.service.getData().pipe(
takeUntil(this.calls),
).subscribe(res => console.log(res));
}
}
Stackblitz (open your console to check the logs)
This is answer partially I found in internet, but I open to better solutions (or improve to below solution(directive)):
In internet I found appDebounceClick
directive which helps me in following way:
I remove update
from add
in .ts file:
add(num) {
this.myValue +=num;
}
And change template in following way:
<div
appDebounceClick
(debounceClick)="update()"
(click)="add(1)"
class="btn-plus"
> -
</div>
<div class="txt"> {{ myValue }} </div>
<!-- similar for btn-minus -->
BONUS
Directive appDebounceClick
written by Cory Rylan (I put code here in case if link will stop working in future):
import { Directive, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs/Subscription';
import { debounceTime } from 'rxjs/operators';
@Directive({
selector: '[appDebounceClick]'
})
export class DebounceClickDirective implements OnInit, OnDestroy {
@Input() debounceTime = 500;
@Output() debounceClick = new EventEmitter();
private clicks = new Subject();
private subscription: Subscription;
constructor() { }
ngOnInit() {
this.subscription = this.clicks.pipe(
debounceTime(this.debounceTime)
).subscribe(e => this.debounceClick.emit(e));
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
@HostListener('click', ['$event'])
clickEvent(event) {
event.preventDefault();
event.stopPropagation();
this.clicks.next(event);
}
}
A helper function --
export const debounced = (cb, time) => {
const db = new Subject();
const sub = db.pipe(debounceTime(time)).subscribe(cb);
const func = v => db.next(v);
func.unsubscribe = () => sub.unsubscribe();
return func;
};
Then an example use could be:
import { Component, OnInit } from '@angular/core';
import { debounced } from 'src/helpers';
@Component({
selector: 'app-example',
// Click calls `debouncedClick` instead of `myClick` directly
template: '<button (click)="debouncedClick($event)">Click This</button>'
})
export class Example implements OnDestroy {
debouncedClick; // Subject.next function
constructor() {
// Done in constructor or ngOnInit for `this` to resolve
this.debouncedClick = debounced($event => this.myClick($event), 800);
}
// Called after debounced resolves (800ms from last call)
myClick($event) {
console.log($event);
}
ngOnDestroy() {
// Stay clean!
this.debouncedFunc.unsubscribe();
}
}
Could also reverse the usage, calling the 'myClick' on click and have the debounced
callback perform the desired action. Dealer's choice.
Personally I this for (keyup)
events as well.
Unsure if the unsubscribe is really necessary - was quicker to implement than to research the memory leak :)