From c7dd15dc4ec0403e7a806e4c70791f36b977ddf1 Mon Sep 17 00:00:00 2001 From: Nikita Tsyhankov Date: Sat, 17 Dec 2022 21:46:50 +0200 Subject: [PATCH 1/2] fix: add store service cleanup --- .../src/lib/core/internals/select-snapshot.ts | 2 +- .../src/lib/core/internals/static-injector.ts | 4 +- .../src/lib/select-snapshot.spec.ts | 59 +++++++++++++------ 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/libs/select-snapshot/src/lib/core/internals/select-snapshot.ts b/libs/select-snapshot/src/lib/core/internals/select-snapshot.ts index 1681040..f7e4a27 100644 --- a/libs/select-snapshot/src/lib/core/internals/select-snapshot.ts +++ b/libs/select-snapshot/src/lib/core/internals/select-snapshot.ts @@ -53,7 +53,7 @@ export function defineSelectSnapshotProperties( ); // Don't use the `directiveInject` here as it works ONLY // during view creation. - store = store || getStore(); + store = getStore(); return store.selectSnapshot(selector); }, enumerable: true, diff --git a/libs/select-snapshot/src/lib/core/internals/static-injector.ts b/libs/select-snapshot/src/lib/core/internals/static-injector.ts index 7afa060..51f6432 100644 --- a/libs/select-snapshot/src/lib/core/internals/static-injector.ts +++ b/libs/select-snapshot/src/lib/core/internals/static-injector.ts @@ -8,6 +8,7 @@ class NgxsSelectSnapshotModuleIsNotImported extends Error { } let injector: Injector | null = null; +let store: Store | null = null; function assertDefined(actual: T | null | undefined): asserts actual is T { if (actual == null) { @@ -25,9 +26,10 @@ export function setInjector(parentInjector: Injector): void { */ export function clearInjector(): void { injector = null; + store = null; } export function getStore(): never | Store { assertDefined(injector); - return injector!.get(Store); + return store || injector!.get(Store); } diff --git a/libs/select-snapshot/src/lib/select-snapshot.spec.ts b/libs/select-snapshot/src/lib/select-snapshot.spec.ts index 897f8ad..da5c1c1 100644 --- a/libs/select-snapshot/src/lib/select-snapshot.spec.ts +++ b/libs/select-snapshot/src/lib/select-snapshot.spec.ts @@ -217,29 +217,29 @@ describe('SelectSnapshot', () => { expect(message).toEqual("Cannot read properties of undefined (reading 'here')"); }); - it('should get the correct snapshot after dispatching multiple actions', () => { - // Arrange - class Increment { - public static type = '[Counter] Increment'; - } + // Arrange + class Increment { + public static type = '[Counter] Increment'; + } - @State({ - name: 'counter', - defaults: 0, - }) - @Injectable() - class CounterState { - @Action(Increment) - increment({ setState, getState }: StateContext): void { - setState(getState() + 1); - } + @State({ + name: 'counter', + defaults: 0, + }) + @Injectable() + class CounterState { + @Action(Increment) + increment({ setState, getState }: StateContext): void { + setState(getState() + 1); } + } - @Component({ template: '' }) - class TestComponent { - @SelectSnapshot(CounterState) counter!: number; - } + @Component({ template: '' }) + class TestComponent { + @SelectSnapshot(CounterState) counter!: number; + } + it('should get the correct snapshot after dispatching multiple actions', () => { // Act TestBed.configureTestingModule({ imports: [ @@ -262,6 +262,27 @@ describe('SelectSnapshot', () => { expect(componentInstance.counter).toBe(4); }); + it('should get the correct state after destroying the module', () => { + // Act + TestBed.configureTestingModule({ + imports: [ + NgxsModule.forRoot([CounterState], { developmentMode: true }), + NgxsSelectSnapshotModule.forRoot(), + ], + declarations: [TestComponent], + }); + + // Assert + const { componentInstance } = TestBed.createComponent(TestComponent); + const store: Store = TestBed.inject(Store); + + expect(componentInstance.counter).toBe(0); + + store.dispatch(new Increment()); + + expect(componentInstance.counter).toBe(1); + }); + @State({ name: 'nullselector', defaults: { From 198cbfe03b07f49ff591f547097a75b621aa8e43 Mon Sep 17 00:00:00 2001 From: Nikita Tsyhankov Date: Sat, 8 Apr 2023 22:51:00 +0300 Subject: [PATCH 2/2] fix: store injectior optimization --- libs/select-snapshot/src/lib/core/internals/static-injector.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/select-snapshot/src/lib/core/internals/static-injector.ts b/libs/select-snapshot/src/lib/core/internals/static-injector.ts index 51f6432..89142d0 100644 --- a/libs/select-snapshot/src/lib/core/internals/static-injector.ts +++ b/libs/select-snapshot/src/lib/core/internals/static-injector.ts @@ -31,5 +31,6 @@ export function clearInjector(): void { export function getStore(): never | Store { assertDefined(injector); - return store || injector!.get(Store); + store = store || injector!.get(Store); + return store; }