Skip to content

Commit

Permalink
feat(bindCallback): rename fromCallback to bindCallback
Browse files Browse the repository at this point in the history
since fromCallback does not behave in a manner consistent with other fromX methods, it is being renamed to bindCallback as it returns a bound function that returns an observable

closes #876
  • Loading branch information
benlesh committed Dec 4, 2015
1 parent e1447ac commit 305d66d
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('Observable.fromCallback', function () {
describe('Observable.bindCallback', function () {
it('should emit one value from a callback', function (done) {
function callback(datum, cb) {
cb(datum);
}
var cbToObs = Observable.fromCallback(callback);
var boundCallback = Observable.bindCallback(callback);

cbToObs(42)
boundCallback(42)
.subscribe(function (x) {
expect(x).toBe(42);
}, function () {
Expand All @@ -22,9 +22,9 @@ describe('Observable.fromCallback', function () {
function callback(datum, cb) {
cb(null, datum);
}
var cbToObs = Observable.fromCallback(callback, null, function (err, datum) { return datum; });
var boundCallback = Observable.bindCallback(callback, null, function (err, datum) { return datum; });

cbToObs(42)
boundCallback(42)
.subscribe(function (x) {
expect(x).toBe(42);
}, function () {
Expand All @@ -37,9 +37,9 @@ describe('Observable.fromCallback', function () {
function callback(cb) {
cb(this.value);
}
var cbToObs = Observable.fromCallback(callback, {value: 42});
var boundCallback = Observable.bindCallback(callback, {value: 42});

cbToObs()
boundCallback()
.subscribe(function (x) {
expect(x).toBe(42);
}, function () {
Expand All @@ -52,9 +52,9 @@ describe('Observable.fromCallback', function () {
function callback(cb) {
cb(42);
}
var cbToObs = Observable.fromCallback(callback, null, function (err) { throw new Error('Yikes!'); });
var boundCallback = Observable.bindCallback(callback, null, function (err) { throw new Error('Yikes!'); });

cbToObs()
boundCallback()
.subscribe(function () {
// Considered a failure if we don't go directly to err handler
done.fail('should not be called');
Expand All @@ -81,7 +81,7 @@ describe('Observable.fromCallback', function () {
cb(datum);
});
}
var subscription = Observable.fromCallback(callback)(42)
var subscription = Observable.bindCallback(callback)(42)
.subscribe(nextSpy, throwSpy, completeSpy);
subscription.unsubscribe();

Expand Down
2 changes: 1 addition & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export class Observable<T> implements CoreOperators<T> {
}

// static method stubs
static bindCallback: <T>(callbackFunc: Function, ctx?: Object, selector?: Function, scheduler?: Scheduler) => Function;
static combineLatest: <T>(...observables: Array<Observable<any> |
Array<Observable<any>> |
((...values: Array<any>) => T) |
Expand All @@ -152,7 +153,6 @@ export class Observable<T> implements CoreOperators<T> {
removeHandler: (handler: Function) => void,
selector?: (...args: Array<any>) => T) => Observable<T>;
static fromPromise: <T>(promise: Promise<T>, scheduler?: Scheduler) => Observable<T>;
static fromCallback: <T>(callbackFunc: Function, ctx?: Object, selector?: Function, scheduler?: Scheduler) => Function;
static interval: (interval: number, scheduler?: Scheduler) => Observable<number>;
static merge: <T>(...observables: Array<Observable<any> | Scheduler | number>) => Observable<T>;
static never: <T>() => Observable<T>;
Expand Down
3 changes: 1 addition & 2 deletions src/Rx.KitchenSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ export interface KitchenSinkOperators<T> extends CoreOperators<T> {
import './add/operator/combineLatest-static';
import './add/operator/concat-static';
import './add/operator/merge-static';
import './add/observable/defer';
import './add/observable/bindCallback';
import './add/observable/empty';
import './add/observable/forkJoin';
import './add/observable/from';
import './add/observable/fromArray';
import './add/observable/fromEvent';
import './add/observable/fromEventPattern';
import './add/observable/fromPromise';
import './add/observable/fromCallback';
import './add/observable/interval';
import './add/observable/never';
import './add/observable/range';
Expand Down
3 changes: 1 addition & 2 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import {Observable} from './Observable';
import './add/operator/combineLatest-static';
import './add/operator/concat-static';
import './add/operator/merge-static';
import './add/observable/defer';
import './observable/bindCallback';
import './add/observable/empty';
import './add/observable/forkJoin';
import './add/observable/from';
import './add/observable/fromArray';
import './add/observable/fromEvent';
import './add/observable/fromEventPattern';
import './add/observable/fromPromise';
import './add/observable/fromCallback';
import './add/observable/interval';
import './add/observable/never';
import './add/observable/range';
Expand Down
3 changes: 3 additions & 0 deletions src/add/observable/bindCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {Observable} from '../../Observable';
import {BoundCallbackObsevable} from '../../observable/bindCallback';
Observable.bindCallback = BoundCallbackObsevable.create;
3 changes: 0 additions & 3 deletions src/add/observable/fromCallback.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {immediate} from '../scheduler/immediate';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';

export class CallbackObservable<T> extends Observable<T> {
export class BoundCallbackObservable<T> extends Observable<T> {

_isScalar: boolean = false;
value: T | T[];
Expand All @@ -16,7 +16,7 @@ export class CallbackObservable<T> extends Observable<T> {
selector: Function = undefined,
scheduler: Scheduler = immediate): Function {
return (...args): Observable<T> => {
return new CallbackObservable(callbackFunc, ctx, selector, args, scheduler);
return new BoundCallbackObservable(callbackFunc, ctx, selector, args, scheduler);
};
}

Expand Down

0 comments on commit 305d66d

Please sign in to comment.