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 pathtakewhile.js
54 lines (47 loc) · 1.92 KB
/
takewhile.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
46
47
48
49
50
51
52
53
54
var TakeWhileObservable = (function (__super__) {
inherits(TakeWhileObservable, __super__);
function TakeWhileObservable(source, fn) {
this.source = source;
this._fn = fn;
__super__.call(this);
}
TakeWhileObservable.prototype.subscribeCore = function (o) {
return this.source.subscribe(new TakeWhileObserver(o, this));
};
return TakeWhileObservable;
}(ObservableBase));
var TakeWhileObserver = (function (__super__) {
inherits(TakeWhileObserver, __super__);
function TakeWhileObserver(o, p) {
this._o = o;
this._p = p;
this._i = 0;
this._r = true;
__super__.call(this);
}
TakeWhileObserver.prototype.next = function (x) {
if (this._r) {
this._r = tryCatch(this._p._fn)(x, this._i++, this._p);
if (this._r === errorObj) { return this._o.onError(this._r.e); }
}
if (this._r) {
this._o.onNext(x);
} else {
this._o.onCompleted();
}
};
TakeWhileObserver.prototype.error = function (e) { this._o.onError(e); };
TakeWhileObserver.prototype.completed = function () { this._o.onCompleted(); };
return TakeWhileObserver;
}(AbstractObserver));
/**
* Returns elements from an observable sequence as long as a specified condition is true.
* The element's index is used in the logic of the predicate function.
* @param {Function} predicate A function to test each element for a condition; the second parameter of the function represents the index of the source element.
* @param {Any} [thisArg] Object to use as this when executing callback.
* @returns {Observable} An observable sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.
*/
observableProto.takeWhile = function (predicate, thisArg) {
var fn = bindCallback(predicate, thisArg, 3);
return new TakeWhileObservable(this, fn);
};