Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bindNodeCallback): emit undefined when callback has no success ar… #2329

Merged
merged 1 commit into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curiosity, is there reason to push type instead of actual value to assert with undefined directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess just habit of "everyone can set undefined value, so never trust it". All JS resources used to be very paranoid when I was learning. ;)

}, 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