-
Notifications
You must be signed in to change notification settings - Fork 9
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
Unregister Services before they are registered #425
Conversation
🦋 Changeset detectedLatest commit: 71e831a The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Just to add some more context to this from an internal discussion for future travelers... I think how this works with that service integration is that if a service is injected when that setupBrowserFakes test-helper hasn't been called yet, the service will be the "normal" one without any mocking capabilities. So using the real DOM APIs. And that's the timing issue that causes the bug. So with this PR, all service injections that happen after that call to However, as this is a very low-risk change, and it does no harm, and it does fix those cases where the services that needed to be mocked was only instantiated after calling setupBrowserFakes, I think we can land this for now, even if there is still some chance of the mock setup not working as expected in all cases. /cc @NullVoxPopuli |
I've only ever seen this problem come up when folks try to use services in initializers -- they should not 🙈 a safer alternative to initializersIn your application route, do your setup in one of the model hooks. Calling unregister does feel like it's more hiding the problem than fixing it. Ultimately, folks will want to fix whatever the underlying problem (or initializer) is. |
Is there a failing test that can be made for this? |
Do we move forward with this PR or do I patch again |
It sounds like @NullVoxPopuli is thinking there's an underlying problem and this is an unreasonable patch? If so, it'd be best to resolve the root issue rather than patching an anti-pattern. What do you think @simonihmig ? |
Anti-pattern to unregister Services? What is this method good for in that case, when should it be used? |
When a service has already been used, yea -- because you're swapping the service with a different implementation than what your app/tests booted with.
I don't know 🤔 |
I am trying to understand. |
registering after a service has already been used though (should) error, iirc -- because you're changing the implementation already in use. The only time it's safe to replace the service is if it hasn't already been used for your test/app instance (such as in an initializer).
<3 |
I understand, thanks for taking the time to explain. In that case, I don't know where we want to go with that PR, if there was/there is no alternative fix. |
do you have any initializers accessing the browser services? |
I cannot tell for now, I need to take a deeper look (well, you kind of know the codebase I am talking about lol). But also, I should have been more accurate when I said "it does help to make many tests pass":
|
Yes, I agree this is not the best fix, as you can see in my statement below. But I still think it does no harm compared to what we have now, and is better than nothing, until we come up with something better...
Yeah, I think we should create one! Can do this later this week...
Yes, that's why the fix is not ideal. But after
I respect your opinion that you don't like this pattern, but I don't think we can say this is an agreed upon anti-pattern. The [initializer guide](https://guides.emberjs.com/release/applications/initializers/9 has lots of
I don't think it is directly doing that. Rather an initializer accessing a service that is accessing another injected service that has browser services injected and accessing some window thingy eagerly!
On the cons side though, and that's a thing that I have a hard time wrapping my head around, while modern Ember is about embracing the platform and less Emberisms, we are squeezing the platform feature of the I think it is possible to fix this properly by not even having to register the service explicitly, instead delegating to the window imported from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving to not be a blocker given Simon's response above. Adding a test, if possible, would be sweet!
Here is a reproduction: #428 Just added an instance-initializer looking up the service and... 💥 @MrChocolatine can you cherry pick that commit into your PR here to prove the issue is getting fixed please? |
Cherry-picked from #428
Done @simonihmig . |
Tests are passing, so the previously failing test is being fixed by this change. Will go ahead and merge this! |
This should normally fix #413 .
@simonihmig