Skip to content

Commit

Permalink
feat(bindCallback): remove thisArg
Browse files Browse the repository at this point in the history
thisArg is removed as this method does not mirror any native function with a thisArg

related #878
  • Loading branch information
benlesh committed Dec 4, 2015
1 parent 305d66d commit feea9a1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 25 deletions.
19 changes: 2 additions & 17 deletions spec/observables/bindCallback-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Observable.bindCallback', function () {
function callback(datum, cb) {
cb(null, datum);
}
var boundCallback = Observable.bindCallback(callback, null, function (err, datum) { return datum; });
var boundCallback = Observable.bindCallback(callback, function (err, datum) { return datum; });

boundCallback(42)
.subscribe(function (x) {
Expand All @@ -33,26 +33,11 @@ describe('Observable.bindCallback', function () {
done);
});

it('should override `this` in the callback', function (done) {
function callback(cb) {
cb(this.value);
}
var boundCallback = Observable.bindCallback(callback, {value: 42});

boundCallback()
.subscribe(function (x) {
expect(x).toBe(42);
}, function () {
done.fail('should not be called');
},
done);
});

it('should emit an error when the selector throws', function (done) {
function callback(cb) {
cb(42);
}
var boundCallback = Observable.bindCallback(callback, null, function (err) { throw new Error('Yikes!'); });
var boundCallback = Observable.bindCallback(callback, function (err) { throw new Error('Yikes!'); });

boundCallback()
.subscribe(function () {
Expand Down
2 changes: 1 addition & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +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 bindCallback: <T>(callbackFunc: Function, selector?: Function, scheduler?: Scheduler) => Function;
static combineLatest: <T>(...observables: Array<Observable<any> |
Array<Observable<any>> |
((...values: Array<any>) => T) |
Expand Down
11 changes: 4 additions & 7 deletions src/observable/bindCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ export class BoundCallbackObservable<T> extends Observable<T> {
value: T | T[];

static create<T>(callbackFunc: Function,
ctx: Object = undefined,
selector: Function = undefined,
scheduler: Scheduler = immediate): Function {
return (...args): Observable<T> => {
return new BoundCallbackObservable(callbackFunc, ctx, selector, args, scheduler);
return new BoundCallbackObservable(callbackFunc, selector, args, scheduler);
};
}

constructor(private callbackFunc: Function,
private ctx,
private selector,
private args: any[],
public scheduler: Scheduler = immediate) {
Expand All @@ -30,7 +28,6 @@ export class BoundCallbackObservable<T> extends Observable<T> {

_subscribe(subscriber: Subscriber<T | T[]>) {
const callbackFunc = this.callbackFunc;
const ctx = this.ctx;
const selector = this.selector;
const args = this.args;
const scheduler = this.scheduler;
Expand All @@ -49,7 +46,7 @@ export class BoundCallbackObservable<T> extends Observable<T> {
this.value = innerArgs;

if (selector) {
results = tryCatch(selector).apply(ctx, innerArgs);
results = tryCatch(selector).apply(this, innerArgs);
if (results === errorObject) { return subscriber.error(results.e); }
subscriber.next(results);
} else {
Expand All @@ -74,7 +71,7 @@ export class BoundCallbackObservable<T> extends Observable<T> {
this._isScalar = true;

if (selector) {
results = tryCatch(selector).apply(ctx, innerArgs);
results = tryCatch(selector).apply(this, innerArgs);
if (results === errorObject) {
return subscription.add(scheduler.schedule(dispatchError, 0, { err: results.e, subscriber }));
}
Expand All @@ -95,7 +92,7 @@ export class BoundCallbackObservable<T> extends Observable<T> {

if (handler) {
args.push(handler);
callbackFunc.apply(ctx, args);
callbackFunc.apply(this, args);
}
}
}
Expand Down

0 comments on commit feea9a1

Please sign in to comment.