W3cubDocs

/ReactiveX

Repeat

create an Observable that emits a particular item multiple times
Repeat

The Repeat operator emits an item repeatedly. Some implementations of this operator allow you to repeat a sequence of items, and some permit you to limit the number of repetitions.

See Also

Language-Specific Information

repeat

RxGroovy implements this operator as repeat. It does not initiate an Observable, but operates on an Observable in such a way that it repeats the sequence emitted by the source Observable as its own sequence, either infinitely, or in the case of repeat(n), n times.

repeat operates by default on the trampoline Scheduler. There is also a variant that allows you to set the Scheduler by passing one in as a parameter.

repeatWhen

There is also an operator called repeatWhen. Rather than buffering and replaying the sequence from the source Observable, it resubscribes to and mirrors the source Observable, but only conditionally.

It decides whether to resubscribe and remirror the source Observable by passing that Observable’s termination notifications (error or completed) to a notification handler as void emissions. This notification handler acts as an Observable operator, taking an Observable that emits these void notifications as input, and returning an Observable that emits void items (meaning, resubscribe and mirror the source Observable) or terminates (meaning, terminate the sequence emitted by repeatWhen).

repeatWhen operates by default on the trampoline Scheduler. There is also a variant that allows you to set the Scheduler by passing one in as a parameter.

doWhile

In RxGroovy, doWhile is not part of the standard set of operators, but is part of the optional rxjava-computation-expressions package. doWhile checks a condition after each repetition of the source sequence, and only repeats it if that condition is true.

whileDo

In RxGroovy, whileDo is not part of the standard set of operators, but is part of the optional rxjava-computation-expressions package. whileDo checks a condition before each repetition of the source sequence, and only repeats it if that condition is true.

repeat

RxJava implements this operator as repeat. It does not initiate an Observable, but operates on an Observable in such a way that it repeats the sequence emitted by the source Observable as its own sequence, either infinitely, or in the case of repeat(n), n times.

repeat operates by default on the trampoline Scheduler. There is also a variant that allows you to set the Scheduler by passing one in as a parameter.

repeatWhen

There is also an operator called repeatWhen. Rather than buffering and replaying the sequence from the source Observable, it resubscribes to and mirrors the source Observable, but only conditionally.

It decides whether to resubscribe and remirror the source Observable by passing that Observable’s termination notifications (error or completed) to a notification handler as void emissions. This notification handler acts as an Observable operator, taking an Observable that emits these void notifications as input, and returning an Observable that emits void items (meaning, resubscribe and mirror the source Observable) or terminates (meaning, terminate the sequence emitted by repeatWhen).

repeatWhen operates by default on the trampoline Scheduler. There is also a variant that allows you to set the Scheduler by passing one in as a parameter.

doWhile

In RxJava, doWhile is not part of the standard set of operators, but is part of the optional rxjava-computation-expressions package. doWhile checks a condition after each repetition of the source sequence, and only repeats it if that condition is true.

whileDo

In RxJava, whileDo is not part of the standard set of operators, but is part of the optional rxjava-computation-expressions package. whileDo checks a condition before each repetition of the source sequence, and only repeats it if that condition is true.

repeat

RxJS implements this operator as repeat. It accepts as its parameter the item to repeat, and optionally two other parameters: the number of times you want the item to repeat, and the Scheduler on which you want to perform this operation (it uses the immediate Scheduler by default).

Sample Code

var source = Rx.Observable.repeat(42, 3);

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 42
Next: 42
Next: 42
Completed

repeat is found in the following distributions:

  • rx.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js
doWile

RxJS also implements the doWhile operator. It repeats the source Observable’s sequence of emissions only so long as a condition you specify remains true.

var i = 0;

var source = Rx.Observable.return(42).doWhile(
    function (x) { return ++i < 2; });

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 42
Next: 42
Completed

doWhile is found in each of the following distributions.

  • rx.all.js
  • rx.all.compat.js
  • rx.experimental.js

It requires one of the following distributions:

  • rx.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js
while

RxJS also implements the while operator. It repeats the source Observable’s sequence of emissions only if a condition you specify is true.

var i = 0;

// Repeat until condition no longer holds
var source = Rx.Observable.while(
    function () { return i++ < 3 },
    Rx.Observable.return(42)
);

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 42
Next: 42
Next: 42
Completed

while is found in the rx.experimental.js distribution. It requires one of the following distributions:

  • rx.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js

RxPHP implements this operator as repeat.

Generates an observable sequence that repeats the given element the specified number of times.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/repeat/repeat.php

$source = \Rx\Observable::range(1, 3)
    ->repeat(3);

$subscription = $source->subscribe($createStdoutObserver());
Next value: 1
Next value: 2
Next value: 3
Next value: 1
Next value: 2
Next value: 3
Next value: 1
Next value: 2
Next value: 3
Complete!

RxPHP also has an operator repeatWhen.

Returns an Observable that emits the same values as the source Observable with the exception of an onCompleted. An onCompleted notification from the source will result in the emission of a count item to the Observable provided as an argument to the notificationHandler function. If that Observable calls onComplete or onError then repeatWhen will call onCompleted or onError on the child subscription. Otherwise, this Observable will resubscribe to the source observable.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/repeat/repeatWhen.php

$source = Rx\Observable::of(42)
    ->repeatWhen(function (\Rx\Observable $notifications) {
        return $notifications
            ->scan(function ($acc, $x) {
                return $acc + $x;
            }, 0)
            ->delay(1000)
            ->doOnNext(function () {
                echo "1 second delay", PHP_EOL;
            })
            ->takeWhile(function ($count) {
                return $count < 2;
            });
    });

$subscription = $source->subscribe($createStdoutObserver());
Next value: 42
1 second delay
Next value: 42
1 second delay
Complete!

© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/repeat.html