Skip to content

Commit

Permalink
fix: Removed custom store rehydration
Browse files Browse the repository at this point in the history
  • Loading branch information
joanllenas committed Mar 14, 2021
1 parent 29e1de9 commit 3aa9cb0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 143 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class AppComponent {

<a name="examples-demo" />

#### Demo
#### Demo 👀

- [Stackblitz Ngrx demo](https://stackblitz.com/edit/ngx-remotedata-demo?file=src%2Fapp%2Fmeow%2Freducer.ts)

Expand All @@ -185,7 +185,7 @@ export class AppComponent {

<a name="examples-ngrx" />

- [Ngrx (includes store rehydration with de/serialization)](src/app/examples/ngrx)
- [Ngrx](src/app/examples/ngrx) _(includes store rehydration from `localStorage`)_

<a name="api" />

Expand Down
34 changes: 25 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
"@angular/platform-browser": "~11.1.0",
"@angular/platform-browser-dynamic": "~11.1.0",
"@angular/router": "~11.1.0",
"@ngrx/effects": "^10.1.2",
"@ngrx/store": "^10.1.2",
"@ngrx/store-devtools": "^10.1.2",
"@ngrx/effects": "^11.0.1",
"@ngrx/store": "^11.0.1",
"@ngrx/store-devtools": "^11.0.1",
"ngrx-store-localstorage": "^11.0.0",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
Expand Down
52 changes: 2 additions & 50 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { StoreModule, ActionReducer, MetaReducer } from '@ngrx/store';
import { StoreModule, MetaReducer } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';

Expand All @@ -16,56 +16,8 @@ import { PosComponent } from './examples/pos/pos.component';
import { PosService } from './examples/pos/pos.service';
import { MeowEffects } from './examples/ngrx/store/effects';
import { BasicsComponent } from './examples/basics/basics.component';
import {
notAsked,
inProgress,
failure,
success,
RemoteData
} from 'ngx-remotedata';
import { localStorageSync } from './examples/ngrx/store/local-store-sync';
import { localStorageSyncReducer } from './examples/ngrx/store/localstorage-sync';

export function localStorageSyncReducer(
reducer: ActionReducer<any>
): ActionReducer<any> {
return localStorageSync({
rehydrate: true,
storageKey: 'myAppStorage',
keys: {
meow: {
serialize: (rd: RemoteData<any>) => rd,
deserialize: (json: {
tag: RemoteData<any>['tag'];
err: any;
val: any;
}) => {
const rd = [
{
matcher: (tag: RemoteData<any>['tag']) => tag === 'NotAsked',
mapper: () => notAsked()
},
{
matcher: (tag: RemoteData<any>['tag']) => tag === 'InProgress',
mapper: () => inProgress(json.val)
},
{
matcher: (tag: RemoteData<any>['tag']) => tag === 'Failure',
mapper: () => failure(json.err, json.val)
},
{
matcher: (tag: RemoteData<any>['tag']) => tag === 'Success',
mapper: () => success(json.val)
}
]
.filter(matchMap => matchMap.matcher(json.tag))
.map(matchMap => matchMap.mapper())
.pop();
return rd || notAsked();
}
}
}
})(reducer);
}
const metaReducers: Array<MetaReducer<any, any>> = [localStorageSyncReducer];

@NgModule({
Expand Down
79 changes: 0 additions & 79 deletions src/app/examples/ngrx/store/local-store-sync.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/app/examples/ngrx/store/localstorage-sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ActionReducer } from '@ngrx/store';
import { localStorageSync } from 'ngrx-store-localstorage';

/**
* Meta reducer used in app.module.ts to sync reducer keys to/from localStorage
*/
export function localStorageSyncReducer(
reducer: ActionReducer<any>
): ActionReducer<any> {
return localStorageSync({ rehydrate: true, keys: ['meow'] })(reducer);
}

0 comments on commit 3aa9cb0

Please sign in to comment.