If you want an Observable to emit a specific sequence of items before it begins emitting the items normally expected from it, apply the StartWith operator to it.
(If, on the other hand, you want to append a sequence of items to the end of those normally emitted by an Observable, you want the Concat operator.)
RxClojure implements this operator as cons
. It takes an item and an Observable
as parameters, and prepends the item to the items emitted by the Observable as its own
Observable sequence.
RxCpp implements this operator as start_with
. It takes an Observable and one
or more items as parameters, and prepends these items, in the order they are given in the
parameter list, to the items emitted by the Observable as its own Observable sequence.
RxGroovy implements this operator as startWith
.
You can pass the values you want to prepend to the Observable sequence to
startWith
either in a single Iterable or as a series of one to nine function
parameters.
def myObservable = Observable.from([1, 2, 3]); myObservable.startWith(-3, -2, -1, 0).subscribe( { println(it); }, // onNext { println("Error: " + it.getMessage()); }, // onError { println("Sequence complete"); } // onCompleted );
-3 -2 -1 0 1 2 3
startWith(Iterable)
startWith(T)
(there are also versions that take up to nine individual items)
You can also pass startWith
an Observable, and it will prepend the emissions
from that Observable to those of the source Observable to make its own set of emissions. This
is a sort of inverted Concat operation.
startWith(Observable)
RxJava implements this operator as startWith
.
You can pass the values you want to prepend to the Observable sequence to
startWith
either in a single Iterable or as a series of one to nine function
parameters.
startWith(Iterable)
startWith(T)
(there are also versions that take up to nine individual items)
You can also pass startWith
an Observable, and it will prepend the emissions
from that Observable to those of the source Observable to make its own set of emissions.
This is a sort of inverted Concat operation.
startWith(Observable)
RxJS implements this operator as startWith
. It accepts a variable number of
function parameters and treats each one as an item that it will prepend to the resulting
Observable sequence in the order they are given in the parameter list.
startWith
is found in the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
RxKotlin implements this operator as startWith
.
You can pass the values you want to prepend to the Observable sequence to
startWith
either in a single Iterable or as a series of one to nine function
parameters.
You can also pass startWith
an Observable, and it will prepend the emissions
from that Observable to those of the source Observable to make its own set of emissions. This
is a sort of inverted Concat operation.
Rx.NET implements this operator as StartWith
. It accepts an array of items
which it prepends to the resulting Observable sequence in the order they appear in the array
before it emits the items from the source Observable.
RxPHP implements this operator as startWith
.
Prepends a value to an observable sequence with an argument of a signal value to prepend.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/startWith/startWith.php $source = \Rx\Observable::just(2) ->startWith(1); $subscription = $source->subscribe($stdoutObserver);
Next value: 1 Next value: 2 Complete!
RxPHP also has an operator startWithArray
.
Prepends a sequence of values to an observable sequence with an argument of an array of values to prepend.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/startWith/startWithArray.php $source = \Rx\Observable::just(4) ->startWithArray([1, 2, 3]); $subscription = $source->subscribe($stdoutObserver);
Next value: 1 Next value: 2 Next value: 3 Next value: 4 Complete!
RxPY implements this operator as start_with
. It accepts an array of items which
it prepends to the resulting Observable sequence in the order they appear in the array before
it emits the items from the source Observable.
Rx.rb implements this operator as start_with
. It accepts an array of items which
it prepends to the resulting Observable sequence in the order they appear in the array before
it emits the items from the source Observable.
RxScala implements this operator with +:
It takes an item and an Observable
as parameters, and prepends the item to the items emitted by the Observable as its own
Observable sequence.