This repository was 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 pathlast.js
79 lines (73 loc) · 2.33 KB
/
last.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var LastObservable = (function (__super__) {
inherits(LastObservable, __super__);
function LastObservable(source, obj) {
this.source = source;
this._obj = obj;
__super__.call(this);
}
LastObservable.prototype.subscribeCore = function (o) {
return this.source.subscribe(new LastObserver(o, this._obj, this.source));
};
return LastObservable;
}(ObservableBase));
var LastObserver = (function(__super__) {
inherits(LastObserver, __super__);
function LastObserver(o, obj, s) {
this._o = o;
this._obj = obj;
this._s = s;
this._i = 0;
this._hv = false;
this._v = null;
__super__.call(this);
}
LastObserver.prototype.next = function (x) {
var shouldYield = false;
if (this._obj.predicate) {
var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
if (res === errorObj) { return this._o.onError(res.e); }
Boolean(res) && (shouldYield = true);
} else if (!this._obj.predicate) {
shouldYield = true;
}
if (shouldYield) {
this._hv = true;
this._v = x;
}
};
LastObserver.prototype.error = function (e) { this._o.onError(e); };
LastObserver.prototype.completed = function () {
if (this._hv) {
this._o.onNext(this._v);
this._o.onCompleted();
}
else if (this._obj.defaultValue === undefined) {
this._o.onError(new EmptyError());
} else {
this._o.onNext(this._obj.defaultValue);
this._o.onCompleted();
}
};
return LastObserver;
}(AbstractObserver));
/**
* Returns the last element of an observable sequence that satisfies the condition in the predicate if specified, else the last element.
* @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate.
*/
observableProto.last = function () {
var obj = {}, source = this;
if (typeof arguments[0] === 'object') {
obj = arguments[0];
} else {
obj = {
predicate: arguments[0],
thisArg: arguments[1],
defaultValue: arguments[2]
};
}
if (isFunction (obj.predicate)) {
var fn = obj.predicate;
obj.predicate = bindCallback(fn, obj.thisArg, 3);
}
return new LastObservable(this, obj);
};