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

[BUGFIX release] Avoid instantiation errors when app/router.js injects the router service. #19405

Merged
merged 2 commits into from
Mar 12, 2021

Conversation

Ravenstine
Copy link
Contributor

Related to my previous PR: #19385

And issue: #17791

@rwjblue Since we talked about this with @snewcomer recently, maybe you can weigh in on this approach?

The intention is to prevent an infinite recursion when service:router is invoked before router:main has a chance to be initialized and cached. This PR does it by making service:router more "passive" so that the responsibility of forwarding routeWillChange and routeDidChange events is handled by router:main, as opposed to looking up(and possibly instantiating) router:main and listening to events on it from service:router. The active behavior of service:router instantiating router:main seems unexpected, and doesn't appear to be required in any tests.

Specifically, the service:router has been changed to only use router:main if it's already available and router:main now forwards those two route events to service:router if that service has been instantiated. That way there's no possibility of getting caught in a loop if service:router happens to be used during initialization of router:main, which can happen unexpectedly when using a service from an add-on that references service:router.

Definitely open to any kind of feedback.

@Ravenstine Ravenstine force-pushed the passive-router-service branch 5 times, most recently from 295b920 to 7344cd3 Compare February 12, 2021 21:52
Copy link
Member

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

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

Since we only need to forward the events when someone calls routerService.on('routeWillChange'), why don't we implement on and setup the wiring there?

packages/@ember/-internals/routing/lib/services/router.ts Outdated Show resolved Hide resolved
Copy link
Member

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

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

Let's go the route of #19454, and look up the router service during init. That way we can avoid relying on { instantiate: false }. It does add some small amount of overhead (automatic creation of service:router), but in practice I don't think that will matter much and the resulting simplicity of the implementation will be easier to maintain.

What do you think @Ravenstine?

@Ravenstine
Copy link
Contributor Author

@rwjblue That makes sense. Doing { instantiate: false } doesn't seem to really save us much. @snewcomer and I will work on this tomorrow.

@rwjblue
Copy link
Member

rwjblue commented Mar 10, 2021

kk, awesome thanks for taking a look @Ravenstine!

@Ravenstine Ravenstine force-pushed the passive-router-service branch 4 times, most recently from 6dc35c8 to 19ddb89 Compare March 10, 2021 18:12
@Ravenstine Ravenstine requested a review from rwjblue March 12, 2021 04:39
@sly7-7
Copy link
Contributor

sly7-7 commented Mar 12, 2021

Thanks a lot to both of you @snewcomer @Ravenstine. This looks good to me. I just don't know what should be the choice for the setupRouter() call. I have my opinion, but I'm so new to the router area that I probably miss something.

@rwjblue rwjblue changed the title Make router service passive [BUGFIX release] Make router service passive Mar 12, 2021
Copy link
Member

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

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

Awesome, this is great work. Thank you @Ravenstine @snewcomer @sly7-7!

@rwjblue rwjblue merged commit 1c5845f into emberjs:master Mar 12, 2021
@rwjblue rwjblue added the Bug label Mar 12, 2021
@rwjblue rwjblue changed the title [BUGFIX release] Make router service passive [BUGFIX release] Avoid instantiation errors when app/router.js injects the router service. Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants