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 pathzipiterable.js
96 lines (80 loc) · 3.2 KB
/
zipiterable.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
function falseFactory() { return false; }
function emptyArrayFactory() { return []; }
function argumentsToArray() {
var len = arguments.length, args = new Array(len);
for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
return args;
}
var ZipIterableObservable = (function(__super__) {
inherits(ZipIterableObservable, __super__);
function ZipIterableObservable(sources, cb) {
this.sources = sources;
this._cb = cb;
__super__.call(this);
}
ZipIterableObservable.prototype.subscribeCore = function (o) {
var sources = this.sources, len = sources.length, subscriptions = new Array(len);
var state = {
q: arrayInitialize(len, emptyArrayFactory),
done: arrayInitialize(len, falseFactory),
cb: this._cb,
o: o
};
for (var i = 0; i < len; i++) {
(function (i) {
var source = sources[i], sad = new SingleAssignmentDisposable();
(isArrayLike(source) || isIterable(source)) && (source = observableFrom(source));
subscriptions[i] = sad;
sad.setDisposable(source.subscribe(new ZipIterableObserver(state, i)));
}(i));
}
return new NAryDisposable(subscriptions);
};
return ZipIterableObservable;
}(ObservableBase));
var ZipIterableObserver = (function (__super__) {
inherits(ZipIterableObserver, __super__);
function ZipIterableObserver(s, i) {
this._s = s;
this._i = i;
__super__.call(this);
}
function notEmpty(x) { return x.length > 0; }
function shiftEach(x) { return x.shift(); }
function notTheSame(i) {
return function (x, j) {
return j !== i;
};
}
ZipIterableObserver.prototype.next = function (x) {
this._s.q[this._i].push(x);
if (this._s.q.every(notEmpty)) {
var queuedValues = this._s.q.map(shiftEach),
res = tryCatch(this._s.cb).apply(null, queuedValues);
if (res === errorObj) { return this._s.o.onError(res.e); }
this._s.o.onNext(res);
} else if (this._s.done.filter(notTheSame(this._i)).every(identity)) {
this._s.o.onCompleted();
}
};
ZipIterableObserver.prototype.error = function (e) { this._s.o.onError(e); };
ZipIterableObserver.prototype.completed = function () {
this._s.done[this._i] = true;
this._s.done.every(identity) && this._s.o.onCompleted();
};
return ZipIterableObserver;
}(AbstractObserver));
/**
* Merges the specified observable sequences into one observable sequence by using the selector function whenever all of the observable sequences or an array have produced an element at a corresponding index.
* The last element in the arguments must be a function to invoke for each series of elements at corresponding indexes in the args.
* @returns {Observable} An observable sequence containing the result of combining elements of the args using the specified result selector function.
*/
observableProto.zipIterable = function () {
if (arguments.length === 0) { throw new Error('invalid arguments'); }
var len = arguments.length, args = new Array(len);
for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
var resultSelector = isFunction(args[len - 1]) ? args.pop() : argumentsToArray;
var parent = this;
args.unshift(parent);
return new ZipIterableObservable(args, resultSelector);
};