-
Notifications
You must be signed in to change notification settings - Fork 1
/
chrome-storage.js
141 lines (98 loc) · 3.4 KB
/
chrome-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class ChromeStorage {
constructor(storageType = 'local') {
if (!chrome.storage) throw 'invalid chrome.storage'
this.STORAGE_TYPE = storageType
}
_get(...args) {
chrome.storage[this.STORAGE_TYPE].get(...args)
}
_set(...args) {
chrome.storage[this.STORAGE_TYPE].set(...args)
}
_remove(...args) {
chrome.storage[this.STORAGE_TYPE].remove(...args)
}
clear() {
return new Promise(resolve => chrome.storage[this.STORAGE_TYPE].clear(resolve))
}
headKey(keys) {
return keys[0]
}
is_nestedKey(keys) {
return keys.length > 1
}
parser(queries) {
return queries.split(/\//g)
}
refer(obj, keys) {
return keys.reduce((acc, cur, i) => {
keys.length -1 !== i &&
(acc.temp = acc.temp[cur] = typeof acc.temp[cur] !== 'object' ? {} : acc.temp[cur] || {})
return acc
}, {
key : keys.slice(-1)[0],
temp : obj = typeof obj === 'object' ? obj : {},
obj
})
}
async deliver(key) {
let keys = this.parser(key),
head = this.headKey(keys),
data = await this.get(head)
keys = keys.slice(1)
return {
keys,
data,
set : data => new Promise(resolve => this._set({ [head] : data }, resolve)),
handleRefer : callback => {
let { obj, temp, key } = this.refer(data, keys)
callback(temp, key)
return obj
}
}
}
getAll() {
return new Promise(resolve => this._get(resolve))
}
get(key) {
if (typeof key !== 'string' || !key) throw 'invalid key'
const keys = this.parser(key)
return new Promise(resolve =>
this._get(this.headKey(keys), e =>
resolve(
this.is_nestedKey(keys) ?
keys.reduce((acc, cur) => acc[cur], e)
:
e[key])))
}
async set(key, value) {
if (typeof key !== 'string' || !key) throw 'invalid key'
const { keys, set, handleRefer } = await this.deliver(key)
await set(!keys.length ? value : handleRefer((temp, key) => temp[key] = value))
}
async push(key, value) {
if (typeof key !== 'string' || !key) throw 'invalid key'
let { data, keys, set, handleRefer } = await this.deliver(key),
verify = (data) => {
if (data === 0 || (data && !Array.isArray(data))) throw `must be an array type. (key data: ${ JSON.stringify(data) })`
if (!data) return false
if (Array.isArray(data)) return true
}
!keys.length ?
verify(data) ? data.push(value) : (data = [], data.push(value))
:
data = handleRefer((temp, key) => verify(temp[key]) ? temp[key].push(value) : (temp[key] = [], temp[key].push(value)))
await set(data)
}
remove(key) {
return new Promise(resolve => {
if (typeof key !== 'string' || !key) throw 'invalid key'
this.deliver(key)
.then(({ keys, set, handleRefer }) =>
!keys.length ?
this._remove(key, resolve)
:
set(handleRefer((temp, key) => delete temp[key])).then(resolve))
})
}
}