Skip to content

Commit f407745

Browse files
committed
Make rxNewToRxNewAndOld work with old observers missing onNext.
1 parent 072bc07 commit f407745

File tree

2 files changed

+72
-49
lines changed

2 files changed

+72
-49
lines changed
Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,65 @@
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+
136
// WHY NOT BOTH?
237
module.exports = function rxNewToRxNewAndOld(rxNewObservable) {
3-
var _subscribe = rxNewObservable.subscribe;
38+
var _subscribe = rxNewObservable.subscribe;
439

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+
}
4553

46-
var _unsubscribe = subscription.unsubscribe;
54+
var _unsubscribe = subscription.unsubscribe;
4755

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+
};
5260

53-
return subscription;
54-
}
61+
return subscription;
62+
};
5563

5664
return rxNewObservable;
57-
}
65+
};

test/unit/internal/rxNewToRxNewAndOld.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ describe('rxNewToRxNewAndOld', function () {
2727
expect(results).to.deep.equal([1, 2, 3, 'done']);
2828
});
2929

30+
it('should work with partial "old" observers', function () {
31+
var source = Observable.of(1, 2, 3);
32+
var results = [];
33+
34+
var sub = rxNewToRxNewAndOld(source).subscribe({
35+
onCompleted: function () {
36+
results.push('done');
37+
}
38+
});
39+
40+
expect(sub.dispose).to.be.a('function');
41+
expect(sub.unsubscribe).to.be.a('function');
42+
expect(results).to.deep.equal(['done']);
43+
});
44+
3045
it('should work with "new" observers', function () {
3146
var source = Observable.of(1, 2, 3);
3247
var results = [];

0 commit comments

Comments
 (0)