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 pathelementat.js
55 lines (48 loc) · 1.77 KB
/
elementat.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
var ElementAtObservable = (function (__super__) {
inherits(ElementAtObservable, __super__);
function ElementAtObservable(source, i, d) {
this.source = source;
this._i = i;
this._d = d;
__super__.call(this);
}
ElementAtObservable.prototype.subscribeCore = function (o) {
return this.source.subscribe(new ElementAtObserver(o, this._i, this._d));
};
return ElementAtObservable;
}(ObservableBase));
var ElementAtObserver = (function (__super__) {
inherits(ElementAtObserver, __super__);
function ElementAtObserver(o, i, d) {
this._o = o;
this._i = i;
this._d = d;
__super__.call(this);
}
ElementAtObserver.prototype.next = function (x) {
if (this._i-- === 0) {
this._o.onNext(x);
this._o.onCompleted();
}
};
ElementAtObserver.prototype.error = function (e) { this._o.onError(e); };
ElementAtObserver.prototype.completed = function () {
if (this._d === undefined) {
this._o.onError(new ArgumentOutOfRangeError());
} else {
this._o.onNext(this._d);
this._o.onCompleted();
}
};
return ElementAtObserver;
}(AbstractObserver));
/**
* Returns the element at a specified index in a sequence or default value if not found.
* @param {Number} index The zero-based index of the element to retrieve.
* @param {Any} [defaultValue] The default value to use if elementAt does not find a value.
* @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence.
*/
observableProto.elementAt = function (index, defaultValue) {
if (index < 0) { throw new ArgumentOutOfRangeError(); }
return new ElementAtObservable(this, index, defaultValue);
};