@@ -3,6 +3,7 @@ import 'dart:isolate';
3
3
4
4
import 'package:logging/logging.dart' ;
5
5
import 'package:powersync/src/log_internal.dart' ;
6
+ import 'package:sqlite_async/mutex.dart' ;
6
7
import 'package:sqlite_async/sqlite3.dart' as sqlite;
7
8
import 'package:sqlite_async/sqlite_async.dart' ;
8
9
@@ -69,6 +70,9 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
69
70
/// null when disconnected, present when connecting or connected
70
71
AbortController ? _disconnecter;
71
72
73
+ /// Use to prevent multiple connections from being opened concurrently
74
+ final Mutex _connectMutex = Mutex ();
75
+
72
76
/// The Logger used by this [PowerSyncDatabase] .
73
77
///
74
78
/// The default is [autoLogger] , which logs to the console in debug builds.
@@ -190,6 +194,13 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
190
194
/// Throttle time between CRUD operations
191
195
/// Defaults to 10 milliseconds.
192
196
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 {
193
204
await initialize ();
194
205
195
206
// Disconnect if connected
0 commit comments