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

refactor(store): tree-shake internal state tokens #2246

Merged
merged 1 commit into from
Nov 12, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $ npm install @ngxs/store@dev
- Fix(store): Prevent writing to state once action handler is unsubscribed [#2231](https://github.com/ngxs/store/pull/2231)
- Performance(store): Replace `instanceof Function` with `typeof` [#2247](https://github.com/ngxs/store/pull/2247)
- Refactor(store): Use `Object.is` as default equality check [#2245](https://github.com/ngxs/store/pull/2245)
- Refactor(store): Tree-shake internal state tokens [#2246](https://github.com/ngxs/store/pull/2246)
- Refactor(router-plugin): Mark selectors as pure [#2248](https://github.com/ngxs/store/pull/2248)
- Refactor(storage-plugin): Mark engine tokens as pure [#2249](https://github.com/ngxs/store/pull/2249)

Expand Down
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
// if 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturovt ɵNGXS_STATE_FACTORY is used by @angular-ru/ngxs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They will need to use the ɵprovideNgxsInternalStateTokens in their providers list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you inject the factory without having access to the injection token? State factory itself is used in the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still inject the factory through the token.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Through which token?

Copy link
Member Author

@arturovt arturovt Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the phrase "having access to the token". Why don't you have access to the token?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ngxs no longer exports ɵNGXS_STATE_FACTORY and ɵNGXS_STATE_CONTEXT_FACTORY tokens after this PR was merged.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't export it before either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, my bad. For some reason instead of import, I thought export was removed. The tokens are still exported from @ngxs/store/internals. I'll try to use ɵprovideNgxsInternalStateTokens() to see if it fixes the issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ɵprovideNgxsInternalStateTokens() fixes the issue. Sorry for the confusion.

ɵ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
}
];
}
Loading