This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtake.js
45 lines (38 loc) · 1.61 KB
/
take.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var TakeObservable = (function(__super__) {
inherits(TakeObservable, __super__);
function TakeObservable(source, count) {
this.source = source;
this._count = count;
__super__.call(this);
}
TakeObservable.prototype.subscribeCore = function (o) {
return this.source.subscribe(new TakeObserver(o, this._count));
};
function TakeObserver(o, c) {
this._o = o;
this._c = c;
this._r = c;
AbstractObserver.call(this);
}
inherits(TakeObserver, AbstractObserver);
TakeObserver.prototype.next = function (x) {
if (this._r-- > 0) {
this._o.onNext(x);
this._r <= 0 && this._o.onCompleted();
}
};
TakeObserver.prototype.error = function (e) { this._o.onError(e); };
TakeObserver.prototype.completed = function () { this._o.onCompleted(); };
return TakeObservable;
}(ObservableBase));
/**
* Returns a specified number of contiguous elements from the start of an observable sequence, using the specified scheduler for the edge case of take(0).
* @param {Number} count The number of elements to return.
* @param {Scheduler} [scheduler] Scheduler used to produce an OnCompleted message in case <paramref name="count count</paramref> is set to 0.
* @returns {Observable} An observable sequence that contains the specified number of elements from the start of the input sequence.
*/
observableProto.take = function (count, scheduler) {
if (count < 0) { throw new ArgumentOutOfRangeError(); }
if (count === 0) { return observableEmpty(scheduler); }
return new TakeObservable(this, count);
};