Skip to content

Commit

Permalink
feat(integration): add migration example using key
Browse files Browse the repository at this point in the history
  • Loading branch information
marcjulian committed Dec 12, 2019
1 parent 7d4bff3 commit 727b02f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
17 changes: 16 additions & 1 deletion integration/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,22 @@ import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
IonicStorageModule.forRoot(),
NgxsModule.forRoot([CounterState]),
NgxsResetPluginModule.forRoot(),
NgxsAsyncStoragePluginModule.forRoot(StorageService),
NgxsAsyncStoragePluginModule.forRoot(StorageService, {
key: ['counter'],
migrations: [
{
version: 0,
key: 'counter',
migrate(state) {
state = {
count: 5,
version: 1
};
return state;
}
}
]
}),
NgxsLoggerPluginModule.forRoot({ disabled: environment.production })
],
providers: [
Expand Down
3 changes: 1 addition & 2 deletions integration/app/home/home.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

<ion-content class="ion-padding">
<ng-container *ngIf="counter$ | async as counter">
<h1>{{ counter }}</h1>
<h1>{{ counter.count }}</h1>
</ng-container>
<ion-button (click)="increment()">Increment</ion-button>
<ion-button (click)="decrement()">Decrement</ion-button>


<ion-button (click)="resetState()">
Reset State
</ion-button>
Expand Down
5 changes: 3 additions & 2 deletions integration/app/home/home.page.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CounterStateModel } from './../store/counter.state';
import { Component } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { CounterState, Increment, Decrement } from '../store/counter.state';
Expand All @@ -11,9 +12,9 @@ import { StateResetAll } from 'ngxs-reset-plugin';
})
export class HomePage {
@Select(CounterState)
public counter$: Observable<number>;
public counter$: Observable<CounterStateModel>;

constructor(private store: Store) {}
constructor(private store: Store) { }

increment() {
this.store.dispatch(new Increment());
Expand Down
22 changes: 16 additions & 6 deletions integration/app/store/counter.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ export class Increment {
export class Decrement {
public static readonly type = '[Counter] Decrement';
}
@State<number>({

export interface CounterStateModel {
count: number;
version: number;
}

@State<CounterStateModel>({
name: 'counter',
defaults: 0
defaults: {
count: 0,
version: 0
}
})
export class CounterState {
@Action(Increment)
public increment({ setState, getState }: StateContext<number>) {
setState(getState() + 1);
public increment({ patchState, getState }: StateContext<CounterStateModel>) {
patchState({ count: getState().count + 1 });
}

@Action(Decrement)
public decrement({ setState, getState }: StateContext<number>) {
setState(getState() - 1);
public decrement({ patchState, getState }: StateContext<CounterStateModel>) {
patchState({ count: getState().count - 1 });
}
}

0 comments on commit 727b02f

Please sign in to comment.