@@ -118,6 +118,7 @@ class PowerSyncDatabaseImpl
118
118
}
119
119
120
120
@override
121
+ @internal
121
122
122
123
/// Connect to the PowerSync service, and keep the databases in sync.
123
124
///
@@ -134,10 +135,11 @@ class PowerSyncDatabaseImpl
134
135
135
136
// Disconnect if connected
136
137
await disconnect ();
137
- disconnecter = AbortController ();
138
+ final disconnector = AbortController ();
139
+ disconnecter = disconnector;
138
140
139
141
await isInitialized;
140
- final dbref = database.isolateConnectionFactory ();
142
+ final dbRef = database.isolateConnectionFactory ();
141
143
ReceivePort rPort = ReceivePort ();
142
144
StreamSubscription ? updateSubscription;
143
145
rPort.listen ((data) async {
@@ -161,7 +163,7 @@ class PowerSyncDatabaseImpl
161
163
updateSubscription = throttled.listen ((event) {
162
164
port.send (['update' ]);
163
165
});
164
- disconnecter ? .onAbort.then ((_) {
166
+ disconnector .onAbort.then ((_) {
165
167
port.send (['close' ]);
166
168
}).ignore ();
167
169
} else if (action == 'uploadCrud' ) {
@@ -197,11 +199,11 @@ class PowerSyncDatabaseImpl
197
199
logger.severe ('Sync Isolate error' , message);
198
200
199
201
// Reconnect
200
- baseConnect (connector: connector);
202
+ connect (connector: connector, crudThrottleTime : crudThrottleTime );
201
203
});
202
204
203
205
disconnected () {
204
- disconnecter ? .completeAbort ();
206
+ disconnector .completeAbort ();
205
207
disconnecter = null ;
206
208
rPort.close ();
207
209
// Clear status apart from lastSyncedAt
@@ -220,7 +222,7 @@ class PowerSyncDatabaseImpl
220
222
}
221
223
222
224
Isolate .spawn (_powerSyncDatabaseIsolate,
223
- _PowerSyncDatabaseIsolateArgs (rPort.sendPort, dbref , retryDelay),
225
+ _PowerSyncDatabaseIsolateArgs (rPort.sendPort, dbRef , retryDelay),
224
226
debugName: 'PowerSyncDatabase' ,
225
227
onError: errorPort.sendPort,
226
228
onExit: exitPort.sendPort);
@@ -274,12 +276,17 @@ Future<void> _powerSyncDatabaseIsolate(
274
276
final upstreamDbClient = args.dbRef.upstreamPort.open ();
275
277
276
278
CommonDatabase ? db;
277
- rPort.listen ((message) {
279
+ final Mutex mutex = args.dbRef.mutex.open ();
280
+ rPort.listen ((message) async {
278
281
if (message is List ) {
279
282
String action = message[0 ];
280
283
if (action == 'update' ) {
281
284
updateController.add ('update' );
282
285
} else if (action == 'close' ) {
286
+ // The SyncSqliteConnection uses this mutex
287
+ // It needs to be closed before killing the isolate
288
+ // in order to free the mutex for other operations.
289
+ await mutex.close ();
283
290
db? .dispose ();
284
291
updateController.close ();
285
292
upstreamDbClient.close ();
@@ -318,7 +325,6 @@ Future<void> _powerSyncDatabaseIsolate(
318
325
}
319
326
320
327
runZonedGuarded (() async {
321
- final mutex = args.dbRef.mutex.open ();
322
328
db = await args.dbRef.openFactory
323
329
.open (SqliteOpenOptions (primaryConnection: false , readOnly: false ));
324
330
final connection = SyncSqliteConnection (db! , mutex);
0 commit comments