@@ -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+
149193const cIdKey = `(cid_${ Math . random ( ) } )` ;
150194
151195function 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