-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Hard bound methods when creating new observer #3842
Comments
I would not call this a bug. The documentation is pretty clear that what's passed is a A somewhat related issue: #3229 |
@cartant If possible, yes, as it leads to less unexpected behaviours. I came to this after I ran into trouble doing something like this when creating an observable return new Observable<any>(obs => {
this.sock.on(eventName, obs.next);
this.sock.on('disconnect',obs.complete)
return () => {
console.log('socket cleaned');
};
}); I know the workaround, but as I like functional programming, and would prefer things like this to work. |
Yeah, but with promises, the resolve and reject functions are passed as separate parameters. I'm not sure I agree with the effected behaviour being "unexpected", as the methods have to be explicitly extracted using destructuring - or whatever. Also, binding the methods to the instance would incur a performance penalty: Maybe there are some implementation changes in the |
I agree with @cartant here in general, this is neither bug nor unexpected behavior. Subscriber never explicitly said to be context binded and it was not a design goal. It is still possible eventually we could have it, but we don't actively pursue as goal compare to other design decisions core trying to achieve at this moment. |
About unexpected, #3229 is an example. But I'm not going to pressure here. All I'm saying is that the developer ergonomics of the subscriber usage would improve when this is implemented. It will enable a more functional way of programming. With the JS destructuring this will enable a more clean way to create observables. const documentClicks$ = new Observable(({ next }) =>
document.addEventListener('click', next)
); I think that is a nice an concise style of programming. (I know about fromEvent, this is a sample on style, not on functionality) |
@SanderElias Sure, but at some point it will be necessary to accept that the API involves subscriptions and subscribers and that they are object instances. After all, this will fail: const source = of(1);
const { unsubscribe } = source.subscribe();
unsubscribe(); Also, Maybe if a factory-function equivalent of |
hmm, A factory function would do it alright. I understand that the (although small) overhead adds up, and will be a chore on creating operators. It would be cool if such a factory would be part of rxjs. If not, I will create one for myself. |
Bug Report/ Feature request
Current Behavior
The follow code fails because the methods on the subscriber are not hard bound to it.
Reproduction
Expected behavior
That the code works as expected. I'm not sure if this is a bug or a feature request thought.
Possible Solution
Hard bound methods on observable
The text was updated successfully, but these errors were encountered: