Skip to content

Commit ddbb3f8

Browse files
committed
调整纪录清除代码
1 parent 02b9957 commit ddbb3f8

3 files changed

Lines changed: 63 additions & 12 deletions

File tree

src/app/cache.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ class ExtCache implements CacheStorage {
8181
});
8282
}
8383

84+
dels(keys: string[]): Promise<void> {
85+
return new Promise((resolve) => {
86+
chrome.storage.session.remove(keys, () => {
87+
const lastError = chrome.runtime.lastError;
88+
if (lastError) {
89+
console.error("chrome.runtime.lastError in chrome.storage.session.remove:", lastError);
90+
// 无视storage API错误,继续执行
91+
}
92+
resolve();
93+
});
94+
});
95+
}
96+
8497
clear(): Promise<void> {
8598
return new Promise((resolve) => {
8699
chrome.storage.session.clear(() => {

src/app/service/service_worker/script.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,6 @@ export class ScriptService {
219219
const uuid = uuidv4();
220220
try {
221221
await this.openUpdateOrInstallPage(uuid, url, source, false);
222-
timeoutExecution(
223-
`${cIdKey}_cleanup_${uuid}`,
224-
() => {
225-
// 清理缓存
226-
cacheInstance.del(`${CACHE_KEY_SCRIPT_INFO}${uuid}`);
227-
},
228-
30 * 1000
229-
);
230222
return `/src/install.html?uuid=${uuid}`;
231223
} catch (err: any) {
232224
console.error(err);

src/pages/install/App.tsx

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,50 @@ const fetchScriptBody = async (url: string, { onProgress }: { [key: string]: any
146146
return { code, metadata };
147147
};
148148

149+
const cleanupStaleInstallInfo = (uuid: string) => {
150+
// 頁面打開時不清除當前uuid,每30秒更新一次記錄
151+
const f = () => {
152+
cacheInstance.tx(`scriptInfoKeeps`, (val: Record<string, number> | undefined, tx) => {
153+
val = val || {};
154+
val[uuid] = Date.now();
155+
tx.set(val);
156+
});
157+
};
158+
f();
159+
setInterval(f, 30_000);
160+
161+
// 頁面打開後清除舊記錄
162+
const delay = Math.floor(5000 * Math.random()) + 10000; // 使用乱数时间避免瀏览器重啟时大量Tabs同时执行清除
163+
timeoutExecution(
164+
`${cIdKey}cleanupStaleInstallInfo`,
165+
() => {
166+
cacheInstance
167+
.tx(`scriptInfoKeeps`, (val: Record<string, number> | undefined, tx) => {
168+
const now = Date.now();
169+
const keeps = new Set<string>();
170+
const out: Record<string, number> = {};
171+
for (const [k, ts] of Object.entries(val ?? {})) {
172+
if (ts > 0 && now - ts < 60_000) {
173+
keeps.add(`${CACHE_KEY_SCRIPT_INFO}${k}`);
174+
out[k] = ts;
175+
}
176+
}
177+
tx.set(out);
178+
return keeps;
179+
})
180+
.then(async (keeps) => {
181+
const list = await cacheInstance.list();
182+
const filtered = list.filter((key) => key.startsWith(CACHE_KEY_SCRIPT_INFO) && !keeps.has(key));
183+
if (filtered.length) {
184+
// 清理缓存
185+
cacheInstance.dels(filtered);
186+
}
187+
});
188+
},
189+
delay
190+
);
191+
};
192+
149193
const cIdKey = `(cid_${Math.random()})`;
150194

151195
function App() {
@@ -198,8 +242,14 @@ function App() {
198242
return;
199243
}
200244

245+
if (window.history.length > 1) {
246+
setDoBackwards(true);
247+
}
248+
setLoaded(true);
249+
201250
if (uuid) {
202251
const cachedInfo = await scriptClient.getInstallInfo(uuid);
252+
cleanupStaleInstallInfo(uuid);
203253
if (cachedInfo?.[0]) isKnownUpdate = true;
204254
info = cachedInfo?.[1] || undefined;
205255
if (!info) {
@@ -261,10 +311,6 @@ function App() {
261311
}
262312
diffCode = prepare.oldScriptCode;
263313
}
264-
if (window.history.length > 1) {
265-
setDoBackwards(true);
266-
}
267-
setLoaded(true);
268314
setScriptCode(code);
269315
setDiffCode(diffCode);
270316
setOldScriptVersion(typeof oldVersion === "string" ? oldVersion : null);

0 commit comments

Comments
 (0)