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

Implement ContextUtils as mixins #3440

Merged
merged 1 commit into from
May 9, 2016
Merged

Conversation

gaearon
Copy link
Contributor

@gaearon gaearon commented May 9, 2016

As discussed in #3439, these are implementation details and are better implemented as mixins so that it’s easier to kill them later. They don’t leak into public API, and React mixin merging behavior comes in handy here.

This way the hierarchy stays flat but we work around facebook/react#2517.

@gaearon gaearon added this to the next-3.0.0 milestone May 9, 2016
const contextName = makeContextName(name)
const listenersKey = `${contextName}/listeners`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep these folks scoped to avoid any possible name clashes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would there be name clashes? The context key in this case is going to be @@contextSubscriber/router – I fixed it from the original implementation where I munged the original context object unnecessarily.

Copy link
Contributor Author

@gaearon gaearon May 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you define something like mixins: [ContextSubscriber('router'), ContextSubscriber('whatever')], React would complain that they both define subscribe method. Even worse, if that subscribe stops being a method, React will blindly merge mixins and thus they would each overwrite the same this.listeners.

Finally, the consuming component (such as <Router>) may add an instance field called this.listeners or a method called subscribe. By scoping mixin method and field names by the corresponding context name, we are playing it safe. We also signal both internally and externally this way that those things (e.g. this.subscribe) are not supposed to be called directly even though technically they are available on the <Router> instance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The joy of mixins...

@taion taion merged commit dcb1b51 into remix-run:next May 9, 2016
@gaearon gaearon deleted the context-mixins branch May 9, 2016 16:54
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
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.

2 participants