|
| 1 | +"use strict"; |
| 2 | +function noop() {} |
| 3 | + |
| 4 | +function toRxNewObserver(observer) { |
| 5 | + var onNext = observer.onNext; |
| 6 | + var onError = observer.onError; |
| 7 | + var onCompleted = observer.onCompleted; |
| 8 | + if ( |
| 9 | + typeof onNext !== "function" && |
| 10 | + typeof onError !== "function" && |
| 11 | + typeof onCompleted !== "function" |
| 12 | + ) { |
| 13 | + return observer; |
| 14 | + } |
| 15 | + // old observer! |
| 16 | + return { |
| 17 | + next: typeof onNext === "function" |
| 18 | + ? function(x) { |
| 19 | + this.destination.onNext(x); |
| 20 | + } |
| 21 | + : noop, |
| 22 | + error: typeof onError === "function" |
| 23 | + ? function(err) { |
| 24 | + this.destination.onError(err); |
| 25 | + } |
| 26 | + : noop, |
| 27 | + complete: typeof onCompleted === "function" |
| 28 | + ? function() { |
| 29 | + this.destination.onCompleted(); |
| 30 | + } |
| 31 | + : noop, |
| 32 | + destination: observer |
| 33 | + }; |
| 34 | +} |
| 35 | + |
1 | 36 | // WHY NOT BOTH?
|
2 | 37 | module.exports = function rxNewToRxNewAndOld(rxNewObservable) {
|
3 |
| - var _subscribe = rxNewObservable.subscribe; |
| 38 | + var _subscribe = rxNewObservable.subscribe; |
4 | 39 |
|
5 |
| - rxNewObservable.subscribe = function (observerOrNextFn, errFn, compFn) { |
6 |
| - var subscription; |
7 |
| - switch (typeof observerOrNextFn) { |
8 |
| - case 'function': |
9 |
| - subscription = _subscribe.call(this, |
10 |
| - observerOrNextFn, errFn, compFn); |
11 |
| - break; |
12 |
| - case 'object': |
13 |
| - var observer = observerOrNextFn; |
14 |
| - if (typeof observerOrNextFn.onNext === 'function') { |
15 |
| - // old observer! |
16 |
| - observer = { |
17 |
| - next: function (x) { |
18 |
| - var destination = this.destination; |
19 |
| - destination.onNext(x); |
20 |
| - }, |
21 |
| - error: function (err) { |
22 |
| - var destination = this.destination; |
23 |
| - if (destination.onError) { |
24 |
| - destination.onError(err); |
25 |
| - } |
26 |
| - }, |
27 |
| - complete: function () { |
28 |
| - var destination = this.destination; |
29 |
| - if (destination.onCompleted) { |
30 |
| - destination.onCompleted(); |
31 |
| - } |
32 |
| - }, |
33 |
| - destination: observerOrNextFn |
34 |
| - } |
35 |
| - } |
36 |
| - subscription = _subscribe.call(this, observer); |
37 |
| - break; |
38 |
| - case 'undefined': |
39 |
| - subscription = _subscribe.call(this); |
40 |
| - break; |
41 |
| - default: |
42 |
| - throw new TypeError('cannot subscribe to observable with ' + |
43 |
| - 'type ' + typeof observerOrNextFn); |
44 |
| - } |
| 40 | + rxNewObservable.subscribe = function(observerOrNextFn, errFn, compFn) { |
| 41 | + var subscription; |
| 42 | + if (typeof observerOrNextFn !== "object" || observerOrNextFn === null) { |
| 43 | + subscription = _subscribe.call( |
| 44 | + this, |
| 45 | + observerOrNextFn, |
| 46 | + errFn, |
| 47 | + compFn |
| 48 | + ); |
| 49 | + } else { |
| 50 | + var observer = toRxNewObserver(observerOrNextFn); |
| 51 | + subscription = _subscribe.call(this, observer); |
| 52 | + } |
45 | 53 |
|
46 |
| - var _unsubscribe = subscription.unsubscribe; |
| 54 | + var _unsubscribe = subscription.unsubscribe; |
47 | 55 |
|
48 |
| - subscription.unsubscribe = subscription.dispose = function () { |
49 |
| - this.isDisposed = true; |
50 |
| - _unsubscribe.call(subscription); |
51 |
| - }; |
| 56 | + subscription.unsubscribe = subscription.dispose = function() { |
| 57 | + this.isDisposed = true; |
| 58 | + _unsubscribe.call(this); |
| 59 | + }; |
52 | 60 |
|
53 |
| - return subscription; |
54 |
| - } |
| 61 | + return subscription; |
| 62 | + }; |
55 | 63 |
|
56 | 64 | return rxNewObservable;
|
57 |
| -} |
| 65 | +}; |
0 commit comments