-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(store): add root store initializer guard
In this commit, we are adding the root store initializer guard, which ensures that `provideStore()` is not accidentally called multiple times (e.g., in feature-level providers such as route providers).
- Loading branch information
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { inject, InjectionToken } from '@angular/core'; | ||
|
||
export const ROOT_STORE_GUARD = /* @__PURE__ */ new InjectionToken('ROOT_STORE_GUARD', { | ||
providedIn: 'root', | ||
factory: () => ({ initialized: false }) | ||
}); | ||
|
||
export function assertRootStoreNotInitialized(): void { | ||
const rootStoreGuard = inject(ROOT_STORE_GUARD); | ||
if (rootStoreGuard.initialized) { | ||
throw new Error('provideStore() should only be called once.'); | ||
} | ||
rootStoreGuard.initialized = true; | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/store/tests/issues/issue-2277-provide-store-twice.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ApplicationConfig, Component } from '@angular/core'; | ||
import { bootstrapApplication } from '@angular/platform-browser'; | ||
import { provideRouter, Router } from '@angular/router'; | ||
import { provideStore } from '@ngxs/store'; | ||
import { freshPlatform, skipConsoleLogging } from '@ngxs/store/internals/testing'; | ||
|
||
describe('provideStore() being called twice', () => { | ||
@Component({ selector: 'app-root', template: '' }) | ||
class TestComponent {} | ||
|
||
@Component({ template: '' }) | ||
class RouteComponent {} | ||
|
||
const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideStore(), | ||
provideRouter([ | ||
{ path: 'route', loadComponent: () => RouteComponent, providers: [provideStore()] } | ||
]) | ||
] | ||
}; | ||
|
||
it( | ||
'should throw an error when provideStore() is called twice', | ||
freshPlatform(async () => { | ||
// Arrange | ||
expect.hasAssertions(); | ||
|
||
// Act | ||
const { injector } = await skipConsoleLogging(() => | ||
bootstrapApplication(TestComponent, appConfig) | ||
); | ||
const router = injector.get(Router); | ||
|
||
try { | ||
await router.navigateByUrl('/route'); | ||
} catch (error) { | ||
// Assert | ||
expect(error.message).toEqual('provideStore() should only be called once.'); | ||
} | ||
}) | ||
); | ||
}); |