This repository has been 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 pathmergedelayerror.js
112 lines (97 loc) · 4 KB
/
mergedelayerror.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var CompositeError = Rx.CompositeError = function(errors) {
this.innerErrors = errors;
this.message = 'This contains multiple errors. Check the innerErrors';
Error.call(this);
};
CompositeError.prototype = Object.create(Error.prototype);
CompositeError.prototype.name = 'CompositeError';
var MergeDelayErrorObservable = (function(__super__) {
inherits(MergeDelayErrorObservable, __super__);
function MergeDelayErrorObservable(source) {
this.source = source;
__super__.call(this);
}
MergeDelayErrorObservable.prototype.subscribeCore = function (o) {
var group = new CompositeDisposable(),
m = new SingleAssignmentDisposable(),
state = { isStopped: false, errors: [], o: o };
group.add(m);
m.setDisposable(this.source.subscribe(new MergeDelayErrorObserver(group, state)));
return group;
};
return MergeDelayErrorObservable;
}(ObservableBase));
var MergeDelayErrorObserver = (function(__super__) {
inherits(MergeDelayErrorObserver, __super__);
function MergeDelayErrorObserver(group, state) {
this._group = group;
this._state = state;
__super__.call(this);
}
function setCompletion(o, errors) {
if (errors.length === 0) {
o.onCompleted();
} else if (errors.length === 1) {
o.onError(errors[0]);
} else {
o.onError(new CompositeError(errors));
}
}
MergeDelayErrorObserver.prototype.next = function (x) {
var inner = new SingleAssignmentDisposable();
this._group.add(inner);
// Check for promises support
isPromise(x) && (x = observableFromPromise(x));
inner.setDisposable(x.subscribe(new InnerObserver(inner, this._group, this._state)));
};
MergeDelayErrorObserver.prototype.error = function (e) {
this._state.errors.push(e);
this._state.isStopped = true;
this._group.length === 1 && setCompletion(this._state.o, this._state.errors);
};
MergeDelayErrorObserver.prototype.completed = function () {
this._state.isStopped = true;
this._group.length === 1 && setCompletion(this._state.o, this._state.errors);
};
inherits(InnerObserver, __super__);
function InnerObserver(inner, group, state) {
this._inner = inner;
this._group = group;
this._state = state;
__super__.call(this);
}
InnerObserver.prototype.next = function (x) { this._state.o.onNext(x); };
InnerObserver.prototype.error = function (e) {
this._state.errors.push(e);
this._group.remove(this._inner);
this._state.isStopped && this._group.length === 1 && setCompletion(this._state.o, this._state.errors);
};
InnerObserver.prototype.completed = function () {
this._group.remove(this._inner);
this._state.isStopped && this._group.length === 1 && setCompletion(this._state.o, this._state.errors);
};
return MergeDelayErrorObserver;
}(AbstractObserver));
/**
* Flattens an Observable that emits Observables into one Observable, in a way that allows an Observer to
* receive all successfully emitted items from all of the source Observables without being interrupted by
* an error notification from one of them.
*
* This behaves like Observable.prototype.mergeAll except that if any of the merged Observables notify of an
* error via the Observer's onError, mergeDelayError will refrain from propagating that
* error notification until all of the merged Observables have finished emitting items.
* @param {Array | Arguments} args Arguments or an array to merge.
* @returns {Observable} an Observable that emits all of the items emitted by the Observables emitted by the Observable
*/
Observable.mergeDelayError = function() {
var args;
if (Array.isArray(arguments[0])) {
args = arguments[0];
} else {
var len = arguments.length;
args = new Array(len);
for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
}
var source = observableOf(null, args);
return new MergeDelayErrorObservable(source);
};