'use strict'; import { BaseStore } from 'base-store'; import { Promise } from 'ES6Promise'; var localStorage = window.localStorage; export class Store implements BaseStore { get = (name) => { var value = localStorage.getItem(name); if(typeof value == 'string'){ value = value.indexOf('{')>-1 && value.indexOf('}')>-1 ? JSON.parse(value) : value; } return Promise.resolve(value); } set = (name,value) => { var storeValue = typeof value == 'object' ? JSON.stringify(value) : value; localStorage.setItem(name,storeValue); return Promise.resolve(value); } base = (storeName) => { return { get: (name:string) => { return this.get(storeName).then((res)=>{ res = res || {}; return res[name]; }); }, getAll: () => { return this.get(storeName).then((res) => { return res || {}; }); }, /** * @param async 是否异步写入 * @returns {Promise>} */ set: (name:string, data:any) => { return this.get(storeName).then((res) => { res = res || {}; res[name] = data; return this.set(storeName, res).then(() => { return data; }); }); }, /** * @param async * @returns {Promise>} */ remove: (name:string) => { return this.get(storeName).then((res) => { res[name] = null; delete res[name]; return this.set(storeName, res).then(() => { return null; }); }); }, clear: () => { return this.clear(); } } } remove = (name) => { localStorage.removeItem(name); return Promise.resolve(null); } clear = () => { localStorage.clear(); return Promise.resolve(null); } }