btroncone / ngrx-store-localstorage

Simple syncing between @ngrx store and local storage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

custom prefix storageKeySerializer based on app state property in feature metaReducer.

Sireini opened this issue · comments

I am trying to figure out how to create a custom prefix based on a siteId which is stored in the AppState. Could you help me out in providing the siteId of the AppState in the localStorageSyncReducer of a StoreModule.forFeature('x')?

export function localStorageSyncReducer(reducer: ActionReducer<ReadMappingState>): ActionReducer<ReadMappingState> {
    const siteId: string = 'this must be a dynamic siteId based on the site in AppState';
    return localStorageSync({
        keys: STORE_KEYS_TO_PERSIST,
        storageKeySerializer: (key) => `${siteId}_${key}`,
        rehydrate: true,
        removeOnUndefined: true,
        mergeReducer
    })(reducer);
}

This is the state module of feature X:

@NgModule({
    imports: [
        CommonModule,
        StoreModule.forFeature('x', xReducer, {
            metaReducers
        }),
        EffectsModule.forFeature([xEffects]),
    ],
    providers: [xFacade],
})
export class xStateModule { }

Would be nice if somebody could help me out :)

It doesn't have to be from the AppState but can also be a siteId from xFeature

Okay I managed to get the id in an action and save it in the localStorage based on which site is selected with the siteId prefix, but there are two problems:

  1. Both states will be overwritten with data.
  2. Rehydrating of a specific state based on the siteId is not working.