-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathcache.ts
More file actions
186 lines (172 loc) · 5.38 KB
/
Copy pathcache.ts
File metadata and controls
186 lines (172 loc) · 5.38 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { stackAsyncTask } from "@App/pkg/utils/async_queue";
interface CacheStorage {
get<T>(key: string): Promise<T | undefined>;
set<T>(key: string, value: T): Promise<void>;
batchSet(data: { [key: string]: any }): Promise<void>;
has(key: string): Promise<boolean>;
del(key: string): Promise<void>;
clear(): Promise<void>;
list(): Promise<string[]>;
}
class ExtCache implements CacheStorage {
get<T>(key: string): Promise<T | undefined> {
return new Promise((resolve) => {
chrome.storage.session.get(key, (value) => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.storage.session.get:", lastError);
// 无视storage API错误,继续执行
}
resolve(value[key]);
});
});
}
set<T>(key: string, value: T): Promise<void> {
return new Promise((resolve) => {
chrome.storage.session.set(
{
[key]: value,
},
() => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.storage.session.set:", lastError);
// 无视storage API错误,继续执行
}
resolve();
}
);
});
}
batchSet(data: { [key: string]: any }): Promise<void> {
return new Promise((resolve) => {
chrome.storage.session.set(data, () => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.storage.session.set:", lastError);
// 无视storage API错误,继续执行
}
resolve();
});
});
}
has(key: string): Promise<boolean> {
return new Promise((resolve) => {
chrome.storage.session.get(key, (value) => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.storage.session.get:", lastError);
// 无视storage API错误,继续执行
}
resolve(value[key] !== undefined);
});
});
}
del(key: string): Promise<void> {
return new Promise((resolve) => {
chrome.storage.session.remove(key, () => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.storage.session.remove:", lastError);
// 无视storage API错误,继续执行
}
resolve();
});
});
}
dels(keys: string[]): Promise<void> {
return new Promise((resolve) => {
chrome.storage.session.remove(keys, () => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.storage.session.remove:", lastError);
// 无视storage API错误,继续执行
}
resolve();
});
});
}
clear(): Promise<void> {
return new Promise((resolve) => {
chrome.storage.session.clear(() => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.storage.session.clear:", lastError);
// 无视storage API错误,继续执行
}
resolve();
});
});
}
list(): Promise<string[]> {
return new Promise((resolve) => {
chrome.storage.session.get(null, (value) => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.storage.session.get:", lastError);
// 无视storage API错误,继续执行
}
resolve(Object.keys(value));
});
});
}
}
class Cache extends ExtCache {
public async getOrSet<T>(key: string, set: () => Promise<T> | T): Promise<T> {
let ret = await this.get<T>(key);
if (!ret) {
ret = await set();
this.set(key, ret);
}
return ret;
}
// 事务处理,如果有事务正在进行,则等待
public tx<T, CB extends (val: T | undefined, tx: { set: (newVal: T) => void; del: () => void }) => any>(
key: string,
callback: CB
): Promise<Awaited<ReturnType<CB>>> {
const enum Actions {
NONE = 0,
SET = 1,
DEL = 2,
}
return stackAsyncTask(key, () => {
let ret: Awaited<ReturnType<CB>>;
const act = { action: Actions.NONE } as { action?: number; newVal?: T };
const set = (newVal: T) => {
act.action = Actions.SET;
act.newVal = newVal;
};
const del = () => {
act.action = Actions.DEL;
act.newVal = undefined;
};
return this.get<T>(key)
.then((result) => {
const tx = {
set,
del,
};
return callback(result, tx);
})
.then((result) => {
ret = result;
if (act.action === Actions.SET) {
return this.set(key, act.newVal);
} else if (act.action === Actions.DEL) {
return this.del(key);
}
})
.then(() => ret);
});
}
incr(key: string, increase: number): Promise<number> {
return this.tx(key, (value: number | undefined, tx) => {
value = (value || 0) + increase;
tx.set(value);
return value;
});
}
}
export type ICacheSet<T> = (result: T | undefined) => Promise<T | undefined> | T | undefined;
export const cacheInstance = new Cache();