2021年3月30日星期二

IONIC and NGRX set initial state from indexed db

I'm working in an Ionic app with angular and I want my state to be persistent so I added the plugin @ionic/storage-angular to save the current state and when I restart the app I want to set in ngrx the state previously saved.

So I created my storage service:

@Injectable({      providedIn: 'root'  })  export class StorageService {      state: AppState;      constructor(private storage: Storage, public store: Store<AppState>) {        this.createDBStorage();        this.loadStorage();      }        async createDBStorage() {        await this.storage.create();      }        async loadStorage() {        const dbState = await this.storage.get('dt-config');        if (dbState) {          this.state = dbState;        } else {          this.state = null;        }      }          getState() {        return this.state || null;      }        saveState(newState: AppState) {        this.storage.set('dt-config', newState);        this.state = newState;      }  }  

I researched about how to set the initial state from my indexed db and I found this ---> How to set initialState in @ngrx/core with a promise from indexedDB but the answer is from three years ago.

According to that answer there is something like this to set the initial state in app.module.ts

export function getInitialState(): Promise<any> {    return StorageService.dB.loadInitialState('app-state');  }    @NgModule({    imports: [      ...      StoreModule.forRoot(reducers, {initialState: getInitialState}),      ...    ], bootstrap: [AppComponent],  })  export class AppModule {}  

And that's exactly what I need the problem is how to call my service or if there is other way to access the item saved in the indexed db?

https://stackoverflow.com/questions/66879576/ionic-and-ngrx-set-initial-state-from-indexed-db March 31, 2021 at 07:05AM

没有评论:

发表评论