Skip to content

Commit

Permalink
fix(bindNodeCallback): emit undefined when callback has no success ar…
Browse files Browse the repository at this point in the history
…guments

Emit undefined insteady of empty array by resulting Observable,
when callback function is called without success parameters.

Closes #2254
  • Loading branch information
mpodlasin committed Feb 6, 2017
1 parent 6ce4773 commit 8b81fc6
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 8b81fc6

Please sign in to comment.