UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

114 lines 3.7 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 ArgumentOutOfRangeError_1 = require("../util/ArgumentOutOfRangeError"); var empty_1 = require("../observable/empty"); /** * Emits only the first `count` values emitted by the source Observable. * * <span class="informal">Takes the first `count` values from the source, then * completes.</span> * * ![](take.png) * * `take` returns an Observable that emits only the first `count` values emitted * by the source Observable. If the source emits fewer than `count` values then * all of its values are emitted. After that, it completes, regardless if the * source completes. * * ## Example * Take the first 5 seconds of an infinite 1-second interval Observable * ```ts * import { interval } from 'rxjs'; * import { take } from 'rxjs/operators'; * * const intervalCount = interval(1000); * const takeFive = intervalCount.pipe(take(5)); * takeFive.subscribe(x => console.log(x)); * * // Logs: * // 0 * // 1 * // 2 * // 3 * // 4 * ``` * * @see {@link takeLast} * @see {@link takeUntil} * @see {@link takeWhile} * @see {@link skip} * * @throws {ArgumentOutOfRangeError} When using `take(i)`, it delivers an * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`. * * @param {number} count The maximum number of `next` values to emit. * @return {Observable<T>} An Observable that emits only the first `count` * values emitted by the source Observable, or all of the values from the source * if the source emits fewer than `count` values. * @method take * @owner Observable */ function take(count) { return function (source) { if (count === 0) { return empty_1.empty(); } else { return source.lift(new TakeOperator(count)); } }; } exports.take = take; var TakeOperator = /** @class */ (function () { function TakeOperator(total) { this.total = total; if (this.total < 0) { throw new ArgumentOutOfRangeError_1.ArgumentOutOfRangeError; } } TakeOperator.prototype.call = function (subscriber, source) { return source.subscribe(new TakeSubscriber(subscriber, this.total)); }; return TakeOperator; }()); /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var TakeSubscriber = /** @class */ (function (_super) { __extends(TakeSubscriber, _super); function TakeSubscriber(destination, total) { var _this = _super.call(this, destination) || this; _this.total = total; _this.count = 0; return _this; } TakeSubscriber.prototype._next = function (value) { var total = this.total; var count = ++this.count; if (count <= total) { this.destination.next(value); if (count === total) { this.destination.complete(); this.unsubscribe(); } } }; return TakeSubscriber; }(Subscriber_1.Subscriber)); //# sourceMappingURL=take.js.map