@@ -100,7 +100,8 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
100100 // We only need to trigger a disconnect here if we have already reached the point of connecting.
101101 // If we do already have pending options, a disconnect has already been performed.
102102 // The connectInternal method also does a sanity disconnect to prevent straggler connections.
103- if ( ! hadPendingOptions ) {
103+ // We should also disconnect if we have already completed a connection attempt.
104+ if ( ! hadPendingOptions || ! this . connectingPromise ) {
104105 await this . disconnectInternal ( ) ;
105106 }
106107
@@ -136,46 +137,47 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
136137 * This is protected in an exclusive lock.
137138 * The promise tracks the creation which is used to synchronize disconnect attempts.
138139 */
139- this . syncStreamInitPromise = ( async ( ) => {
140- // Always await this if present since we will be populating a new sync implementation shortly
141- await this . disconnectingPromise ;
142-
143- if ( ! this . pendingConnectionOptions ) {
144- // A disconnect could have cleared this.
145- return ;
140+ this . syncStreamInitPromise = new Promise ( async ( resolve , reject ) => {
141+ try {
142+ await this . disconnectingPromise ;
143+
144+ if ( ! this . pendingConnectionOptions ) {
145+ this . logger . debug ( 'No pending connection options found, not creating sync stream implementation' ) ;
146+ // A disconnect could have cleared this.
147+ return ;
148+ }
149+
150+ const { connector, options } = this . pendingConnectionOptions ;
151+ appliedOptions = options ;
152+
153+ this . pendingConnectionOptions = null ;
154+
155+ const { sync, onDispose } = await this . options . createSyncImplementation ( connector , options ) ;
156+ this . iterateListeners ( ( l ) => l . syncStreamCreated ?.( sync ) ) ;
157+ this . syncStreamImplementation = sync ;
158+ this . syncDisposer = onDispose ;
159+ await this . syncStreamImplementation . waitForReady ( ) ;
160+ resolve ( ) ;
161+ } catch ( error ) {
162+ reject ( error ) ;
146163 }
147-
148- const { connector, options } = this . pendingConnectionOptions ;
149- appliedOptions = options ;
150-
151- this . pendingConnectionOptions = null ;
152-
153- const { sync, onDispose } = await this . options . createSyncImplementation ( connector , options ) ;
154- this . iterateListeners ( ( l ) => l . syncStreamCreated ?.( sync ) ) ;
155- this . syncStreamImplementation = sync ;
156- this . syncDisposer = onDispose ;
157- await this . syncStreamImplementation . waitForReady ( ) ;
158- } ) ( ) ;
164+ } ) ;
159165
160166 await this . syncStreamInitPromise ;
161167 this . syncStreamInitPromise = null ;
162168
163169 if ( ! appliedOptions ) {
164- this . logger . debug ( 'No pending connection options found, not connecting' ) ;
165170 // A disconnect could have cleared the options which did not create a syncStreamImplementation
166171 return ;
167172 }
168173
169174 // It might be possible that a disconnect triggered between the last check
170175 // and this point. Awaiting here allows the sync stream to be cleared if disconnected.
171- this . logger . debug ( 'Waiting for disconnect to complete before connecting' , this . disconnectingPromise ) ;
172176 await this . disconnectingPromise ;
173- this . logger . debug ( 'Disconnect completed, proceeding to connect' ) ;
174177
175178 this . syncStreamImplementation ?. triggerCrudUpload ( ) ;
176179 this . logger . debug ( 'Attempting to connect to PowerSync instance' ) ;
177180 await this . syncStreamImplementation ?. connect ( appliedOptions ! ) ;
178- this . logger . debug ( 'connected to sync stream implementation' ) ;
179181 }
180182
181183 /**
0 commit comments