Skip to content

Commit

Permalink
Merge pull request #2328 from Podlas29/bind-callback-no-callback-argu…
Browse files Browse the repository at this point in the history
…ments

fix(bindCallback): emit undefined when callback is without arguments
  • Loading branch information
jayphelps authored Feb 14, 2017
2 parents 589b7f6 + 915a2a8 commit 1d27ea2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
36 changes: 36 additions & 0 deletions spec/observables/bindCallback-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/observable/BoundCallbackObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class BoundCallbackObservable<T> extends Observable<T> {
subject: AsyncSubject<T>;

/* tslint:disable:max-line-length */
static create(callbackFunc: (callback: () => any) => any, selector?: void, scheduler?: IScheduler): () => Observable<void>;
static create<R>(callbackFunc: (callback: (result: R) => any) => any, selector?: void, scheduler?: IScheduler): () => Observable<R>;
static create<T, R>(callbackFunc: (v1: T, callback: (result: R) => any) => any, selector?: void, scheduler?: IScheduler): (v1: T) => Observable<R>;
static create<T, T2, R>(callbackFunc: (v1: T, v2: T2, callback: (result: R) => any) => any, selector?: void, scheduler?: IScheduler): (v1: T, v2: T2) => Observable<R>;
Expand Down Expand Up @@ -119,7 +120,7 @@ export class BoundCallbackObservable<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 @@ -157,7 +158,7 @@ export class BoundCallbackObservable<T> extends Observable<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 1d27ea2

Please sign in to comment.