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 pathtomap.js
60 lines (51 loc) · 1.9 KB
/
tomap.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
var ToMapObservable = (function (__super__) {
inherits(ToMapObservable, __super__);
function ToMapObservable(source, k, e) {
this.source = source;
this._k = k;
this._e = e;
__super__.call(this);
}
ToMapObservable.prototype.subscribeCore = function (o) {
return this.source.subscribe(new ToMapObserver(o, this._k, this._e));
};
return ToMapObservable;
}(ObservableBase));
var ToMapObserver = (function (__super__) {
inherits(ToMapObserver, __super__);
function ToMapObserver(o, k, e) {
this._o = o;
this._k = k;
this._e = e;
this._m = new root.Map();
__super__.call(this);
}
ToMapObserver.prototype.next = function (x) {
var key = tryCatch(this._k)(x);
if (key === errorObj) { return this._o.onError(key.e); }
var elem = x;
if (this._e) {
elem = tryCatch(this._e)(x);
if (elem === errorObj) { return this._o.onError(elem.e); }
}
this._m.set(key, elem);
};
ToMapObserver.prototype.error = function (e) {
this._o.onError(e);
};
ToMapObserver.prototype.completed = function () {
this._o.onNext(this._m);
this._o.onCompleted();
};
return ToMapObserver;
}(AbstractObserver));
/**
* Converts the observable sequence to a Map if it exists.
* @param {Function} keySelector A function which produces the key for the Map.
* @param {Function} [elementSelector] An optional function which produces the element for the Map. If not present, defaults to the value from the observable sequence.
* @returns {Observable} An observable sequence with a single value of a Map containing the values from the observable sequence.
*/
observableProto.toMap = function (keySelector, elementSelector) {
if (typeof root.Map === 'undefined') { throw new TypeError(); }
return new ToMapObservable(this, keySelector, elementSelector);
};