@@ -3,6 +3,7 @@ import 'dart:isolate';
33
44import 'package:logging/logging.dart' ;
55import 'package:powersync/src/log_internal.dart' ;
6+ import 'package:sqlite_async/mutex.dart' ;
67import 'package:sqlite_async/sqlite3.dart' as sqlite;
78import 'package:sqlite_async/sqlite_async.dart' ;
89
@@ -69,6 +70,9 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
6970 /// null when disconnected, present when connecting or connected
7071 AbortController ? _disconnecter;
7172
73+ /// Use to prevent multiple connections from being opened concurrently
74+ final Mutex _connectMutex = Mutex ();
75+
7276 /// The Logger used by this [PowerSyncDatabase] .
7377 ///
7478 /// The default is [autoLogger] , which logs to the console in debug builds.
@@ -190,6 +194,13 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
190194 /// Throttle time between CRUD operations
191195 /// Defaults to 10 milliseconds.
192196 Duration crudThrottleTime = const Duration (milliseconds: 10 )}) async {
197+ _connectMutex.lock (() =>
198+ _connect (connector: connector, crudThrottleTime: crudThrottleTime));
199+ }
200+
201+ Future <void > _connect (
202+ {required PowerSyncBackendConnector connector,
203+ required Duration crudThrottleTime}) async {
193204 await initialize ();
194205
195206 // Disconnect if connected
0 commit comments