ngrx-store-localstorage
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -5,6 +5,5 @@ "use strict"; | ||
var INIT_ACTION = '@ngrx/store/init'; | ||
var detectDate = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/; | ||
// correctly parse dates from local storage | ||
exports.dateReviver = function (key, value) { | ||
if (typeof value === 'string' && (detectDate.test(value))) { | ||
if (typeof value === 'string' && (Date.parse(value))) { | ||
return new Date(value); | ||
@@ -11,0 +10,0 @@ } |
{ | ||
"name": "ngrx-store-localstorage", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "State and local storage syncing for @ngrx/store", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -5,3 +5,3 @@ # ngrx-store-localstorage | ||
## Dependencies | ||
`ngrx-store-localstorage` depends on [@ngrx/store](https://github.com/ngrx/store) and [Angular 2](https://github.com/angular/angular). | ||
`ngrx-store-localstorage` depends on [@ngrx/store](https://github.com/ngrx/store) and [Angular 2+](https://github.com/angular/angular). | ||
@@ -12,21 +12,27 @@ ## Usage | ||
``` | ||
1. Import `compose` and `combineReducers` from `@ngrx/store` and `@ngrx/core/compose`. | ||
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 `rehydrate` property. | ||
4. Invoke composed function with application reducers as an argument to `StoreModule.provideStore`. | ||
**UPDATE FOR NGRX 4** | ||
1. Wrap localStorageSync in an exported function. | ||
2. Include in your meta-reducers array in `StoreModule.forRoot`. | ||
```ts | ||
import { NgModule } from '@angular/core'; | ||
import { StoreModule, combineReducers } from '@ngrx/store'; | ||
import { compose } from '@ngrx/core/compose'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { StoreModule, ActionReducerMap, ActionReducer } from '@ngrx/store'; | ||
import { localStorageSync } from 'ngrx-store-localstorage'; | ||
import { todos, visibilityFilter } from './reducers'; | ||
import { reducers } from './reducers'; | ||
const reducers: ActionReducerMap<IState> = {todos, visibilityFilter}; | ||
export function localStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> { | ||
return localStorageSync({keys: ['todos']})(reducer); | ||
} | ||
const metaReducers: Array<ActionReducer<any, any>> = [localStorageSyncReducer]; | ||
@NgModule({ | ||
imports: [ | ||
BrowserModule, | ||
StoreModule.provideStore( | ||
compose( | ||
localStorageSync({keys: ['todos']}), | ||
combineReducers | ||
)({todos, visibilityFilter}) | ||
StoreModule.forRoot( | ||
reducers, | ||
{metaReducers} | ||
) | ||
@@ -33,0 +39,0 @@ ] |
const INIT_ACTION = '@ngrx/store/init'; | ||
const detectDate = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/; | ||
// correctly parse dates from local storage | ||
export const dateReviver = (key: string, value: any) => { | ||
if (typeof value === 'string' && (detectDate.test(value))) { | ||
if (typeof value === 'string' && (Date.parse(value))) { | ||
return new Date(value); | ||
@@ -207,2 +206,2 @@ } | ||
storageKeySerializer?: (key: string) => string; | ||
} | ||
} |
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
112
50584
755