UNPKG

@o3r/store-sync

Version:

This module is exposing an NgRx store synchronization solution (synchronous and asynchronous) via the class StorageSync.

189 lines (182 loc) • 7.83 kB
import { Logger, Serializer } from '@o3r/core'; import * as _ngrx_store from '@ngrx/store'; /** * Options to provide to the synchronous store synchronization mechanism */ interface SyncStorageSyncOptions { /** Callback to define the serialization strategy of the store */ serialize?: (state: any) => any; /** Callback to define the deserialization strategy of the store */ deserialize?: (state: any) => any; /** Callback to define the reviving strategy of the store */ reviver?: (key: string, value: any) => any; /** replacer callback function provided to the parser */ replacer?: ((key: string, value: any) => any) | string[]; /** Filter to apply to the store fields */ filter?: string[]; /** Spacing for serializer */ space?: string | number; /** Defines whether the store is loaded as forFeature */ syncForFeature?: boolean; } /** * Interface of the configurations for a store */ interface StorageKeyConfiguration { [key: string]: string[] | number[] | StorageKeyConfiguration[] | SyncStorageSyncOptions | ((key: string, value: any) => any); } /** * Definition of a store to to synchronize */ type StorageKeys = (StorageKeyConfiguration | SyncStorageSyncOptions | string)[]; /** * Configuration to define the way the stores should be synchronize to the storage */ interface SyncStorageConfig { /** Map of the store to synchronize */ keys: StorageKeys; /** Should the stores be initially rehydrated */ rehydrate?: boolean; /** Storage reference */ storage?: Storage; /** Should be removed from storage if undefined */ removeOnUndefined?: boolean; /** Should apply restore date to the restored stores */ restoreDates?: boolean; /** Callback to define the serialization strategy for the store keys */ storageKeySerializer?: (key: string) => string; /** Callback to define the condition to the synchronization */ syncCondition?: (state: any) => any; /** Callback to define the condition to the key synchronization */ syncKeyCondition?: (key: string, state: any) => boolean; /** check the availability of the storage */ checkStorageAvailability?: boolean; /** Merge reducer to use to deserialize the store */ mergeReducer?: (state: any, rehydratedState: any, action: any) => any; /** Logger to report messages */ logger?: Logger; /** Post process after sync storage execution */ postProcess?: (state: any) => void; } /** * Reviver the date from a JSON field if the string is matching iso format. Return the same value otherwise * @param _key JSON item key name * @param value JSON item value */ declare const dateReviver: (_key: string, value: any) => any; /** * Rehydrate the state of the whole application store * @param keys Keys of the store to rehydrate * @param storage storage when getting the data from * @param storageKeySerializer storage key transform function * @param restoreDates determine if the date need to be restored * @param logger */ declare const rehydrateApplicationState: (keys: StorageKeys, storage: Storage | undefined, storageKeySerializer: (key: string) => string, restoreDates: boolean, logger?: Logger) => any; /** * Update the state of the store after reviving from storage * Note: this function is mainly use for internal process of store synchronization * @param state store state * @param keys key of the store * @param storage storage to use for the synchronization * @param storageKeySerializer callback to serialize the store key * @param removeOnUndefined Define if the store should be clean in the storage if undefined * @param syncCondition callback to define if the synchronization should be process * @param logger Logger to report messages */ declare const syncStateUpdate: (state: any, keys: StorageKeys, storage: Storage | undefined, storageKeySerializer: (key: string | number) => string, removeOnUndefined?: boolean, syncCondition?: (state: any) => any, logger?: Logger) => void; /** * Local and Session storage synchronization helper * @param config */ declare const syncStorage: (config: SyncStorageConfig) => (reducer: any) => (state: any, action: any) => any; /** * Format of a key in `StoreSyncConfig` */ interface StoreSyncSerializers { [storeName: string]: Serializer<any>; } /** * An interface defining the configuration attributes to bootstrap `storeSyncMetaReducer` */ interface StoreSyncConfig { /** State keys to sync with storage */ keys: StoreSyncSerializers[]; /** Pull initial state from storage on startup */ rehydrate?: boolean; /** Specify an object that conforms to the Storage interface to use, this will default to localStorage */ storage?: Storage; /** When set, sync to storage will only occur when this function returns a true boolean */ syncCondition?: (state: any) => any; } /** * Typings for async storage */ type AsyncStorage = Omit<Storage, 'getItem'> & { getItem(key: string): Promise<string | null>; }; /** * Options for async storage sync */ interface AsyncStorageSyncOptions extends Omit<SyncStorageConfig, 'storage'> { storage?: AsyncStorage; } /** Options for storage sync method */ type StorageSyncOptions = SyncStorageConfig | AsyncStorageSyncOptions; /** * Options that can be set on the Storage sync constructor * postProcess and syncKeyCondition will be handled by the StorageSync class if smart sync is activated */ type StorageSyncConstructorOptions = Omit<StorageSyncOptions, 'postProcess' | 'syncKeyCondition'>; /** * Storage synchronizer */ declare class StorageSync { private readonly alreadyHydratedStoreSlices; private hasHydrated; private readonly storeImage; options: StorageSyncOptions; constructor(options?: StorageSyncConstructorOptions, extraOptions?: { disableSmartSync: boolean; }); /** * Handle state manipulation in case of store storage sync * Merge strategy is a full merge at init; When a new `store slice` is registered, its initial state is merged with its corresponding storage content * Pass it as mergeReducer callback when calling localStorageSync function from 'ngrx-store-localstorage', in the app * @param state actual state * @param rehydratedState state from storage * @param action Triggered action */ readonly mergeReducer: (state: any, rehydratedState: any, action: any) => any; /** * Returns a meta reducer that handles storage sync */ localStorageSync: () => (reducer: any) => (state: any, action: any) => any; } /** * Defines if an object is a Serializer * @param obj Object to test * @returns True if the object is a Serializer */ declare const isSerializer: (obj: any) => obj is Serializer<any>; /** * Label of the action to rehydrate the store */ declare const REHYDRATE_ACTION_LABEL = "[storeSync] rehydrate"; /** * Action to rehydrate the store */ declare const rehydrateAction: _ngrx_store.ActionCreator<"[storeSync] rehydrate", (props: { payload: Record<string, any>; }) => { payload: Record<string, any>; } & _ngrx_store.Action<"[storeSync] rehydrate">>; /** * Defines if the storage is a non-async storage * @param options The storage config * @returns in case the storage used is a non-async storage */ declare const isLocalStorageConfig: (options: StorageSyncOptions) => options is SyncStorageConfig; export { REHYDRATE_ACTION_LABEL, StorageSync, dateReviver, isLocalStorageConfig, isSerializer, rehydrateAction, rehydrateApplicationState, syncStateUpdate, syncStorage }; export type { AsyncStorage, AsyncStorageSyncOptions, StorageKeyConfiguration, StorageKeys, StorageSyncConstructorOptions, StorageSyncOptions, StoreSyncConfig, StoreSyncSerializers, SyncStorageConfig, SyncStorageSyncOptions }; //# sourceMappingURL=index.d.ts.map