UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

126 lines 4.09 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var Subscriber_1 = require("../Subscriber"); var empty_1 = require("../observable/empty"); /** * Returns an Observable that will resubscribe to the source stream when the source stream completes, at most count times. * * <span class="informal">Repeats all values emitted on the source. It's like {@link retry}, but for non error cases.</span> * * ![](repeat.png) * * Similar to {@link retry}, this operator repeats the stream of items emitted by the source for non error cases. * Repeat can be useful for creating observables that are meant to have some repeated pattern or rhythm. * * Note: `repeat(0)` returns an empty observable and `repeat()` will repeat forever * * ## Example * Repeat a message stream * ```ts * import { of } from 'rxjs'; * import { repeat, delay } from 'rxjs/operators'; * * const source = of('Repeat message'); * const example = source.pipe(repeat(3)); * example.subscribe(x => console.log(x)); * * // Results * // Repeat message * // Repeat message * // Repeat message * ``` * * Repeat 3 values, 2 times * ```ts * import { interval } from 'rxjs'; * import { repeat, take } from 'rxjs/operators'; * * const source = interval(1000); * const example = source.pipe(take(3), repeat(2)); * example.subscribe(x => console.log(x)); * * // Results every second * // 0 * // 1 * // 2 * // 0 * // 1 * // 2 * ``` * * @see {@link repeatWhen} * @see {@link retry} * * @param {number} [count] The number of times the source Observable items are repeated, a count of 0 will yield * an empty Observable. * @return {Observable} An Observable that will resubscribe to the source stream when the source stream completes * , at most count times. * @method repeat * @owner Observable */ function repeat(count) { if (count === void 0) { count = -1; } return function (source) { if (count === 0) { return empty_1.empty(); } else if (count < 0) { return source.lift(new RepeatOperator(-1, source)); } else { return source.lift(new RepeatOperator(count - 1, source)); } }; } exports.repeat = repeat; var RepeatOperator = /** @class */ (function () { function RepeatOperator(count, source) { this.count = count; this.source = source; } RepeatOperator.prototype.call = function (subscriber, source) { return source.subscribe(new RepeatSubscriber(subscriber, this.count, this.source)); }; return RepeatOperator; }()); /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var RepeatSubscriber = /** @class */ (function (_super) { __extends(RepeatSubscriber, _super); function RepeatSubscriber(destination, count, source) { var _this = _super.call(this, destination) || this; _this.count = count; _this.source = source; return _this; } RepeatSubscriber.prototype.complete = function () { if (!this.isStopped) { var _a = this, source = _a.source, count = _a.count; if (count === 0) { return _super.prototype.complete.call(this); } else if (count > -1) { this.count = count - 1; } source.subscribe(this._unsubscribeAndRecycle()); } }; return RepeatSubscriber; }(Subscriber_1.Subscriber)); //# sourceMappingURL=repeat.js.map