import { Injectable } from '@angular/core'; import { Storage } from '@ionic/storage'; import { BaseStore } from 'base-store'; import { FastStore } from "./fast-store"; import { BaseLog as Log } from 'base-log'; const log = new Log('Store'); let initFastNames = []; @Injectable() export class Store extends BaseStore{ constructor(public storage: Storage,public fastStore:FastStore) { super(); this.storage.ready().then((res)=>{ log.info('storage driver', res['driver'](), true); }); this.getInitFastNames().then((initNames:any)=>{ log.info('fastStore initNames', initNames, true); initFastNames = initNames || []; }); } // 初始化高速缓存 initFastStore = () => { return this.getInitFastNames().then((initNames:any = [])=>{ if(initNames && initNames.length>0){ return Promise.all(initNames.map((name)=>{ return this.get(name).then((value) => { return this.fastStore.set(name, value); }); })); }else{ return new Promise(()=>{}); } }); } // 获取高速存储初始化字段 getInitFastNames = () => { return this.get('initFastNames'); } // 设置高速存储初始化字段 setInitFastNames = (name) => { if(initFastNames.indexOf(name) == -1) initFastNames.push(name); return this.set('initFastNames',initFastNames); } /** * 写入本地存储 * @param name 存储字段名 * @param value 存储值 * @param fast 是否开启高速存储 * @param initFast 是否在启动时初始化高速存储 * @returns {Promise} */ set (name:string,value:any,fast?:boolean,initFast?:boolean) { if(fast) { this.fastStore.set(name, value); if(initFast) this.setInitFastNames(name); } return this.storage.set(name, value); } get (name:string,fast?:boolean) { if(fast){ const fastValue = this.fastStore.get(name); if(fastValue){ return new Promise((resolve)=>{ resolve(fastValue); }); }else{ return this.storage.get(name).then((value)=>{ return this.fastStore.set(name,value); }); } }else{ return this.storage.get(name); } } /** * 移除存储,同时移除高速存储和本地存储中的内容 * @param name 移除名称 * @param async 是否异步移除 * @returns {any} */ remove (name:string,async?:boolean) { const fastValue = this.fastStore.get(name); if(fastValue && async){ log.warn(`${name}字段异步remove,请注意潜在的风险!`); this.storage.remove(name); return new Promise(()=>{ return this.fastStore.remove(name); }) }else{ if(fastValue) this.fastStore.remove(name); return this.storage.remove(name); } } clear () { this.fastStore.clear(); return this.storage.clear() } length = (fast?:boolean) => { if(fast){ return new Promise((resolve)=>{ resolve(this.storage.length()); }) }else{ return this.storage.length() } } keys = (fast?:boolean) => { if(fast){ return new Promise((resolve)=>{ resolve(this.storage.keys()); }) }else{ return this.storage.keys(); } } forEach = (iteratorCallback,fast?:boolean) => { if(fast){ this.fastStore.forEach(iteratorCallback); }else{ this.storage.forEach(iteratorCallback); } } // 基础存储 base (storeName,fast?:boolean,initFast?:boolean){ return { get: (name:string) => { return this.get(storeName,fast).then((res)=>{ res = res || {}; return res[name]; }); }, getAll: (fast?:boolean) => { return this.get(storeName,fast).then((res) => { return res || {}; }); }, /** * @param async 是否异步写入 * @returns {Promise>} */ set: (name:string, data:any,async?:boolean) => { return this.get(storeName,fast).then((res) => { res = res || {}; res[name] = data; if(fast && async){ log.warn(`${name}字段异步set,请注意潜在的风险!`); this.set(storeName, res, fast, initFast); return data; }else{ return this.set(storeName, res, fast, initFast).then(() => { return data; }); } }); }, /** * @param async * @returns {Promise>} */ remove: (name:string,async?:boolean) => { return this.get(storeName,fast).then((res) => { res[name] = null; delete res[name]; if(fast && async){ log.warn(`${name}字段异步remove,请注意潜在的风险!`); this.set(storeName, res, fast, initFast); return res; }else{ return this.set(storeName, res, fast, initFast); } }); }, clear: () => { return this.clear(); } } } // 页面存储 page = (pageName:string) => { return this.base('page.' + pageName); } }