From 8b81fc61cea813ef6cd8e425743a9599d83d3ac4 Mon Sep 17 00:00:00 2001 From: Mateusz Podlasin Date: Sun, 5 Feb 2017 13:49:04 +0100 Subject: [PATCH] fix(bindNodeCallback): emit undefined when callback has no success arguments Emit undefined insteady of empty array by resulting Observable, when callback function is called without success parameters. Closes #2254 --- spec/observables/bindNodeCallback-spec.ts | 38 +++++++++++++++++++ src/observable/BoundNodeCallbackObservable.ts | 4 +- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/spec/observables/bindNodeCallback-spec.ts b/spec/observables/bindNodeCallback-spec.ts index 15a9e53d32..e6e50de25a 100644 --- a/spec/observables/bindNodeCallback-spec.ts +++ b/spec/observables/bindNodeCallback-spec.ts @@ -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); @@ -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); diff --git a/src/observable/BoundNodeCallbackObservable.ts b/src/observable/BoundNodeCallbackObservable.ts index b3b3877df9..8eac5e1533 100644 --- a/src/observable/BoundNodeCallbackObservable.ts +++ b/src/observable/BoundNodeCallbackObservable.ts @@ -112,7 +112,7 @@ export class BoundNodeCallbackObservable extends Observable { subject.complete(); } } else { - subject.next(innerArgs.length === 1 ? innerArgs[0] : innerArgs); + subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs); subject.complete(); } }; @@ -162,7 +162,7 @@ function dispatch(this: Action>, state: DispatchState) { 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 })); } };