@@ -10,8 +10,9 @@ export type MirabufCacheID = string
10
10
11
11
export interface MirabufCacheInfo {
12
12
id : MirabufCacheID
13
- cacheKey : string
14
13
miraType : MiraType
14
+ cacheKey : string
15
+ buffer ?: ArrayBuffer
15
16
name ?: string
16
17
thumbnailStorageID ?: string
17
18
}
@@ -29,10 +30,10 @@ const root = await navigator.storage.getDirectory()
29
30
const robotFolderHandle = await root . getDirectoryHandle ( robotsDirName , { create : true } )
30
31
const fieldFolderHandle = await root . getDirectoryHandle ( fieldsDirName , { create : true } )
31
32
32
- export const backUpRobots : Map < MirabufCacheID , ArrayBuffer > = new Map < MirabufCacheID , ArrayBuffer > ( )
33
- export const backUpFields : Map < MirabufCacheID , ArrayBuffer > = new Map < MirabufCacheID , ArrayBuffer > ( )
33
+ export let backUpRobots : MapCache = { }
34
+ export let backUpFields : MapCache = { }
34
35
35
- const canOPFS = await ( async ( ) => {
36
+ export const canOPFS = await ( async ( ) => {
36
37
try {
37
38
if ( robotFolderHandle . name == robotsDirName ) {
38
39
robotFolderHandle . entries
@@ -52,6 +53,21 @@ const canOPFS = await (async () => {
52
53
}
53
54
} catch ( e ) {
54
55
console . log ( `No access to OPFS` )
56
+
57
+ // Copy-pasted from RemoveAll()
58
+ for await ( const key of robotFolderHandle . keys ( ) ) {
59
+ robotFolderHandle . removeEntry ( key )
60
+ }
61
+ for await ( const key of fieldFolderHandle . keys ( ) ) {
62
+ fieldFolderHandle . removeEntry ( key )
63
+ }
64
+
65
+ window . localStorage . setItem ( robotsDirName , "{}" )
66
+ window . localStorage . setItem ( fieldsDirName , "{}" )
67
+
68
+ backUpRobots = { }
69
+ backUpFields = { }
70
+
55
71
return false
56
72
}
57
73
} ) ( )
@@ -75,11 +91,10 @@ class MirabufCachingService {
75
91
*/
76
92
public static GetCacheMap ( miraType : MiraType ) : MapCache {
77
93
if (
78
- ( window . localStorage . getItem ( MIRABUF_LOCALSTORAGE_GENERATION_KEY ) ?? "" ) = = MIRABUF_LOCALSTORAGE_GENERATION
94
+ ( window . localStorage . getItem ( MIRABUF_LOCALSTORAGE_GENERATION_KEY ) ?? "" ) ! = MIRABUF_LOCALSTORAGE_GENERATION
79
95
) {
80
96
window . localStorage . setItem ( MIRABUF_LOCALSTORAGE_GENERATION_KEY , MIRABUF_LOCALSTORAGE_GENERATION )
81
- window . localStorage . setItem ( robotsDirName , "{}" )
82
- window . localStorage . setItem ( fieldsDirName , "{}" )
97
+ this . RemoveAll ( )
83
98
return { }
84
99
}
85
100
@@ -188,16 +203,19 @@ class MirabufCachingService {
188
203
try {
189
204
const map : MapCache = this . GetCacheMap ( miraType )
190
205
const id = map [ key ] . id
206
+ const _buffer = miraType == MiraType . ROBOT ? backUpRobots [ id ] . buffer : backUpFields [ id ] . buffer
191
207
const _name = map [ key ] . name
192
208
const _thumbnailStorageID = map [ key ] . thumbnailStorageID
193
209
const info : MirabufCacheInfo = {
194
210
id : id ,
195
211
cacheKey : key ,
196
212
miraType : miraType ,
213
+ buffer : _buffer ,
197
214
name : name ?? _name ,
198
215
thumbnailStorageID : thumbnailStorageID ?? _thumbnailStorageID ,
199
216
}
200
217
map [ key ] = info
218
+ miraType == MiraType . ROBOT ? ( backUpRobots [ id ] = info ) : ( backUpFields [ id ] = info )
201
219
window . localStorage . setItem ( miraType == MiraType . ROBOT ? robotsDirName : fieldsDirName , JSON . stringify ( map ) )
202
220
return true
203
221
} catch ( e ) {
@@ -243,7 +261,7 @@ class MirabufCachingService {
243
261
// Get buffer from hashMap. If not in hashMap, check OPFS. Otherwise, buff is undefined
244
262
const cache = miraType == MiraType . ROBOT ? backUpRobots : backUpFields
245
263
const buff =
246
- cache . get ( id ) ??
264
+ cache [ id ] ?. buffer ??
247
265
( await ( async ( ) => {
248
266
const fileHandle = canOPFS
249
267
? await ( miraType == MiraType . ROBOT ? robotFolderHandle : fieldFolderHandle ) . getFileHandle ( id , {
@@ -299,7 +317,7 @@ class MirabufCachingService {
299
317
300
318
const backUpCache = miraType == MiraType . ROBOT ? backUpRobots : backUpFields
301
319
if ( backUpCache ) {
302
- backUpCache . delete ( id )
320
+ delete backUpCache [ id ]
303
321
}
304
322
305
323
World . AnalyticsSystem ?. Event ( "Cache Remove" , {
@@ -318,18 +336,20 @@ class MirabufCachingService {
318
336
* Removes all Mirabuf files from the caching services. Mostly for debugging purposes.
319
337
*/
320
338
public static async RemoveAll ( ) {
321
- for await ( const key of robotFolderHandle . keys ( ) ) {
322
- robotFolderHandle . removeEntry ( key )
323
- }
324
- for await ( const key of fieldFolderHandle . keys ( ) ) {
325
- fieldFolderHandle . removeEntry ( key )
339
+ if ( canOPFS ) {
340
+ for await ( const key of robotFolderHandle . keys ( ) ) {
341
+ robotFolderHandle . removeEntry ( key )
342
+ }
343
+ for await ( const key of fieldFolderHandle . keys ( ) ) {
344
+ fieldFolderHandle . removeEntry ( key )
345
+ }
326
346
}
327
347
328
- window . localStorage . removeItem ( robotsDirName )
329
- window . localStorage . removeItem ( fieldsDirName )
348
+ window . localStorage . setItem ( robotsDirName , "{}" )
349
+ window . localStorage . setItem ( fieldsDirName , "{}" )
330
350
331
- backUpRobots . clear ( )
332
- backUpFields . clear ( )
351
+ backUpRobots = { }
352
+ backUpFields = { }
333
353
}
334
354
335
355
// Optional name for when assembly is being decoded anyway like in CacheAndGetLocal()
@@ -339,19 +359,19 @@ class MirabufCachingService {
339
359
miraType ?: MiraType ,
340
360
name ?: string
341
361
) : Promise < MirabufCacheInfo | undefined > {
342
- const backupID = Date . now ( ) . toString ( )
343
362
try {
363
+ const backupID = Date . now ( ) . toString ( )
344
364
if ( ! miraType ) {
345
- console . log ( "Double loading" )
365
+ console . debug ( "Double loading" )
346
366
miraType = this . AssemblyFromBuffer ( miraBuff ) . dynamic ? MiraType . ROBOT : MiraType . FIELD
347
367
}
348
368
349
369
// Local cache map
350
370
const map : MapCache = this . GetCacheMap ( miraType )
351
371
const info : MirabufCacheInfo = {
352
372
id : backupID ,
353
- cacheKey : key ,
354
373
miraType : miraType ,
374
+ cacheKey : key ,
355
375
name : name ,
356
376
}
357
377
map [ key ] = info
@@ -377,7 +397,14 @@ class MirabufCachingService {
377
397
378
398
// Store in hash
379
399
const cache = miraType == MiraType . ROBOT ? backUpRobots : backUpFields
380
- cache . set ( backupID , miraBuff )
400
+ const mapInfo : MirabufCacheInfo = {
401
+ id : backupID ,
402
+ miraType : miraType ,
403
+ cacheKey : key ,
404
+ buffer : miraBuff ,
405
+ name : name ,
406
+ }
407
+ cache [ backupID ] = mapInfo
381
408
382
409
return info
383
410
} catch ( e ) {
0 commit comments