@@ -7,6 +7,7 @@ import 'package:powersync_core/sqlite_async.dart';
7
7
import 'package:powersync_core/src/abort_controller.dart' ;
8
8
import 'package:powersync_core/src/connector.dart' ;
9
9
import 'package:powersync_core/src/crud.dart' ;
10
+ import 'package:powersync_core/src/database/active_instances.dart' ;
10
11
import 'package:powersync_core/src/database/core_version.dart' ;
11
12
import 'package:powersync_core/src/powersync_update_notification.dart' ;
12
13
import 'package:powersync_core/src/schema.dart' ;
@@ -45,8 +46,7 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
45
46
StreamController <SyncStatus > statusStreamController =
46
47
StreamController <SyncStatus >.broadcast ();
47
48
48
- /// Use to prevent multiple connections from being opened concurrently
49
- final Mutex _connectMutex = Mutex ();
49
+ late final ActiveDatabaseGroup _activeGroup;
50
50
51
51
@override
52
52
@@ -71,6 +71,22 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
71
71
72
72
@protected
73
73
Future <void > baseInit () async {
74
+ String identifier = 'memory' ;
75
+ try {
76
+ identifier = database.openFactory.path;
77
+ } catch (ignore) {
78
+ // The in-memory database used in some tests doesn't have an open factory.
79
+ }
80
+
81
+ _activeGroup = ActiveDatabaseGroup .referenceDatabase (identifier);
82
+ if (_activeGroup.refCount > 1 ) {
83
+ logger.warning (
84
+ 'Multiple instances for the same database have been detected. '
85
+ 'This can cause unexpected results, please check your PowerSync client '
86
+ 'instantiation logic if this is not intentional' ,
87
+ );
88
+ }
89
+
74
90
statusStream = statusStreamController.stream;
75
91
updates = powerSyncUpdateNotifications (database.updates);
76
92
@@ -209,12 +225,16 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
209
225
await isInitialized;
210
226
// Disconnect any active sync connection.
211
227
await disconnect ();
212
- // Now we can close the database
213
- await database.close ();
214
228
215
- // If there are paused subscriptionso n the status stream, don't delay
216
- // closing the database because of that.
217
- unawaited (statusStreamController.close ());
229
+ if (! database.closed) {
230
+ // Now we can close the database
231
+ await database.close ();
232
+
233
+ // If there are paused subscriptionso n the status stream, don't delay
234
+ // closing the database because of that.
235
+ unawaited (statusStreamController.close ());
236
+ _activeGroup.close ();
237
+ }
218
238
}
219
239
220
240
/// Connect to the PowerSync service, and keep the databases in sync.
@@ -233,7 +253,7 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
233
253
Zone current = Zone .current;
234
254
235
255
Future <void > reconnect () {
236
- return _connectMutex .lock (() => baseConnect (
256
+ return _activeGroup.syncConnectMutex .lock (() => baseConnect (
237
257
connector: connector,
238
258
crudThrottleTime: crudThrottleTime,
239
259
// The reconnect function needs to run in the original zone,
0 commit comments