From 915a2a84cbc0342f0d011ae8373065cc034b2371 Mon Sep 17 00:00:00 2001 From: Mateusz Podlasin Date: Sun, 5 Feb 2017 13:26:37 +0100 Subject: [PATCH] fix(bindCallback): emit undefined when callback is without arguments In resulting Observable emit undefined when callback is called without parameters, instead of emitting empty array. --- spec/observables/bindCallback-spec.ts | 36 +++++++++++++++++++++++ src/observable/BoundCallbackObservable.ts | 5 ++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/spec/observables/bindCallback-spec.ts b/spec/observables/bindCallback-spec.ts index cae44820a0..59c92ab1e0 100644 --- a/spec/observables/bindCallback-spec.ts +++ b/spec/observables/bindCallback-spec.ts @@ -8,6 +8,23 @@ const Observable = Rx.Observable; /** @test {bindCallback} */ describe('Observable.bindCallback', () => { describe('when not scheduled', () => { + it('should emit undefined from a callback without arguments', () => { + function callback(cb) { + cb(); + } + const boundCallback = Observable.bindCallback(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(datum); @@ -104,6 +121,25 @@ describe('Observable.bindCallback', () => { }); describe('when scheduled', () => { + it('should emit undefined from a callback without arguments', () => { + function callback(cb) { + cb(); + } + const boundCallback = Observable.bindCallback(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(datum); diff --git a/src/observable/BoundCallbackObservable.ts b/src/observable/BoundCallbackObservable.ts index bdcc87a178..dcc95cac5e 100644 --- a/src/observable/BoundCallbackObservable.ts +++ b/src/observable/BoundCallbackObservable.ts @@ -15,6 +15,7 @@ export class BoundCallbackObservable extends Observable { subject: AsyncSubject; /* tslint:disable:max-line-length */ + static create(callbackFunc: (callback: () => any) => any, selector?: void, scheduler?: IScheduler): () => Observable; static create(callbackFunc: (callback: (result: R) => any) => any, selector?: void, scheduler?: IScheduler): () => Observable; static create(callbackFunc: (v1: T, callback: (result: R) => any) => any, selector?: void, scheduler?: IScheduler): (v1: T) => Observable; static create(callbackFunc: (v1: T, v2: T2, callback: (result: R) => any) => any, selector?: void, scheduler?: IScheduler): (v1: T, v2: T2) => Observable; @@ -119,7 +120,7 @@ export class BoundCallbackObservable extends Observable { subject.complete(); } } else { - subject.next(innerArgs.length === 1 ? innerArgs[0] : innerArgs); + subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs); subject.complete(); } }; @@ -157,7 +158,7 @@ export class BoundCallbackObservable extends Observable { 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 })); } };