@@ -18,11 +18,11 @@ globalThis.saveDatabase = async () => {
1818} ;
1919
2020// IndexedDB helpers
21- const IDB_DB = 'avalonia-sqlite3' ;
22- const IDB_STORE = 'dbfiles' ;
23- const IDB_KEY = 'todo.db' ;
21+ export const IDB_DB = 'avalonia-sqlite3' ;
22+ export const IDB_STORE = 'dbfiles' ;
23+ export const IDB_KEY = 'todo.db' ;
2424
25- function idbOpen ( ) {
25+ export function idbOpen ( ) {
2626 return new Promise ( ( resolve , reject ) => {
2727 const req = indexedDB . open ( IDB_DB , 1 ) ;
2828 req . onupgradeneeded = ( ) => req . result . createObjectStore ( IDB_STORE ) ;
@@ -31,7 +31,7 @@ function idbOpen() {
3131 } ) ;
3232}
3333
34- async function idbPut ( key , uint8 ) {
34+ export async function idbPut ( key , uint8 ) {
3535 const db = await idbOpen ( ) ;
3636 return new Promise ( ( resolve , reject ) => {
3737 const tx = db . transaction ( IDB_STORE , 'readwrite' ) ;
@@ -44,7 +44,7 @@ async function idbPut(key, uint8) {
4444 } ) ;
4545}
4646
47- async function idbGet ( key ) {
47+ export async function idbGet ( key ) {
4848 const db = await idbOpen ( ) ;
4949 return new Promise ( ( resolve , reject ) => {
5050 const tx = db . transaction ( IDB_STORE , 'readonly' ) ;
@@ -112,36 +112,55 @@ export async function loadSQLite() {
112112 // Restore saved DB (if any) into worker FS
113113 try {
114114 const saved = await idbGet ( IDB_KEY ) ;
115- const DB_FILENAME = '/ todo.db' ;
115+ const DB_FILENAME = 'todo.db' ;
116116
117117 if ( saved && saved . byteLength > 0 ) {
118118 console . log ( 'Found saved DB in IndexedDB (' + saved . byteLength + ' bytes) — restoring into worker...' ) ;
119119 // We use our custom 'upload' message to populate the worker's MEMFS
120- await callWorker ( {
120+ const uploadResp = await callWorker ( {
121121 type : 'upload' ,
122122 args : { filename : DB_FILENAME , deserialize : saved }
123123 } ) ;
124- console . log ( 'DB bytes uploaded to worker VFS.' ) ;
124+ console . log ( 'DB bytes uploaded to worker VFS:' , uploadResp ) ;
125125 } else {
126126 console . log ( 'No prior DB found in IndexedDB — worker will start with an empty DB.' ) ;
127127 }
128128
129129 // send 'open' message to worker
130+ console . log ( 'Sending open message for:' , DB_FILENAME ) ;
130131 const resp = await callWorker ( { type : 'open' , args : { filename : DB_FILENAME } } ) ;
131132 if ( resp && resp . type === 'open' ) {
132- console . log ( 'DB opened in worker VFS.' ) ;
133+ console . log ( 'DB opened in worker VFS:' , resp ) ;
133134 } else {
134- console . warn ( 'Worker open response:' , resp ) ;
135+ console . warn ( 'Worker open response (unexpected type) :' , resp ) ;
135136 }
136137 } catch ( e ) {
137- console . error ( 'Error opening/restoring DB in worker:' , e ) ;
138+ console . error ( 'Error opening/restoring DB in worker. Full error object :' , JSON . stringify ( e , null , 2 ) ) ;
138139 }
139140
140141 // Replace global saveDatabase with one that asks the worker to export the DB and then persists to IndexedDB
141142 globalThis . saveDatabase = async ( ) => {
142143 try {
143- const DB_FILENAME = '/todo.db' ;
144- console . log ( 'Saving DB: requesting export from worker...' ) ;
144+ const DB_FILENAME = 'todo.db' ;
145+
146+ // Priority: Try to save from the main thread's .NET VFS first
147+ const runtime = globalThis . window ?. dotnetRuntime ;
148+ const dotnetFS = runtime && ( ( typeof runtime . getModule === 'function' ? runtime . getModule ( ) . FS : runtime . Module ?. FS ) || runtime . FS ) ;
149+ if ( dotnetFS ) {
150+ try {
151+ console . log ( 'Saving DB: reading from .NET main thread VFS...' ) ;
152+ const data = dotnetFS . readFile ( DB_FILENAME ) ;
153+ if ( data && data . byteLength > 0 ) {
154+ await idbPut ( IDB_KEY , data ) ;
155+ console . log ( 'DB persisted from .NET VFS to IndexedDB (' + data . byteLength + ' bytes).' ) ;
156+ return ;
157+ }
158+ } catch ( vfsErr ) {
159+ console . warn ( 'Failed to read DB from .NET VFS (might not be created yet):' , vfsErr ) ;
160+ }
161+ }
162+
163+ console . log ( 'Saving DB: fallback to requesting export from worker...' ) ;
145164 // Ask the worker to export the DB bytes back to us
146165 const resp = await callWorker ( { type : 'export' , args : { filename : DB_FILENAME } } ) ;
147166 // Expect the worker to respond with an object containing result.byteArray: Uint8Array
0 commit comments