Skip to content

Commit

Permalink
Merge pull request ReactiveX#2329 from Podlas29/bind-node-callback-no…
Browse files Browse the repository at this point in the history
…-callback-arguments

fix(bindNodeCallback): emit undefined when callback has no success ar…
  • Loading branch information
jayphelps authored Feb 14, 2017
2 parents d23add0 + 8b81fc6 commit 589b7f6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 38 additions & 0 deletions spec/observables/bindNodeCallback-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ const Observable = Rx.Observable;
/** @test {bindNodeCallback} */
describe('Observable.bindNodeCallback', () => {
describe('when not scheduled', () => {
it('should emit undefined when callback is called without success arguments', () => {
function callback(cb) {
cb(null);
}

const boundCallback = Observable.bindNodeCallback(callback);
const results = [];

boundCallback()
.subscribe((x: any) => {
results.push(typeof x);
}, null, () => {
results.push('done');
});

expect(results).to.deep.equal(['undefined', 'done']);
});

it('should emit one value from a callback', () => {
function callback(datum, cb) {
cb(null, datum);
Expand Down Expand Up @@ -126,6 +144,26 @@ describe('Observable.bindNodeCallback', () => {
});

describe('when scheduled', () => {
it('should emit undefined when callback is called without success arguments', () => {
function callback(cb) {
cb(null);
}

const boundCallback = Observable.bindNodeCallback(callback, null, rxTestScheduler);
const results = [];

boundCallback()
.subscribe((x: any) => {
results.push(typeof x);
}, null, () => {
results.push('done');
});

rxTestScheduler.flush();

expect(results).to.deep.equal(['undefined', 'done']);
});

it('should emit one value from a callback', () => {
function callback(datum, cb) {
cb(null, datum);
Expand Down
4 changes: 2 additions & 2 deletions src/observable/BoundNodeCallbackObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class BoundNodeCallbackObservable<T> extends Observable<T> {
subject.complete();
}
} else {
subject.next(innerArgs.length === 1 ? innerArgs[0] : innerArgs);
subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
subject.complete();
}
};
Expand Down Expand Up @@ -162,7 +162,7 @@ function dispatch<T>(this: Action<DispatchState<T>>, state: DispatchState<T>) {
self.add(scheduler.schedule(dispatchNext, 0, { value: result, subject }));
}
} else {
const value = innerArgs.length === 1 ? innerArgs[0] : innerArgs;
const value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
self.add(scheduler.schedule(dispatchNext, 0, { value, subject }));
}
};
Expand Down

0 comments on commit 589b7f6

Please sign in to comment.