Forked from LironHazan/close-that-stream-if-no-need.ts
Created
January 26, 2022 07:49
-
-
Save webia1/bda5f7b8dd7d86978500c49570275da3 to your computer and use it in GitHub Desktop.
Irena's stop fetching recipe - for rxjs blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private unsubscribeDataFetch: Subject<void> = new Subject<void>(); | |
// polling data till we get what we need and stop | |
fetchData() { | |
this.dataService | |
.pipe(takeUntil(this.unsubscribeDataFetch)) | |
.subscribe((data) => actOnData(data)); | |
} | |
actOnData(data) { | |
if (this.someCondition(data)) { | |
this.stopFetchingData(); | |
} | |
} | |
stopFetchingData() { | |
this.unsubscribeDataFetch.next(); | |
this.unsubscribeDataFetch.complete(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment