Skip to content

Commit

Permalink
refactor: Alternative bind approach
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Feb 4, 2022
1 parent 9f3af3b commit aeb2689
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/internal/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
}
}

const _bind = Function.prototype.bind;

function bind<Fn extends (...args: any[]) => any>(fn: Fn, thisArg: any): Fn {
return _bind.call(fn, thisArg);
}

export class SafeSubscriber<T> extends Subscriber<T> {
constructor(
observerOrNext?: Partial<Observer<T>> | ((value: T) => void) | null,
Expand Down Expand Up @@ -166,9 +172,9 @@ export class SafeSubscriber<T> extends Subscriber<T> {
} else {
context = observerOrNext;
}
next = next && ((value: T) => context.next(value));
error = error && ((err: any) => context.error(err));
complete = complete && (() => context.complete());
next = next && bind(next, context);
error = error && bind(error, context);
complete = complete && bind(complete, context);
}

// Once we set the destination, the superclass `Subscriber` will
Expand Down

0 comments on commit aeb2689

Please sign in to comment.