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 pathslice.js
59 lines (52 loc) · 1.96 KB
/
slice.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
var SliceObservable = (function (__super__) {
inherits(SliceObservable, __super__);
function SliceObservable(source, b, e) {
this.source = source;
this._b = b;
this._e = e;
__super__.call(this);
}
SliceObservable.prototype.subscribeCore = function (o) {
return this.source.subscribe(new SliceObserver(o, this._b, this._e));
};
return SliceObservable;
}(ObservableBase));
var SliceObserver = (function (__super__) {
inherits(SliceObserver, __super__);
function SliceObserver(o, b, e) {
this._o = o;
this._b = b;
this._e = e;
this._i = 0;
__super__.call(this);
}
SliceObserver.prototype.next = function (x) {
if (this._i >= this._b) {
if (this._e === this._i) {
this._o.onCompleted();
} else {
this._o.onNext(x);
}
}
this._i++;
};
SliceObserver.prototype.error = function (e) { this._o.onError(e); };
SliceObserver.prototype.completed = function () { this._o.onCompleted(); };
return SliceObserver;
}(AbstractObserver));
/*
* The slice() method returns a shallow copy of a portion of an Observable into a new Observable object.
* Unlike the array version, this does not support negative numbers for being or end.
* @param {Number} [begin] Zero-based index at which to begin extraction. If omitted, this will default to zero.
* @param {Number} [end] Zero-based index at which to end extraction. slice extracts up to but not including end.
* If omitted, this will emit the rest of the Observable object.
* @returns {Observable} A shallow copy of a portion of an Observable into a new Observable object.
*/
observableProto.slice = function (begin, end) {
var start = begin || 0;
if (start < 0) { throw new Rx.ArgumentOutOfRangeError(); }
if (typeof end === 'number' && end < start) {
throw new Rx.ArgumentOutOfRangeError();
}
return new SliceObservable(this, start, end);
};