I am aware of Observable.Never()
as a way to create a sequence that never completes, but is there an extension/clean process for creating an observable that produces a single value and then never completes? Do i go with Observable.Create(...)
? Observable.Concat(Observable.Return(onlyValue), Observable.Never<T>())
? Or is there something built in or more "RXy" than this?
You can use Observable. defer instead. It accepts a function that returns an Observable or an Observable-like thing (read: Promise, Array, Iterators).
Creating Observableslinkimport { Observable } from 'rxjs'; const observable = new Observable(function subscribe(subscriber) { const id = setInterval(() => { subscriber. next('hi'); }, 1000); });
We start by creating the subject, then create two Observers that will log each value they receive from the Subject (Observable). We tell the Subject to push the value 1 . We then create ObserverC which also logs each value it receives from the Subject. Finally, we tell the Subject to push the value 2 .
An Observable is basically a function that can return a stream of values to an observer over time, this can either be synchronously or asynchronously. The data values returned can go from zero to an infinite range of values.
For your specific question, a simple choice is to use ‛Never‛ and ‛StartWith‛:
Observable.Never<int>().StartWith(5)
But for the more general case of "I have an observable sequence that will produce some results and eventually complete and I want to change it so it does not complete" (of which your question is a special case), your Concat idea is the way to do it
source.Concat(Observable.Never<int>());
or
Observable.Concat(source, Observable.Never<int>());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With