Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Created October 10, 2020 06:14
Show Gist options
  • Save LironHazan/51acdb9dd97de985ac1823d46b3f6486 to your computer and use it in GitHub Desktop.
Save LironHazan/51acdb9dd97de985ac1823d46b3f6486 to your computer and use it in GitHub Desktop.

Revisions

  1. LironHazan created this gist Oct 10, 2020.
    20 changes: 20 additions & 0 deletions close-that-stream-if-no-need.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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();
    }