|
| 1 | +import { map } from 'rxjs/operators'; |
1 | 2 | /* eslint-disable no-console */ |
2 | 3 | /* eslint-disable spaced-comment */ |
3 | 4 | /* eslint-disable @typescript-eslint/explicit-function-return-type */ |
@@ -128,6 +129,12 @@ addEventListener('message', e => { |
128 | 129 | db1.addNonAligneImage(_sqlite3, e.data); |
129 | 130 | break; |
130 | 131 | } |
| 132 | + case 'cleanOutDatedCached': { |
| 133 | + console.error('cleanOutDatedCached', e.data); |
| 134 | + cleanOutdatedCache(e.data); |
| 135 | + |
| 136 | + break; |
| 137 | + } |
131 | 138 |
|
132 | 139 | case 'resetDatabase': { |
133 | 140 | let db1 = dbs.get(e.data.payload.examId); |
@@ -480,6 +487,32 @@ addEventListener('message', e => { |
480 | 487 | } |
481 | 488 | }); |
482 | 489 |
|
| 490 | +async function cleanOutdatedCache(data: any) { |
| 491 | + try { |
| 492 | + console.error('cleanOutdatedCache for ', data); |
| 493 | + |
| 494 | + const examIds = data.payload.examIds; |
| 495 | + const cacheNames: string[] = examIds.map((id: number) => '' + id + '.sqlite3'); |
| 496 | + const cacheJournalNames = examIds.map((id: number) => '' + id + '.sqlite3-journal'); |
| 497 | + cacheNames.push(...cacheJournalNames); |
| 498 | + if (navigator.storage.getDirectory !== undefined) { |
| 499 | + const root = await navigator.storage.getDirectory(); |
| 500 | + const keys = (root as any).keys(); |
| 501 | + let next = await keys.next(); |
| 502 | + while (next.value) { |
| 503 | + if (next.value.endsWith('.sqlite3') || (next.value.endsWith('.sqlite3-journal') && !cacheNames.includes(next.value))) { |
| 504 | + await root.removeEntry(next.value); |
| 505 | + } |
| 506 | + next = await keys.next(); |
| 507 | + } |
| 508 | + } |
| 509 | + } finally { |
| 510 | + postMessage({ |
| 511 | + msg: data.msg, |
| 512 | + uid: data.uid, |
| 513 | + }); |
| 514 | + } |
| 515 | +} |
483 | 516 | /* |
484 | 517 | addTemplate(elt: AlignImage ): Promise<number>; |
485 | 518 | countNonAlignWithPageNumber(examId:number, pageInscan:number): Promise<number>; |
@@ -623,9 +656,9 @@ class DB { |
623 | 656 | const keys = (root as any).keys(); |
624 | 657 | let next = await keys.next(); |
625 | 658 | while (next.value !== undefined) { |
626 | | - if (next.value.endsWith('.sqlite3')) { |
| 659 | + if (next.value === this.examName + '.sqlite3') { |
627 | 660 | await root.removeEntry(next.value); |
628 | | - } else if (next.value.endsWith('sqlite3-journal')) { |
| 661 | + } else if (next.value === this.examName + 'sqlite3-journal') { |
629 | 662 | await root.removeEntry(next.value); |
630 | 663 | } |
631 | 664 | next = await keys.next(); |
|
0 commit comments