I've been using Angular for a while. And recently, I'm keeping thinking(and searching) what's the preferred practice to subscribe an Observable like Subject, which will get emitted by a certain DOM event.
For example (Which I think may be a neat and clean implementation here?)
<!-- component.html -->
<div>
<button (click)="clickEvent$.next($event)">Click Me!</button>
</div>
// component.ts
...
class Component implements OnInit, OnDestroy {
public clickEvent$ = new Subject();
private subscriptions = new Subscription();
public ngOnInit() {
this.subscriptions.add(this.handleClick(this.clickEvent$));
}
public ngOnDestroy() {
this.subscriptions.unsubscribe();
}
public handleClick<T>(subject: Subject<T>): Subscription {
return subject.pipe(debounceTime(300)).subscribe(
event => {
// do something
}
);
}
}
...
So I wonder if there is any better or recommended implementation. If so please do share it :)