-
Notifications
You must be signed in to change notification settings - Fork 405
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
🚀[FEATURE]: @ngxs/storage-plugin forFeature #850
Comments
Can in more detail |
You can use the |
@eranshmil I don't see this working, can you provide me an example? @splincode My use case is when you have a root state and couple of lazy loaded states but you only wanna save in storage one of the lazy loaded states. |
I thought about the same thing. What @Carniatto wanted to say with "allow only part of the state to be persisted in localStorage" is (at least it was like following in my opinion): Allow to add some keys with a forFeature dynamically, based on the lazy loaded modules. For example, if you split your application like this:
Then I'd personally don't like to configure the NgxsStorage-plugin for AdminModule when loading AppModule initially. |
it's also a performance gain since the filtering for keys happens after a storage-update has been initiated https://stackblitz.com/edit/ngxs-repro-yffmsq |
@splincode Only issue with using data would be the fact you are removing CRQS. For me it's not a benefit. I hate to see this just replace all over and go like NGRX |
@Jordan-Hall up to you, if NGRX can help you |
I think you've missed understood. I'm saying this is required and not to force NGXS data on us because I hate NGRX. One of the reasons why I've been moving clients over to NGXS |
Therefore, this issue is still open, and it must be solved. And if you have the time and desire, you can send a pull request and it will be a good help |
Sure, doesn't it make sense to decouple that one in @ngxs-labs/data? It appears to have better API rather than using ForRoot and forFeature. Plus then Data can use it |
Using the date plugin, you do not need forRoot, forFeature. Because there you can add a decorator over the state and it will be easily applied. |
I managed to add an additional key in a lazy loaded module (and do not provide it in the "main" module) with the following workaround: I've placed the code in an export const MFEAPP_STATE_NAME = 'mycustomappstatename';
@State<MFEAppStateModel>({
name: MFEAPP_STATE_NAME ,
...
} In order to update the keys, I inject the export class MFEAppSettingsGuard implements CanActivate {
constructor(
@Inject(NGXS_STORAGE_PLUGIN_OPTIONS)
private readonly ngxsStoragePluginOptions: NgxsStoragePluginOptions
) {
this.ngxsStoragePluginOptions.key = [
...(this.ngxsStoragePluginOptions.key as string[]),
MFEAPP_STATE_NAME,
];
}
public canActivate(): Observable<boolean> {...}` |
Just tested it in the constructor of the lazy loaded module and it seems to work. Thanks for your workaround! |
I was a bit premature when I commented. The key is created in the local storage, but when I reload the value is reset to the initial state. Do you also had this problem and if so how did u resolve it? |
I tried @hobe approach but it doesn't work for me. Seems like the problem is that even if you update plugin options the internal keys variable used by plugin remains with initial value assigned inside the constructor. |
@luchian94 I did consider to implement this feature and I think it should be a part of the implementation. The problem is the design and the order of things happening in the code. Suppose the following case: imports: [
NgxsModule.forFeature([CountriesState]),
NgxsStoragePluginModule.forFeature([CountriesState])
] The We can't force users to declare the |
Any update on this issue? |
I'm submitting a...
Current behavior
only supports forRoot option
Expected behavior
allow only part of the state to be persisted in localStorage
What is the motivation / use case for changing the behavior?
In large applications or even in Nx Workspaces one's might find interesting to have the state for some lazy modules persisted in localStorage but not all the state
Environment
The text was updated successfully, but these errors were encountered: