In an Angular service I have a function, that executes on certain events. Inside the callback I need to fetch some data from ngrx store and do something with it based on a condition. Here is a simplified example:
export class MyService {
constructor(private store: Store<{}>) {
Library.onSomethingChanged((val) => {
this.store.select(mySelector).pipe(
tap((valueFromStore) => {
if (valueFromStore) {
// do something
}
})
).subscribe()
})
}
}
I'm not quite sure how to deal with subscribe
in this situation. I only need the pipe to execute once if something changes. Would this create a new subscription every time the onSomethingChanged
callback executes?
I think if you use take(1)
you will get the first emission and the subscription will not leak:
Library.onSomethingChanged((val) => {
this.store.select(mySelector).pipe(
take(1),
tap((valueFromStore) => {
if (valueFromStore) {
// do something
}
})
).subscribe()