UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

84 lines 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Observable_1 = require("../Observable"); /** * Creates an Observable that emits a sequence of numbers within a specified * range. * * <span class="informal">Emits a sequence of numbers in a range.</span> * * ![](range.png) * * `range` operator emits a range of sequential integers, in order, where you * select the `start` of the range and its `length`. By default, uses no * {@link SchedulerLike} and just delivers the notifications synchronously, but may use * an optional {@link SchedulerLike} to regulate those deliveries. * * ## Example * Emits the numbers 1 to 10</caption> * ```ts * import { range } from 'rxjs'; * * const numbers = range(1, 10); * numbers.subscribe(x => console.log(x)); * ``` * @see {@link timer} * @see {@link index/interval} * * @param {number} [start=0] The value of the first integer in the sequence. * @param {number} count The number of sequential integers to generate. * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} to use for scheduling * the emissions of the notifications. * @return {Observable} An Observable of numbers that emits a finite range of * sequential integers. * @static true * @name range * @owner Observable */ function range(start, count, scheduler) { if (start === void 0) { start = 0; } return new Observable_1.Observable(function (subscriber) { if (count === undefined) { count = start; start = 0; } var index = 0; var current = start; if (scheduler) { return scheduler.schedule(dispatch, 0, { index: index, count: count, start: start, subscriber: subscriber }); } else { do { if (index++ >= count) { subscriber.complete(); break; } subscriber.next(current++); if (subscriber.closed) { break; } } while (true); } return undefined; }); } exports.range = range; /** @internal */ function dispatch(state) { var start = state.start, index = state.index, count = state.count, subscriber = state.subscriber; if (index >= count) { subscriber.complete(); return; } subscriber.next(start); if (subscriber.closed) { return; } state.index = index + 1; state.start = start + 1; this.schedule(state); } exports.dispatch = dispatch; //# sourceMappingURL=range.js.map