h5:add storage plugin

This commit is contained in:
pengfei.zhou
2019-12-25 15:19:41 +08:00
committed by osborn
parent 76e15e17ee
commit 48e6066e75
6 changed files with 95 additions and 20 deletions

View File

@@ -0,0 +1,41 @@
import { DoricPlugin } from "../DoricPlugin";
export class StoragePlugin extends DoricPlugin {
setItem(args: {
zone?: string,
key: string,
value: string
}) {
localStorage.setItem(`${args.zone}_${args.key}`, args.value)
return Promise.resolve()
}
getItem(args: {
zone?: string,
key: string,
}) {
return Promise.resolve(localStorage.getItem(`${args.zone}_${args.key}`))
}
remove(args: {
zone?: string,
key: string,
}) {
localStorage.removeItem(`${args.zone}_${args.key}`)
return Promise.resolve()
}
clear(args: {
zone: string,
}) {
let removingKeys = []
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i)
if (key && key.startsWith(`${args.zone}_`)) {
removingKeys.push(key)
}
}
removingKeys.forEach(e => {
localStorage.removeItem(e)
})
return Promise.resolve()
}
}