- Notifications
You must be signed in to change notification settings - Fork14
⏱ WIP: Async Storage Plugin
License
ngxs-labs/async-storage-plugin
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Supports custom storage engine with async access
To install@ngxs-labs/async-storage-plugin run the following command:
npm install --save @ngxs-labs/async-storage-plugin#orif you use yarnyarn add @ngxs-labs/async-storage-plugin
Import the module into your root application module:
import{NgModule}from'@angular/core';import{NgxsModule}from'@ngxs/store';import{NgxsAsyncStoragePluginModule}from'@ngxs-labs/async-storage-plugin';@NgModule({imports:[NgxsModule.forRoot(states),NgxsAsyncStoragePluginModule.forRoot(YOUR_CUSTOM_ENGINE)]})exportclassAppModule{}
You can implement your own async storage engine by providing an engine that implementsAsyncStorageEngine:
exportclassMyAsyncStorageEngineimplementsAsyncStorageEngine{constructor(privatestorage:YourStorage){}length():Observable<number>{// Your logic here}getItem(key:any):Observable<any>{// Your logic here}setItem(key:any,val:any):void{// Your logic here}removeItem(key:any):void{// Your logic here}clear():void{// Your logic here}key(val:number):Observable<string>{// Your logic here}}@NgModule({imports:[NgxsModule.forRoot([]),NgxsAsyncStoragePluginModule.forRoot(MyAsyncStorageEngine)]})exportclassAppModule{}
If your async storage returns aPromise you can wrap calls withfrom(storage.length()) fromrxjs.
Here is an example implementation of theAsyncStorageEngine using the Ionic Storage.You can find theStorageService in the integration project.
import{Injectable,NgModule}from'@angular/core';import{IonicStorageModule,Storage}from'@ionic/storage';import{AsyncStorageEngine,NgxsAsyncStoragePluginModule}from'@ngxs-labs/async-storage-plugin';import{NgxsModule}from'@ngxs/store';import{from,Observable}from'rxjs';@Injectable({providedIn:'root'})exportclassStorageServiceimplementsAsyncStorageEngine{constructor(privatestorage:Storage){}length():Observable<number>{returnfrom(this.storage.length());}getItem(key:any):Observable<any>{returnfrom(this.storage.get(key));}setItem(key:any,val:any):void{this.storage.set(key,val);}removeItem(key:any):void{this.storage.remove(key);}clear():void{this.storage.clear();}key(val:number):Observable<string>{returnfrom(this.storage.keys().then(keys=>keys[val]));}}@NgModule({imports:[NgxsModule.forRoot([]),NgxsAsyncStoragePluginModule.forRoot(StorageService),IonicStorageModule.forRoot()]})exportclassAppModule{}
This plugin provides the same options and migration settings as theStorage Plugin. SeeOptions andMigrations here.
About
⏱ WIP: Async Storage Plugin
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.
