@@ -9,7 +9,8 @@ import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnecto
99import {
1010 AbstractStreamingSyncImplementation ,
1111 DEFAULT_CRUD_UPLOAD_THROTTLE_MS ,
12- StreamingSyncImplementationListener
12+ StreamingSyncImplementationListener ,
13+ StreamingSyncImplementation
1314} from './sync/stream/AbstractStreamingSyncImplementation' ;
1415import { CrudBatch } from './sync/bucket/CrudBatch' ;
1516import { CrudTransaction } from './sync/bucket/CrudTransaction' ;
@@ -63,12 +64,25 @@ export interface PowerSyncDBListener extends StreamingSyncImplementationListener
6364 initialized : ( ) => void ;
6465}
6566
67+ export interface PowerSyncCloseOptions {
68+ /**
69+ * Disconnect the sync stream client if connected.
70+ * This is usually true, but can be false for Web when using
71+ * multiple tabs and a shared sync provider.
72+ */
73+ disconnect ?: boolean ;
74+ }
75+
6676const POWERSYNC_TABLE_MATCH = / ( ^ p s _ d a t a _ _ | ^ p s _ d a t a _ l o c a l _ _ ) / ;
6777
6878const DEFAULT_DISCONNECT_CLEAR_OPTIONS : DisconnectAndClearOptions = {
6979 clearLocal : true
7080} ;
7181
82+ export const DEFAULT_POWERSYNC_CLOSE_OPTIONS : PowerSyncCloseOptions = {
83+ disconnect : true
84+ } ;
85+
7286export const DEFAULT_WATCH_THROTTLE_MS = 30 ;
7387
7488export const DEFAULT_POWERSYNC_DB_OPTIONS = {
@@ -101,10 +115,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
101115 * Current connection status.
102116 */
103117 currentStatus ?: SyncStatus ;
104- syncStreamImplementation ?: AbstractStreamingSyncImplementation ;
118+ syncStreamImplementation ?: StreamingSyncImplementation ;
105119 sdkVersion : string ;
106120
107- private abortController : AbortController | null ;
108121 protected bucketStorageAdapter : BucketStorageAdapter ;
109122 private syncStatusListenerDisposer ?: ( ) => void ;
110123 protected _isReadyPromise : Promise < void > ;
@@ -113,7 +126,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
113126 constructor ( protected options : PowerSyncDatabaseOptions ) {
114127 super ( ) ;
115128 this . bucketStorageAdapter = this . generateBucketStorageAdapter ( ) ;
116- this . closed = true ;
129+ this . closed = false ;
117130 this . currentStatus = undefined ;
118131 this . options = { ...DEFAULT_POWERSYNC_DB_OPTIONS , ...options } ;
119132 this . _schema = options . schema ;
@@ -189,7 +202,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
189202 * Cannot be used while connected - this should only be called before {@link AbstractPowerSyncDatabase.connect}.
190203 */
191204 async updateSchema ( schema : Schema ) {
192- if ( this . abortController ) {
205+ if ( this . syncStreamImplementation ) {
193206 throw new Error ( 'Cannot update schema while connected' ) ;
194207 }
195208
@@ -207,19 +220,6 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
207220 await this . database . execute ( 'SELECT powersync_replace_schema(?)' , [ JSON . stringify ( this . schema . toJSON ( ) ) ] ) ;
208221 }
209222
210- /**
211- * Queues a CRUD upload when internal CRUD tables have been updated.
212- */
213- protected async watchCrudUploads ( ) {
214- for await ( const event of this . onChange ( {
215- tables : [ PSInternalTable . CRUD ] ,
216- rawTableNames : true ,
217- signal : this . abortController ?. signal
218- } ) ) {
219- this . syncStreamImplementation ?. triggerCrudUpload ( ) ;
220- }
221- }
222-
223223 /**
224224 * Wait for initialization to complete.
225225 * While initializing is automatic, this helps to catch and report initialization errors.
@@ -232,10 +232,15 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
232232 * Connects to stream of events from the PowerSync instance.
233233 */
234234 async connect ( connector : PowerSyncBackendConnector ) {
235+ await this . waitForReady ( ) ;
236+
235237 // close connection if one is open
236238 await this . disconnect ( ) ;
237239
238- await this . waitForReady ( ) ;
240+ if ( this . closed ) {
241+ throw new Error ( 'Cannot connect using a closed client' ) ;
242+ }
243+
239244 this . syncStreamImplementation = this . generateSyncStreamImplementation ( connector ) ;
240245 this . syncStatusListenerDisposer = this . syncStreamImplementation . registerListener ( {
241246 statusChanged : ( status ) => {
@@ -244,11 +249,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
244249 }
245250 } ) ;
246251
247- this . abortController = new AbortController ( ) ;
248- // Begin network stream
252+ await this . syncStreamImplementation . waitForReady ( ) ;
249253 this . syncStreamImplementation . triggerCrudUpload ( ) ;
250- this . syncStreamImplementation . streamingSync ( this . abortController . signal ) ;
251- this . watchCrudUploads ( ) ;
254+ this . syncStreamImplementation . connect ( ) ;
252255 }
253256
254257 /**
@@ -257,9 +260,10 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
257260 * Use {@link connect} to connect again.
258261 */
259262 async disconnect ( ) {
260- this . abortController ?. abort ( ) ;
263+ await this . syncStreamImplementation ?. disconnect ( ) ;
261264 this . syncStatusListenerDisposer ?.( ) ;
262- this . abortController = null ;
265+ await this . syncStreamImplementation ?. dispose ( ) ;
266+ this . syncStreamImplementation = undefined ;
263267 }
264268
265269 /**
@@ -308,11 +312,17 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
308312 * Once close is called, this connection cannot be used again - a new one
309313 * must be constructed.
310314 */
311- async close ( ) {
315+ async close ( options : PowerSyncCloseOptions = DEFAULT_POWERSYNC_CLOSE_OPTIONS ) {
312316 await this . waitForReady ( ) ;
313317
314- await this . disconnect ( ) ;
318+ const { disconnect } = options ;
319+ if ( disconnect ) {
320+ await this . disconnect ( ) ;
321+ }
322+
323+ await this . syncStreamImplementation ?. dispose ( ) ;
315324 this . database . close ( ) ;
325+ this . closed = true ;
316326 }
317327
318328 /**
0 commit comments