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(bindCallback): properly set context of input function #2319

Merged
merged 1 commit into from
Feb 2, 2017
Merged

fix(bindCallback): properly set context of input function #2319

merged 1 commit into from
Feb 2, 2017

Conversation

mpodlasin
Copy link
Contributor

Current behaviour:
Currently when input function (function that is passed as argument to bindCallback) is internally called, its context is set to context of underlying observable. If input function is not bound beforehand it is possible to implement:

function intercept(cb) {
   const observable = this;
   // do something with underlying observable
}
const boundIntercept = Observable.bindCallback(intercept);
boundIntercept(); // intercept is called with underlying observable as this

Expected behaviour
Since output function (function returned by `bindCallback) should behave identically as input function (apart from changing callback to observable) it is reasonable to assume that context of ouput function will become context of input function.

const boundMethod = Observable.bindCallback(someObject.method);
boundMethod.call(someObject) // calls method with someObject as this (as it was probably intended)

In fact this is what rxjs 4 does, as seen here:
https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/perf/operators/fromcallback.js#L41

This pr implements such behaviour.

Related
This pr does not change operator api. There is still no option to manually set context, which is possible in rxjs 4:

const bound = Observable.fromCallback(func, context);
bound() // func is called with context as this

Please let me know if I should add such functionality in this pr, create another pr or drop context setting functionality altogether.

Have a great day!

Set context of input function to context of output function, so
that it is possible to control context at call time and underlying observable is
not available in input function
@coveralls
Copy link

coveralls commented Feb 2, 2017

Coverage Status

Coverage increased (+0.0003%) to 97.689% when pulling bcd5b93 on Podlas29:bind-callback-this into faabd3e on ReactiveX:master.

@benlesh benlesh merged commit cb91c76 into ReactiveX:master Feb 2, 2017
@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants