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): run onDestroy outside of injection context #4200

Merged
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
20 changes: 19 additions & 1 deletion modules/signals/spec/signal-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ describe('signalStore', () => {
expect(message).toBe('onDestroy');
});

// FIX: injection context will be provided for `onDestroy` in a separate PR
// see https://github.com/ngrx/platform/pull/4196#issuecomment-1875228588
it('executes hooks in injection context', () => {
const messages: string[] = [];
const TOKEN = new InjectionToken('TOKEN', {
Expand All @@ -281,7 +283,7 @@ describe('signalStore', () => {
messages.push('onInit');
},
onDestroy() {
inject(TOKEN);
// inject(TOKEN);
messages.push('onDestroy');
},
})
Expand All @@ -293,6 +295,22 @@ describe('signalStore', () => {
destroy();
expect(messages).toEqual(['onInit', 'onDestroy']);
});

it('succeeds with onDestroy and providedIn: root', () => {
const messages: string[] = [];
const Store = signalStore(
{ providedIn: 'root' },
withHooks({
onDestroy() {
messages.push('ending...');
},
})
);
TestBed.inject(Store);
TestBed.resetTestEnvironment();

expect(messages).toEqual(['ending...']);
});
});

describe('composition', () => {
Expand Down
26 changes: 8 additions & 18 deletions modules/signals/src/signal-store.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import {
DestroyRef,
inject,
Injectable,
Injector,
runInInjectionContext,
signal,
Type,
} from '@angular/core';
import { DestroyRef, inject, Injectable, signal, Type } from '@angular/core';
import { STATE_SIGNAL, StateSignal } from './state-signal';
import {
EmptyFeatureResult,
InnerSignalStore,
MergeFeatureResults,
SignalStoreProps,
SignalStoreConfig,
SignalStoreFeature,
SignalStoreFeatureResult,
SignalStoreProps,
} from './signal-store-models';
import { Prettify } from './ts-helpers';

Expand Down Expand Up @@ -325,16 +317,14 @@ export function signalStore(
(this as any)[key] = props[key];
}

if (hooks.onInit) {
hooks.onInit();
}
const { onInit, onDestroy } = hooks;

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

inject(DestroyRef).onDestroy(() => {
runInInjectionContext(injector, hooks.onDestroy!);
markostanimirovic marked this conversation as resolved.
Show resolved Hide resolved
});
if (onDestroy) {
inject(DestroyRef).onDestroy(onDestroy);
markostanimirovic marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down