From 305d66d0fc54c4bf83d5a190864f8f76252f2e43 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Thu, 3 Dec 2015 13:29:35 -0800 Subject: [PATCH] feat(bindCallback): rename fromCallback to bindCallback 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 --- ...-callback-spec.js => bindCallback-spec.js} | 20 +++++++++---------- src/Observable.ts | 2 +- src/Rx.KitchenSink.ts | 3 +-- src/Rx.ts | 3 +-- src/add/observable/bindCallback.ts | 3 +++ src/add/observable/fromCallback.ts | 3 --- .../{fromCallback.ts => bindCallback.ts} | 4 ++-- 7 files changed, 18 insertions(+), 20 deletions(-) rename spec/observables/{from-callback-spec.js => bindCallback-spec.js} (80%) create mode 100644 src/add/observable/bindCallback.ts delete mode 100644 src/add/observable/fromCallback.ts rename src/observable/{fromCallback.ts => bindCallback.ts} (95%) diff --git a/spec/observables/from-callback-spec.js b/spec/observables/bindCallback-spec.js similarity index 80% rename from spec/observables/from-callback-spec.js rename to spec/observables/bindCallback-spec.js index 5e406b0d10..e5395f4b9a 100644 --- a/spec/observables/from-callback-spec.js +++ b/spec/observables/bindCallback-spec.js @@ -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 () { @@ -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 () { @@ -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 () { @@ -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'); @@ -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(); diff --git a/src/Observable.ts b/src/Observable.ts index 7aa132d93f..1decd4a02a 100644 --- a/src/Observable.ts +++ b/src/Observable.ts @@ -134,6 +134,7 @@ export class Observable implements CoreOperators { } // static method stubs + static bindCallback: (callbackFunc: Function, ctx?: Object, selector?: Function, scheduler?: Scheduler) => Function; static combineLatest: (...observables: Array | Array> | ((...values: Array) => T) | @@ -152,7 +153,6 @@ export class Observable implements CoreOperators { removeHandler: (handler: Function) => void, selector?: (...args: Array) => T) => Observable; static fromPromise: (promise: Promise, scheduler?: Scheduler) => Observable; - static fromCallback: (callbackFunc: Function, ctx?: Object, selector?: Function, scheduler?: Scheduler) => Function; static interval: (interval: number, scheduler?: Scheduler) => Observable; static merge: (...observables: Array | Scheduler | number>) => Observable; static never: () => Observable; diff --git a/src/Rx.KitchenSink.ts b/src/Rx.KitchenSink.ts index 3b74aadd04..3a945ff708 100644 --- a/src/Rx.KitchenSink.ts +++ b/src/Rx.KitchenSink.ts @@ -25,7 +25,7 @@ export interface KitchenSinkOperators extends CoreOperators { 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'; @@ -33,7 +33,6 @@ 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'; diff --git a/src/Rx.ts b/src/Rx.ts index f398663da8..3a4c6d1d7d 100644 --- a/src/Rx.ts +++ b/src/Rx.ts @@ -11,7 +11,7 @@ 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'; @@ -19,7 +19,6 @@ 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'; diff --git a/src/add/observable/bindCallback.ts b/src/add/observable/bindCallback.ts new file mode 100644 index 0000000000..391ec38b28 --- /dev/null +++ b/src/add/observable/bindCallback.ts @@ -0,0 +1,3 @@ +import {Observable} from '../../Observable'; +import {BoundCallbackObsevable} from '../../observable/bindCallback'; +Observable.bindCallback = BoundCallbackObsevable.create; diff --git a/src/add/observable/fromCallback.ts b/src/add/observable/fromCallback.ts deleted file mode 100644 index 102ff163c5..0000000000 --- a/src/add/observable/fromCallback.ts +++ /dev/null @@ -1,3 +0,0 @@ -import {Observable} from '../../Observable'; -import {CallbackObservable} from '../../observable/fromCallback'; -Observable.fromCallback = CallbackObservable.create; diff --git a/src/observable/fromCallback.ts b/src/observable/bindCallback.ts similarity index 95% rename from src/observable/fromCallback.ts rename to src/observable/bindCallback.ts index 951bfd0237..bcbdbb7595 100644 --- a/src/observable/fromCallback.ts +++ b/src/observable/bindCallback.ts @@ -6,7 +6,7 @@ import {immediate} from '../scheduler/immediate'; import {tryCatch} from '../util/tryCatch'; import {errorObject} from '../util/errorObject'; -export class CallbackObservable extends Observable { +export class BoundCallbackObservable extends Observable { _isScalar: boolean = false; value: T | T[]; @@ -16,7 +16,7 @@ export class CallbackObservable extends Observable { selector: Function = undefined, scheduler: Scheduler = immediate): Function { return (...args): Observable => { - return new CallbackObservable(callbackFunc, ctx, selector, args, scheduler); + return new BoundCallbackObservable(callbackFunc, ctx, selector, args, scheduler); }; }