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 pathswitch.js
70 lines (60 loc) · 2.33 KB
/
switch.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
var SwitchObservable = (function(__super__) {
inherits(SwitchObservable, __super__);
function SwitchObservable(source) {
this.source = source;
__super__.call(this);
}
SwitchObservable.prototype.subscribeCore = function (o) {
var inner = new SerialDisposable(), s = this.source.subscribe(new SwitchObserver(o, inner));
return new BinaryDisposable(s, inner);
};
inherits(SwitchObserver, AbstractObserver);
function SwitchObserver(o, inner) {
this.o = o;
this.inner = inner;
this.stopped = false;
this.latest = 0;
this.hasLatest = false;
AbstractObserver.call(this);
}
SwitchObserver.prototype.next = function (innerSource) {
var d = new SingleAssignmentDisposable(), id = ++this.latest;
this.hasLatest = true;
this.inner.setDisposable(d);
isPromise(innerSource) && (innerSource = observableFromPromise(innerSource));
d.setDisposable(innerSource.subscribe(new InnerObserver(this, id)));
};
SwitchObserver.prototype.error = function (e) {
this.o.onError(e);
};
SwitchObserver.prototype.completed = function () {
this.stopped = true;
!this.hasLatest && this.o.onCompleted();
};
inherits(InnerObserver, AbstractObserver);
function InnerObserver(parent, id) {
this.parent = parent;
this.id = id;
AbstractObserver.call(this);
}
InnerObserver.prototype.next = function (x) {
this.parent.latest === this.id && this.parent.o.onNext(x);
};
InnerObserver.prototype.error = function (e) {
this.parent.latest === this.id && this.parent.o.onError(e);
};
InnerObserver.prototype.completed = function () {
if (this.parent.latest === this.id) {
this.parent.hasLatest = false;
this.parent.stopped && this.parent.o.onCompleted();
}
};
return SwitchObservable;
}(ObservableBase));
/**
* Transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.
* @returns {Observable} The observable sequence that at any point in time produces the elements of the most recent inner observable sequence that has been received.
*/
observableProto['switch'] = observableProto.switchLatest = function () {
return new SwitchObservable(this);
};