Skip to content

Commit

Permalink
refactor(store): tree-shake internal state tokens
Browse files Browse the repository at this point in the history
In this commit, we remove internal injection token providers from the root providers list.
Instead, we expose a function that provides these tokens.
  • Loading branch information
arturovt committed Nov 3, 2024
1 parent f22f4cb commit 066a020
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/store/internals/src/internal-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const NG_DEV_MODE = typeof ngDevMode !== 'undefined' && ngDevMode;

// These tokens are internal and can change at any point.

export const ɵNGXS_STATE_FACTORY = new InjectionToken<any>(
export const ɵNGXS_STATE_FACTORY = /* @__PURE__ */ new InjectionToken<any>(
NG_DEV_MODE ? 'ɵNGXS_STATE_FACTORY' : ''
);

export const ɵNGXS_STATE_CONTEXT_FACTORY = new InjectionToken<any>(
export const ɵNGXS_STATE_CONTEXT_FACTORY = /* @__PURE__ */ new InjectionToken<any>(
NG_DEV_MODE ? 'ɵNGXS_STATE_CONTEXT_FACTORY' : ''
);
21 changes: 21 additions & 0 deletions packages/store/src/internal/provide-internal-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { makeEnvironmentProviders } from '@angular/core';
import { ɵNGXS_STATE_CONTEXT_FACTORY, ɵNGXS_STATE_FACTORY } from '@ngxs/store/internals';

import { StateFactory } from './state-factory';
import { StateContextFactory } from './state-context-factory';

// Backward compatibility is provided because these tokens are used by third-party
// libraries. We expose a separate function to allow tree-shaking of these tokens
// rif they are not used in standard applications that do not rely on them.
export function ɵprovideNgxsInternalStateTokens() {
return makeEnvironmentProviders([
{
provide: ɵNGXS_STATE_CONTEXT_FACTORY,
useExisting: StateContextFactory
},
{
provide: ɵNGXS_STATE_FACTORY,
useExisting: StateFactory
}
]);
}
2 changes: 2 additions & 0 deletions packages/store/src/private_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export { NgxsRootModule as ɵNgxsRootModule } from './modules/ngxs-root.module';
export { NgxsFeatureModule as ɵNgxsFeatureModule } from './modules/ngxs-feature.module';

export * from './selectors/private_api';

export { ɵprovideNgxsInternalStateTokens } from './internal/provide-internal-tokens';
16 changes: 1 addition & 15 deletions packages/store/src/standalone-features/root-providers.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { APP_BOOTSTRAP_LISTENER, Provider, inject } from '@angular/core';
import {
ɵStateClass,
ɵNGXS_STATE_CONTEXT_FACTORY,
ɵNGXS_STATE_FACTORY,
ɵNgxsAppBootstrappedState
} from '@ngxs/store/internals';
import { ɵStateClass, ɵNgxsAppBootstrappedState } from '@ngxs/store/internals';

import { PluginManager } from '../plugin-manager';
import { StateFactory } from '../internal/state-factory';
import { CUSTOM_NGXS_EXECUTION_STRATEGY } from '../execution/symbols';
import { StateContextFactory } from '../internal/state-context-factory';
import { NgxsModuleOptions, ROOT_STATE_TOKEN, NGXS_OPTIONS } from '../symbols';

/**
Expand Down Expand Up @@ -43,14 +37,6 @@ export function getRootProviders(
{
provide: CUSTOM_NGXS_EXECUTION_STRATEGY,
useValue: options.executionStrategy
},
{
provide: ɵNGXS_STATE_CONTEXT_FACTORY,
useExisting: StateContextFactory
},
{
provide: ɵNGXS_STATE_FACTORY,
useExisting: StateFactory
}
];
}

0 comments on commit 066a020

Please sign in to comment.