-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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(signals): destroy hook doesn't run in injection context. #4196
fix(signals): destroy hook doesn't run in injection context. #4196
Conversation
The Signal Store saves the injection context upon instantiation and re-uses it in the `onDestroy` hook. If the Store is provided in `root`, any test with an `onDestroy` hook fails with the error message: `NG0205: Injector has already been destroyed`. This commit requires `withHook` do access the DI outside of `onDestroy`.
✅ Deploy Preview for ngrx-io ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
Thanks Rainer! I suggest splitting the bugfix and new feature into separate PRs. Regarding the new signature, it seems useful! But I'd slightly change it in terms of verbosity: withHooks((store) => {
// on init logic
return () => /* on destroy logic */
})
Returning the withHooks(() => {
// init logic can be performed here
return {
onInit(store) {
// and here as well
}
};
}) Also, feel free to open a feature request for the new |
I'll split it into two parts @markostanimirovic. But please help me out here. What would be the new feature here? As far as I see it, the fix is the new feature. |
We can split it in the following way:
|
I'm sorry, but I don't know how to provide a fix without breaking change. Currently, it is like this: inject(DestroyRef).onDestroy(() => {
runInInjectionContext(injector, hooks.onDestroy!);
}); The way I understand the proposed fix: inject(DestroyRef).onDestroy(() => {
hooks.onDestroy!();
}); But then any hook like withHooks({onDestroy(store) {
const service = inject(SomeService);
}}); will fail. So I would fix one thing and introduce a bug at the same time. Maybe you can give me a hint. Thanks! |
Yeah, it will be a breaking change, but if I understood correctly, the current behavior breaks tests when SignalStore is provided at the root level, so I think it's fine to move the Btw, |
This one gets split up into two different PRs (fix and feature) |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
It fixes a testing issues when a store, which is provided in root, has an onDestroy hook.
What is the current behavior?
The Signal Store saves the injection context upon instantiation and re-uses it in the
onDestroy
hook.If the Store is provided in
root
, any test with anonDestroy
hook fails with the error message:NG0205: Injector has already been destroyed
.This commit requires
withHook
to access the DI outside ofonDestroy
.With the main branch, it is very easily to test. Just run the following test:
What is the new behavior?
onDestroy
runs outside of the injection context.I would like to add, that this PR makes
withHooks
in its usage fully compatible withwithMethods
,withComputed
and are similar extensions that provide access to the DI.Does this PR introduce a breaking change?
We add a note the changelog, describe the reasons and that's it. Signal Store is, as far as I know, still in developer preview.
Other information
I left the old notation of
withHooks
. If that PR would be accepted, I'd suggest to remove that one as well, so thatwithHooks
accepts only aHookSupplier
as argument.