@@ -13,34 +13,44 @@ export function openWorkerDatabasePort(
1313) {
1414 const needsDedicated = vfs == WASQLiteVFS . AccessHandlePoolVFS || vfs == WASQLiteVFS . OPFSCoopSyncVFS ;
1515
16+ const handleError = ( event : ErrorEvent ) => {
17+ // We don't expect worker errors, so turn errors on workers into unhandled errors in this context
18+ // to fail tests.
19+ throw `Unexpected worker error: ${ event . error } ` ;
20+ }
21+
22+ const openWorker = ( resolvedUri : string | URL , options : WorkerOptions = { } ) => {
23+ const useShared = ! needsDedicated && multipleTabs ;
24+ if ( useShared ) {
25+ const sharedWorker = new SharedWorker ( `${ worker } ` , {
26+ ...options ,
27+ /* @vite -ignore */
28+ name : `shared-DB-worker-${ workerIdentifier } `
29+ } ) ;
30+ sharedWorker . onerror = handleError ;
31+ return sharedWorker . port ;
32+ } else {
33+ const dedicatedWorker = new Worker ( `${ worker } ` , {
34+ ...options ,
35+ /* @vite -ignore */
36+ name : `DB-worker-${ workerIdentifier } `
37+ } ) ;
38+ dedicatedWorker . onerror = handleError ;
39+
40+ return dedicatedWorker ;
41+ }
42+ } ;
43+
1644 if ( worker ) {
17- return ! needsDedicated && multipleTabs
18- ? new SharedWorker ( `${ worker } ` , {
19- /* @vite -ignore */
20- name : `shared-DB-worker-${ workerIdentifier } `
21- } ) . port
22- : new Worker ( `${ worker } ` , {
23- /* @vite -ignore */
24- name : `DB-worker-${ workerIdentifier } `
25- } ) ;
45+ return openWorker ( worker ) ;
2646 } else {
2747 /**
2848 * Webpack V5 can bundle the worker automatically if the full Worker constructor syntax is used
2949 * https://webpack.js.org/guides/web-workers/
3050 * This enables multi tab support by default, but falls back if SharedWorker is not available
3151 * (in the case of Android)
3252 */
33- return ! needsDedicated && multipleTabs
34- ? new SharedWorker ( new URL ( './WASQLiteDB.worker.js' , import . meta. url ) , {
35- /* @vite -ignore */
36- name : `shared-DB-worker-${ workerIdentifier } ` ,
37- type : 'module'
38- } ) . port
39- : new Worker ( new URL ( './WASQLiteDB.worker.js' , import . meta. url ) , {
40- /* @vite -ignore */
41- name : `DB-worker-${ workerIdentifier } ` ,
42- type : 'module'
43- } ) ;
53+ return openWorker ( new URL ( './WASQLiteDB.worker.js' , import . meta. url ) , { type : 'module' } ) ;
4454 }
4555}
4656
0 commit comments