Movatterモバイル変換


[0]ホーム

URL:


ReactiveX
  1. Operators
  2. Creating
  3. Repeat

Repeat

create an Observable that emits a particular item multiple times

Repeat

TheRepeat operator emits an item repeatedly. Some implementations of this operator allow you to repeat asequence of items, and some permit you to limit the number of repetitions.

See Also

Language-Specific Information:

repeat

RxGroovy implements this operator asrepeat. 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 ofrepeat(n),n times.

repeat operates by default on thetrampolineScheduler. 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 calledrepeatWhen. Rather than buffering and replaying the sequence from the source Observable, itresubscribes 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 asvoid emissions. This notification handler acts as an Observable operator, taking an Observable that emits thesevoid notifications as input, and returning an Observable that emitsvoid items (meaning, resubscribe and mirror the source Observable) or terminates (meaning, terminate the sequence emitted byrepeatWhen).

repeatWhen operates by default on thetrampolineScheduler. 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 optionalrxjava-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 optionalrxjava-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 asrepeat. 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 ofrepeat(n),n times.

repeat operates by default on thetrampolineScheduler. 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 calledrepeatWhen. Rather than buffering and replaying the sequence from the source Observable, itresubscribes 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 asvoid emissions. This notification handler acts as an Observable operator, taking an Observable that emits thesevoid notifications as input, and returning an Observable that emitsvoid items (meaning, resubscribe and mirror the source Observable) or terminates (meaning, terminate the sequence emitted byrepeatWhen).

repeatWhen operates by default on thetrampolineScheduler. 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 optionalrxjava-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 optionalrxjava-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 asrepeat. 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 theScheduler on which you want to perform this operation (it uses theimmediate 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: 42Next: 42Next: 42Completed

repeat is found in the following distributions:

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

RxJS also implements thedoWhile 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: 42Next: 42Completed

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 thewhile 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 holdsvar 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: 42Next: 42Next: 42Completed

while is found in therx.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 asrepeat.

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: 1Next value: 2Next value: 3Next value: 1Next value: 2Next value: 3Next value: 1Next value: 2Next value: 3Complete!

RxPHP also has an operatorrepeatWhen.

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: 421 second delayNext value: 421 second delayComplete!

[8]ページ先頭

©2009-2026 Movatter.jp