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 pathswitchfirst.js
77 lines (66 loc) · 2.39 KB
/
switchfirst.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
var SwitchFirstObservable = (function (__super__) {
inherits(SwitchFirstObservable, __super__);
function SwitchFirstObservable(source) {
this.source = source;
__super__.call(this);
}
SwitchFirstObservable.prototype.subscribeCore = function (o) {
var m = new SingleAssignmentDisposable(),
g = new CompositeDisposable(),
state = {
hasCurrent: false,
isStopped: false,
o: o,
g: g
};
g.add(m);
m.setDisposable(this.source.subscribe(new SwitchFirstObserver(state)));
return g;
};
return SwitchFirstObservable;
}(ObservableBase));
var SwitchFirstObserver = (function(__super__) {
inherits(SwitchFirstObserver, __super__);
function SwitchFirstObserver(state) {
this._s = state;
__super__.call(this);
}
SwitchFirstObserver.prototype.next = function (x) {
if (!this._s.hasCurrent) {
this._s.hasCurrent = true;
isPromise(x) && (x = observableFromPromise(x));
var inner = new SingleAssignmentDisposable();
this._s.g.add(inner);
inner.setDisposable(x.subscribe(new InnerObserver(this._s, inner)));
}
};
SwitchFirstObserver.prototype.error = function (e) {
this._s.o.onError(e);
};
SwitchFirstObserver.prototype.completed = function () {
this._s.isStopped = true;
!this._s.hasCurrent && this._s.g.length === 1 && this._s.o.onCompleted();
};
inherits(InnerObserver, __super__);
function InnerObserver(state, inner) {
this._s = state;
this._i = inner;
__super__.call(this);
}
InnerObserver.prototype.next = function (x) { this._s.o.onNext(x); };
InnerObserver.prototype.error = function (e) { this._s.o.onError(e); };
InnerObserver.prototype.completed = function () {
this._s.g.remove(this._i);
this._s.hasCurrent = false;
this._s.isStopped && this._s.g.length === 1 && this._s.o.onCompleted();
};
return SwitchFirstObserver;
}(AbstractObserver));
/**
* Performs a exclusive waiting for the first to finish before subscribing to another observable.
* Observables that come in between subscriptions will be dropped on the floor.
* @returns {Observable} A exclusive observable with only the results that happen when subscribed.
*/
observableProto.switchFirst = function () {
return new SwitchFirstObservable(this);
};