ngrx-store-localstorage
Advanced tools
Comparing version 5.0.1 to 5.1.0
export declare const dateReviver: (key: string, value: any) => any; | ||
export declare const rehydrateApplicationState: (keys: any[], storage: Storage, storageKeySerializer: (key: string) => string, restoreDates: boolean) => any; | ||
export declare const syncStateUpdate: (state: any, keys: any[], storage: Storage, storageKeySerializer: (key: string) => string, removeOnUndefined: boolean) => void; | ||
export declare const syncStateUpdate: (state: any, keys: any[], storage: Storage, storageKeySerializer: (key: string) => string, removeOnUndefined: boolean, syncCondition?: (state: any) => any) => void; | ||
export declare const localStorageSync: (config: LocalStorageConfig) => (reducer: any) => (state: any, action: any) => any; | ||
@@ -13,2 +13,3 @@ export declare const localStorageSyncAndClean: (keys: any[], rehydrate?: boolean, removeOnUndefined?: boolean) => (reducer: any) => any; | ||
storageKeySerializer?: (key: string) => string; | ||
syncCondition?: (state: any) => any; | ||
} |
@@ -84,3 +84,17 @@ "use strict"; | ||
}; | ||
exports.syncStateUpdate = function (state, keys, storage, storageKeySerializer, removeOnUndefined) { | ||
exports.syncStateUpdate = function (state, keys, storage, storageKeySerializer, removeOnUndefined, syncCondition) { | ||
if (syncCondition) { | ||
try { | ||
if (syncCondition(state) !== true) { | ||
return; | ||
} | ||
} | ||
catch (e) { | ||
// Treat TypeError as do not sync | ||
if (e instanceof TypeError) { | ||
return; | ||
} | ||
throw e; | ||
} | ||
} | ||
keys.forEach(function (key) { | ||
@@ -185,3 +199,3 @@ var stateSlice = state[key]; | ||
var nextState = reducer(state, action); | ||
exports.syncStateUpdate(nextState, stateKeys, config.storage, config.storageKeySerializer, config.removeOnUndefined); | ||
exports.syncStateUpdate(nextState, stateKeys, config.storage, config.storageKeySerializer, config.removeOnUndefined, config.syncCondition); | ||
return nextState; | ||
@@ -188,0 +202,0 @@ }; |
{ | ||
"name": "ngrx-store-localstorage", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"description": "State and local storage syncing for @ngrx/store", | ||
@@ -21,3 +21,9 @@ "main": "./dist/index.js", | ||
}, | ||
"keywords": ["redux", "ngrx", "store", "localstorage", "rxjs"], | ||
"keywords": [ | ||
"redux", | ||
"ngrx", | ||
"store", | ||
"localstorage", | ||
"rxjs" | ||
], | ||
"author": "Brian Troncone", | ||
@@ -24,0 +30,0 @@ "license": "MIT", |
@@ -79,2 +79,3 @@ # ngrx-store-localstorage | ||
* `restoreDates` \(*boolean? = true*): Restore serialized date objects. If you work directly with ISO date strings, set this option to `false`. | ||
* `syncCondition` (optional) `(state) => boolean`: When set, sync to storage medium will only occur when this function returns a true boolean. Example: `(state) => state.config.syncToStorage` will check the state tree under config.syncToStorage and if true, it will sync to the storage. If undefined or false it will not sync to storage. Often useful for "remember me" options in login. | ||
Usage: `localStorageSync({keys: ['todos', 'visibilityFilter'], storageKeySerializer: (key) => 'cool_' + key, ... })`. In this example `Storage` will use keys `cool_todos` and `cool_visibilityFilter` keys to store `todos` and `visibilityFilter` slices of state). The key itself is used by default - `(key) => key`. | ||
@@ -81,0 +82,0 @@ |
@@ -377,2 +377,44 @@ declare var beforeEachProviders, it, describe, expect, inject; | ||
}); | ||
it('syncCondition', () => { | ||
// Test that syncCondition can selectively trigger a sync state update | ||
let s = new MockStorage(); | ||
let skr = mockStorageKeySerializer; | ||
// Selector always returns false - meaning it should never sync | ||
const shouldNotSyncSelector = (state: any) => { | ||
return false; | ||
}; | ||
syncStateUpdate(initialState, ['state'], s, skr, false, shouldNotSyncSelector); | ||
let raw = s.getItem('state'); | ||
expect(raw).toEqual(null); | ||
let finalState: any = rehydrateApplicationState(['state'], s, skr, true); | ||
expect(JSON.stringify(finalState)).toEqual('{}'); | ||
// Selector should error - so still no sync | ||
const errorSelector = (state: any) => { | ||
return state.doesNotExist; | ||
}; | ||
syncStateUpdate(initialState, ['state'], s, skr, false, errorSelector); | ||
raw = s.getItem('state'); | ||
expect(raw).toEqual(null); | ||
// Selector always returns true - so it should sync | ||
const shouldSyncSelector = (state: any) => { | ||
return true; | ||
}; | ||
syncStateUpdate(initialState, ['state'], s, skr, false, shouldSyncSelector); | ||
raw = s.getItem('state'); | ||
expect(raw).toEqual(t1Json); | ||
finalState = rehydrateApplicationState(['state'], s, skr, true); | ||
expect(JSON.stringify(finalState)).toEqual(initialStateJson); | ||
}); | ||
}); |
@@ -112,4 +112,18 @@ const INIT_ACTION = '@ngrx/store/init'; | ||
storageKeySerializer: (key: string) => string, | ||
removeOnUndefined: boolean | ||
removeOnUndefined: boolean, | ||
syncCondition?: (state: any) => any | ||
) => { | ||
if (syncCondition) { | ||
try { | ||
if (syncCondition(state) !== true) { | ||
return; | ||
} | ||
} catch (e) { | ||
// Treat TypeError as do not sync | ||
if (e instanceof TypeError) { | ||
return; | ||
} | ||
throw e; | ||
} | ||
} | ||
keys.forEach(key => { | ||
@@ -244,3 +258,4 @@ let stateSlice = state[key]; | ||
config.storageKeySerializer, | ||
config.removeOnUndefined | ||
config.removeOnUndefined, | ||
config.syncCondition | ||
); | ||
@@ -280,2 +295,3 @@ return nextState; | ||
storageKeySerializer?: (key: string) => string; | ||
syncCondition?: (state: any) => any; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56243
928
114