@@ -6,13 +6,22 @@ import { fetchDatabase } from './api'
6
6
import { checksums , tables } from '#content/manifest'
7
7
8
8
let db : Database
9
- let dbPromise : Promise < Database >
9
+ const loadedCollections : Record < string , string > = { }
10
+ const dbPromises : Record < string , Promise < Database > > = { }
10
11
export function loadDatabaseAdapter < T > ( collection : T ) : DatabaseAdapter {
11
12
async function loadAdapter ( collection : T ) {
12
13
if ( ! db ) {
13
- dbPromise = dbPromise || loadAndInitializeDatabase ( collection )
14
- db = await dbPromise
14
+ dbPromises . _ = dbPromises . _ || initializeDatabase ( )
15
+ db = await dbPromises . _
16
+ dbPromises . _ = undefined
15
17
}
18
+ if ( ! loadedCollections [ String ( collection ) ] ) {
19
+ dbPromises [ String ( collection ) ] = dbPromises [ String ( collection ) ] || loadCollectionDatabase ( collection )
20
+ await dbPromises [ String ( collection ) ]
21
+ loadedCollections [ String ( collection ) ] = 'loaded'
22
+ dbPromises [ String ( collection ) ] = undefined
23
+ }
24
+
16
25
return db
17
26
}
18
27
@@ -42,7 +51,7 @@ export function loadDatabaseAdapter<T>(collection: T): DatabaseAdapter {
42
51
}
43
52
}
44
53
45
- async function loadAndInitializeDatabase < T > ( collection : T ) {
54
+ async function initializeDatabase ( ) {
46
55
if ( ! db ) {
47
56
const sqlite3InitModule = await import ( '@sqlite.org/sqlite-wasm' ) . then ( m => m . default )
48
57
// @ts -expect-error sqlite3ApiConfig is not defined in the module
@@ -63,7 +72,10 @@ async function loadAndInitializeDatabase<T>(collection: T) {
63
72
const sqlite3 = await sqlite3InitModule ( )
64
73
db = new sqlite3 . oo1 . DB ( )
65
74
}
75
+ return db
76
+ }
66
77
78
+ async function loadCollectionDatabase < T > ( collection : T ) {
67
79
// Do not initialize database with dump for preview mode
68
80
if ( window . sessionStorage . getItem ( 'previewToken' ) ) {
69
81
return db
0 commit comments