ngrx-store-localstorage
Advanced tools
Comparing version 0.1.6 to 0.1.7
export declare const dateReviver: (key: string, value: any) => any; | ||
export declare const rehydrateApplicationState: (keys: any[], storage: Storage) => any; | ||
export declare const syncStateUpdate: (state: any, keys: any[], storage: Storage, removeOnUndefined: boolean) => void; | ||
export declare const localStorageSync: (keys: any[], rehydrate?: boolean, storage?: Storage, removeOnUndefined?: boolean) => (reducer: any) => (state: any, action: any) => any; | ||
export declare const localStorageSync: (config: LocalStorageConfig) => (reducer: any) => (state: any, action: any) => any; | ||
export declare const localStorageSyncAndClean: (keys: any[], rehydrate?: boolean, removeOnUndefined?: boolean) => (reducer: any) => any; | ||
export interface LocalStorageConfig { | ||
keys: any[]; | ||
rehydrate?: boolean; | ||
storage?: Storage; | ||
removeOnUndefined?: boolean; | ||
} |
@@ -145,25 +145,25 @@ "use strict"; | ||
}; | ||
exports.localStorageSync = function (keys, rehydrate, storage, removeOnUndefined) { | ||
if (rehydrate === void 0) { rehydrate = false; } | ||
if (storage === void 0) { storage = localStorage; } | ||
if (removeOnUndefined === void 0) { removeOnUndefined = false; } | ||
return function (reducer) { | ||
var stateKeys = validateStateKeys(keys); | ||
var rehydratedState = rehydrate ? exports.rehydrateApplicationState(stateKeys, storage) : undefined; | ||
return function (state, action) { | ||
if (state === void 0) { state = rehydratedState; } | ||
/* | ||
Handle case where state is rehydrated AND initial state is supplied. | ||
Any additional state supplied will override rehydrated state for the given key. | ||
*/ | ||
if (action.type === INIT_ACTION && rehydratedState) { | ||
state = Object.assign({}, state, rehydratedState); | ||
} | ||
var nextState = reducer(state, action); | ||
exports.syncStateUpdate(nextState, stateKeys, storage, removeOnUndefined); | ||
return nextState; | ||
}; | ||
exports.localStorageSync = function (config) { return function (reducer) { | ||
if (config.storage === undefined) { | ||
config.storage = localStorage || window.localStorage; | ||
} | ||
var stateKeys = validateStateKeys(config.keys); | ||
var rehydratedState = config.rehydrate ? exports.rehydrateApplicationState(stateKeys, config.storage) : undefined; | ||
return function (state, action) { | ||
if (state === void 0) { state = rehydratedState; } | ||
/* | ||
Handle case where state is rehydrated AND initial state is supplied. | ||
Any additional state supplied will override rehydrated state for the given key. | ||
*/ | ||
if (action.type === INIT_ACTION && rehydratedState) { | ||
state = Object.assign({}, state, rehydratedState); | ||
} | ||
var nextState = reducer(state, action); | ||
exports.syncStateUpdate(nextState, stateKeys, config.storage, config.removeOnUndefined); | ||
return nextState; | ||
}; | ||
}; | ||
}; }; | ||
/* | ||
@deprecated: Use localStorageSync(LocalStorageConfig) | ||
Wraps localStorageSync functionality acepting the removeOnUndefined boolean parameter in order | ||
@@ -177,5 +177,11 @@ to clean/remove the state from the browser on situations like state reset or logout. | ||
return function (reducer) { | ||
return _this.localStorageSync(keys, rehydrate, localStorage, removeOnUndefined); | ||
var config = { | ||
keys: keys, | ||
rehydrate: rehydrate, | ||
storage: localStorage, | ||
removeOnUndefined: removeOnUndefined | ||
}; | ||
return _this.localStorageSync(config); | ||
}; | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "ngrx-store-localstorage", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "State and local storage syncing for @ngrx/store", | ||
@@ -42,2 +42,10 @@ "main": "./dist/index.js", | ||
"@types/core-js": "^0.9.35", | ||
"@types/crypto-js": "^3.1.33", | ||
"@types/jasmine": "^2.5.47", | ||
"@types/node": "^7.0.18", | ||
"crypto-js": "^3.1.9-1", | ||
"es6-promise": "^3.0.2", | ||
"es6-shim": "^0.35.0", | ||
"jasmine": "^2.4.1", | ||
"jasmine-core": "^2.4.1", | ||
"rimraf": "^2.5.4", | ||
@@ -44,0 +52,0 @@ "rxjs": "^5.1.1", |
@@ -12,4 +12,4 @@ # ngrx-store-localstorage | ||
1. Import `compose` and `combineReducers` from `@ngrx/store` and `@ngrx/core/compose`. | ||
2. Invoke the `localStorageSync` function after `combineReducers`, specifying the slices of state you would like to keep synced with local storage. | ||
3. Optionally specify whether to rehydrate this state from local storage as `initialState` on application bootstrap. | ||
2. Invoke the `localStorageSync` function after `combineReducers`, this receives a `LocalStorageConfig` object and assigns the property `keys` the slices of state you would like to keep synced with local storage. | ||
3. Optionally specify in the `LocalStorageConfig` whether to rehydrate this state from local storage as `initialState` on application bootstrap with the `rehydrateState` property. | ||
4. Invoke composed function with application reducers as an argument to `StoreModule.provideStore`. | ||
@@ -28,3 +28,3 @@ ```ts | ||
compose( | ||
localStorageSync(['todos']), | ||
localStorageSync({keys: ['todos']}), | ||
combineReducers | ||
@@ -39,14 +39,17 @@ )({todos, visibilityFilter}) | ||
## API | ||
### `localStorageSync(keys: any[], rehydrateState: boolean = false, storage: Storage = localStorage, removeOnUndefined: boolean = false): Reducer` | ||
Provide state (reducer) keys to sync with local storage. Optionally specify whether to rehydrate `initialState` from local storage on bootstrap. | ||
*Returns a meta-reducer*. | ||
### `localStorageSync(config: LocalStorageConfig): Reducer` | ||
Provide state (reducer) keys to sync with local storage. *Returns a meta-reducer*. | ||
#### Arguments | ||
* `keys` State keys to sync with local storage. The keys can be defined in two different formats: | ||
* \(*string[]*): Array of strings representing the state (reducer) keys. Full state will be synced (e.g. `localStorageSync(['todos'])`). | ||
* `config` An object that matches with the `LocalStorageConfig` interface, `keys` is the only required property. | ||
* \(*object[]*): Array of objects where for each object the key represents the state key and the value represents custom serialize/deserialize options. This can be one of the following: | ||
### **LocalStorageConfig** | ||
An interface that holds the needed configuration attributes to bootstrap `localStorageSync`. The following are properties which compose the `LocalStorageConfig`: | ||
* `keys` (required) State keys to sync with local storage. The keys can be defined in two different formats: | ||
* `string[]`: Array of strings representing the state (reducer) keys. Full state will be synced (e.g. `localStorageSync({keys: ['todos']})`). | ||
* An array of properties which should be synced. This allows for the partial state sync (e.g. `localStorageSync([{todos: ['name', 'status'] }, ... ])`). | ||
* `object[]`: Array of objects where for each object the key represents the state key and the value represents custom serialize/deserialize options. This can be one of the following: | ||
* An array of properties which should be synced. This allows for the partial state sync (e.g. `localStorageSync({keys: [{todos: ['name', 'status'] }, ... ]})`). | ||
* A reviver function as specified in the [JSON.parse documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). | ||
@@ -68,8 +71,10 @@ | ||
* `rehydrateState` \(*boolean? = false*): Pull initial state from local storage on startup. | ||
* `storage` \(*Storage? = localStorage*): Specify an object that conforms to the Storage interface to use, this will default localStorage. | ||
* `removeOnUndefined` \(*boolean? = false*): Specify if the state is removed from the storage when the new value is undefined. | ||
* `rehydrateState` (optional) `boolean`: Pull initial state from local storage on startup, this will default to `false`. | ||
* `storage` (optional) `Storage`: Specify an object that conforms to the Storage interface to use, this will default to `localStorage`. | ||
* `removeOnUndefined` (optional) `boolean`: Specify if the state is removed from the storage when the new value is undefined, this will default to `false`. | ||
--- | ||
### `localStorageSyncAndClean(keys: any[], rehydrate: boolean = false, removeOnUndefined: boolean = false): Reducer` | ||
### ~~`localStorageSyncAndClean(keys: any[], rehydrate: boolean = false, removeOnUndefined: boolean = false): Reducer`~~ | ||
**This function is deprecated and soon will be removed, please use _localStorageSync(LocalStorageConfig)_.** | ||
A shorthand that wraps the functionalities of `localStorageSync` and asumes `localStorage` as the storage. | ||
@@ -76,0 +81,0 @@ |
@@ -155,6 +155,11 @@ const INIT_ACTION = '@ngrx/store/init'; | ||
export const localStorageSync = (keys: any[], rehydrate: boolean = false, storage: Storage = localStorage, removeOnUndefined: boolean = false) => (reducer: any) => { | ||
const stateKeys = validateStateKeys(keys); | ||
const rehydratedState = rehydrate ? rehydrateApplicationState(stateKeys, storage) : undefined; | ||
export const localStorageSync = (config: LocalStorageConfig) => (reducer: any) => { | ||
if (config.storage === undefined) { | ||
config.storage = localStorage || window.localStorage; | ||
} | ||
const stateKeys = validateStateKeys(config.keys); | ||
const rehydratedState = config.rehydrate ? rehydrateApplicationState(stateKeys, config.storage) : undefined; | ||
return function (state = rehydratedState, action: any) { | ||
@@ -169,3 +174,3 @@ /* | ||
const nextState = reducer(state, action); | ||
syncStateUpdate(nextState, stateKeys, storage, removeOnUndefined); | ||
syncStateUpdate(nextState, stateKeys, config.storage, config.removeOnUndefined); | ||
return nextState; | ||
@@ -176,2 +181,4 @@ }; | ||
/* | ||
@deprecated: Use localStorageSync(LocalStorageConfig) | ||
Wraps localStorageSync functionality acepting the removeOnUndefined boolean parameter in order | ||
@@ -182,3 +189,18 @@ to clean/remove the state from the browser on situations like state reset or logout. | ||
export const localStorageSyncAndClean = (keys: any[], rehydrate: boolean = false, removeOnUndefined: boolean = false) => (reducer: any) => { | ||
return this.localStorageSync(keys, rehydrate, localStorage, removeOnUndefined); | ||
let config: LocalStorageConfig = { | ||
keys: keys, | ||
rehydrate: rehydrate, | ||
storage: localStorage, | ||
removeOnUndefined: removeOnUndefined | ||
}; | ||
return this.localStorageSync(config); | ||
}; | ||
export interface LocalStorageConfig { | ||
keys: any[]; | ||
rehydrate?: boolean; | ||
storage?: Storage; | ||
removeOnUndefined?: boolean; | ||
} |
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
48027
723
104
17