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

fix(signals): destroy hook doesn't run in injection context. #4196

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions modules/signals/spec/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Component, inject, Type } from '@angular/core';
import { TestBed } from '@angular/core/testing';

export function createLocalService<Service extends Type<unknown>>(
serviceToken: Service
serviceToken: Service,
provideLocally = true
): {
service: InstanceType<Service>;
flushEffects: () => void;
Expand All @@ -11,7 +12,7 @@ export function createLocalService<Service extends Type<unknown>>(
@Component({
standalone: true,
template: '',
providers: [serviceToken],
providers: provideLocally ? [serviceToken] : [],
})
class TestComponent {
service = inject(serviceToken);
Expand Down
52 changes: 41 additions & 11 deletions modules/signals/spec/signal-store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { inject, InjectionToken, isSignal, signal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import {
inject,
Injectable,
InjectionToken,
isSignal,
signal,
} from '@angular/core';
import { TestBed, waitForAsync } from '@angular/core/testing';
import {
patchState,
signalStore,
Expand Down Expand Up @@ -275,15 +281,16 @@ describe('signalStore', () => {
factory: () => 'ngrx',
});
const Store = signalStore(
withHooks({
onInit() {
inject(TOKEN);
messages.push('onInit');
},
onDestroy() {
inject(TOKEN);
messages.push('onDestroy');
},
withHooks(() => {
const token = inject(TOKEN);
return {
onInit() {
messages.push('onInit');
},
onDestroy() {
messages.push('onDestroy');
},
};
})
);
const { destroy } = createLocalService(Store);
Expand All @@ -293,6 +300,29 @@ describe('signalStore', () => {
destroy();
expect(messages).toEqual(['onInit', 'onDestroy']);
});

it('runs a destroy hook in a root-scoped store during instantiation', waitForAsync(() => {
const TOKEN = new InjectionToken('TOKEN', {
providedIn: 'root',
factory: () => 'ngrx',
});
let value = '';
const Store = signalStore(
{ providedIn: 'root' },
withHooks(() => {
const token = inject(TOKEN);
return {
onDestroy: () => {
value = token;
},
};
})
);

createLocalService(Store, false);
TestBed.resetTestEnvironment();
expect(value).toBe('ngrx');
}));
});

describe('composition', () => {
Expand Down
5 changes: 2 additions & 3 deletions modules/signals/src/signal-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,9 @@ export function signalStore(

if (hooks.onDestroy) {
const injector = inject(Injector);
const { onDestroy } = hooks;

inject(DestroyRef).onDestroy(() => {
runInInjectionContext(injector, hooks.onDestroy!);
});
inject(DestroyRef).onDestroy(onDestroy);
}
}
}
Expand Down
19 changes: 15 additions & 4 deletions modules/signals/src/with-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@ type HooksFactory<Input extends SignalStoreFeatureResult> = (
>
) => void;

export function withHooks<Input extends SignalStoreFeatureResult>(hooks: {
type HooksSupplier<Input extends SignalStoreFeatureResult> = () => {
onInit?: HooksFactory<Input>;
onDestroy?: HooksFactory<Input>;
}): SignalStoreFeature<Input, EmptyFeatureResult> {
};

export function withHooks<Input extends SignalStoreFeatureResult>(
hooks:
| {
onInit?: HooksFactory<Input>;
onDestroy?: HooksFactory<Input>;
}
| HooksSupplier<Input>
): SignalStoreFeature<Input, EmptyFeatureResult> {
return (store) => {
const createHook = (name: keyof typeof hooks) => {
const hook = hooks[name];
const _hooks = typeof hooks === 'function' ? hooks() : hooks;

const createHook = (name: keyof typeof _hooks) => {
const hook = _hooks[name];
const currentHook = store.hooks[name];

return hook
Expand Down