RxJS - subscribe only once but do not complete Observable

Raold

imagine situation when you have some Observable that contains data that changes in real time, example below...

interface User {
   name: string;
   projectId: string;
   dataThatChangesALotInRealTime: Object;
}

userData: Observable<User>

This userData observable is used in component to show some data that changes in real time. e.g.

<p>
{{ (userData | async)?.dataThatChangesALotInRealTime }}
</p>

Now I want to insert some data to database according to current data in userData observable. Here is the function

addToDatabase() {
  let sub = this.userData.subscribe(data => {
     this.exampleDatabase.doc(`test/${data.dataThatChangesALotInRealTime.id}`)
          .add({ test: 'hello'})
     sub.unsubscribe() // <- This
  })
}

Question

Is this a correct solution to unsubscribe inside subscription to avoid multiple insertion into database? Is there a different/better way to do this?

This is just minimalistic example, if you have some questions or my explanation is poor, let me know in comments and I will update my question. Thank you

Ingo Bürk

You can use the first operator:

this.userData.pipe(first()).subscribe(...);

This will automatically complete (and therefore unsubscribe) after the first value has been emitted.

Note that you should ensure that it emits at least once before completing as otherwise an error will be thrown. If you can't ensure this, you can use take(1) instead:

this.userData.pipe(take(1)).subscribe(...);

Note that this doesn't actually modify the userData observable directly, so other subscriptions to it will continue emitting regardless. This is because operators in rxjs do not modify observables but instead return a new observable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Difference between the methods .pipe() and .subscribe() on a RXJS observable

How to do initialization logic on an observable pipe once per subscription in rxjs

Difference between do and subscribe in rxjs

rxjs using promise only once on subscribe

RxJs: How to only maintain the latest value until inner observable complete

With RxJs in Angular do I need to only complete the Observables created in the component?

rxjs Observable: handle unsubscribe of all subscribe

redux-observable and rxjs. Converting promise to Observable - epic gets called only once

RxJS: How to subscribe to inner observable only when outer observable/subject is subscribed to?

Return an observable only after subscribe

Subscribe to child Observable's Subscribe in RxJS

When is rxjs observable subscribe executed synchronously?

How to await an asynchonous method defined inside subscribe before to complete an Rxjs Observable?

Do parts of an rxjs pipe only for certain emissions of the Observable

RxJS Observable - subscribe once every time a condition met

RxJS 5 fromEvent observable subscribe called twice

Angular Rxjs Observable items count doubles on subscribe

rxjs6 ActivatedRoute subscribe to observable

Observable subscribe alway once issue

How to subscribe multiple observable in rxjs 6 ? do is not a function in rxjs 6

how to subscribe to the last observable and execute a funtion in rxjs?

RxJS (Interval) Observable wait for last observable to complete

Can I subscribe to an RXJS observable the following way?

Do I need to complete a Subject that is only used to compose another Observable?

RxJS Waits until Subscribe Observable Finish

angular 2 rxjs subscribe to observable code not executing

subscribe keys of and observable in Angular/RxJS

rxjs observable doesn't subscribe with async pipe

RxJs Observable not calling complete method

TOP Ranking

HotTag

Archive