11import {
2- BaseObserver ,
3- DBAdapter ,
4- DBAdapterListener ,
5- DBGetUtils ,
6- DBLockOptions ,
7- LockContext ,
8- PowerSyncOpenFactoryOptions ,
9- QueryResult ,
10- Transaction
2+ type DBAdapter ,
3+ type DBAdapterListener ,
4+ type DBGetUtils ,
5+ type DBLockOptions ,
6+ type LockContext ,
7+ type PowerSyncOpenFactoryOptions ,
8+ type QueryResult ,
9+ type Transaction ,
10+ BaseObserver
1111} from '@powersync/common' ;
1212import * as Comlink from 'comlink' ;
13- import Logger , { ILogger } from 'js-logger' ;
14- import type { DBWorkerInterface , OpenDB } from '../../../worker/db/open-db' ;
13+ import Logger , { type ILogger } from 'js-logger' ;
14+ import type { DBFunctionsInterface , OpenDB } from '../../../shared/types' ;
15+ import { _openDB } from '../../../shared/open-db' ;
1516import { getWorkerDatabaseOpener } from '../../../worker/db/open-worker-database' ;
1617
1718export type WASQLiteFlags = {
1819 enableMultiTabs ?: boolean ;
20+ useWebWorker ?: boolean ;
1921} ;
2022
2123export interface WASQLiteDBAdapterOptions extends Omit < PowerSyncOpenFactoryOptions , 'schema' > {
@@ -34,13 +36,13 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
3436 private initialized : Promise < void > ;
3537 private logger : ILogger ;
3638 private dbGetHelpers : DBGetUtils | null ;
37- private workerMethods : DBWorkerInterface | null ;
39+ private methods : DBFunctionsInterface | null ;
3840
3941 constructor ( protected options : WASQLiteDBAdapterOptions ) {
4042 super ( ) ;
4143 this . logger = Logger . get ( 'WASQLite' ) ;
4244 this . dbGetHelpers = null ;
43- this . workerMethods = null ;
45+ this . methods = null ;
4446 this . initialized = this . init ( ) ;
4547 this . dbGetHelpers = this . generateDBHelpers ( { execute : this . _execute . bind ( this ) } ) ;
4648 }
@@ -56,22 +58,31 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
5658 getWorker ( ) { }
5759
5860 protected async init ( ) {
59- const { enableMultiTabs } = this . flags ;
61+ const { enableMultiTabs, useWebWorker } = this . flags ;
6062 if ( ! enableMultiTabs ) {
6163 this . logger . warn ( 'Multiple tabs are not enabled in this browser' ) ;
6264 }
6365
64- const dbOpener = this . options . workerPort
65- ? Comlink . wrap < OpenDB > ( this . options . workerPort )
66- : getWorkerDatabaseOpener ( this . options . dbFilename , enableMultiTabs ) ;
66+ if ( useWebWorker ) {
67+ const dbOpener = this . options . workerPort
68+ ? Comlink . wrap < OpenDB > ( this . options . workerPort )
69+ : getWorkerDatabaseOpener ( this . options . dbFilename , enableMultiTabs ) ;
6770
68- this . workerMethods = await dbOpener ( this . options . dbFilename ) ;
71+ this . methods = await dbOpener ( this . options . dbFilename ) ;
6972
70- this . workerMethods . registerOnTableChange (
71- Comlink . proxy ( ( opType : number , tableName : string , rowId : number ) => {
72- this . iterateListeners ( ( cb ) => cb . tablesUpdated ?.( { opType, table : tableName , rowId } ) ) ;
73- } )
74- ) ;
73+ this . methods ! . registerOnTableChange (
74+ Comlink . proxy ( ( opType : number , tableName : string , rowId : number ) => {
75+ this . iterateListeners ( ( cb ) => cb . tablesUpdated ?.( { opType, table : tableName , rowId } ) ) ;
76+ } )
77+ ) ;
78+
79+ return ;
80+ }
81+ this . methods = await _openDB ( this . options . dbFilename , { useWebWorker : false } ) ;
82+
83+ this . methods . registerOnTableChange ( ( opType : number , tableName : string , rowId : number ) => {
84+ this . iterateListeners ( ( cb ) => cb . tablesUpdated ?.( { opType, table : tableName , rowId } ) ) ;
85+ } ) ;
7586 }
7687
7788 async execute ( query : string , params ?: any [ ] | undefined ) : Promise < QueryResult > {
@@ -87,7 +98,7 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
8798 */
8899 private _execute = async ( sql : string , bindings ?: any [ ] ) : Promise < QueryResult > => {
89100 await this . initialized ;
90- const result = await this . workerMethods ! . execute ! ( sql , bindings ) ;
101+ const result = await this . methods ! . execute ! ( sql , bindings ) ;
91102 return {
92103 ...result ,
93104 rows : {
@@ -102,7 +113,7 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
102113 */
103114 private _executeBatch = async ( query : string , params ?: any [ ] ) : Promise < QueryResult > => {
104115 await this . initialized ;
105- const result = await this . workerMethods ! . executeBatch ! ( query , params ) ;
116+ const result = await this . methods ! . executeBatch ! ( query , params ) ;
106117 return {
107118 ...result ,
108119 rows : undefined
@@ -115,7 +126,7 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
115126 * tabs are still using it.
116127 */
117128 close ( ) {
118- this . workerMethods ?. close ?.( ) ;
129+ this . methods ?. close ?.( ) ;
119130 }
120131
121132 async getAll < T > ( sql : string , parameters ?: any [ ] | undefined ) : Promise < T [ ] > {
0 commit comments