Skip to content

Commit bb566d3

Browse files
committed
Remove duplicate mutex implementation
1 parent 331fc7b commit bb566d3

File tree

3 files changed

+7
-31
lines changed

3 files changed

+7
-31
lines changed

sqlite3_web/lib/src/client.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:stream_channel/stream_channel.dart';
88
import 'package:web/web.dart'
99
hide Response, Request, FileSystem, Notification, Lock;
1010

11+
import 'locks.dart';
1112
import 'types.dart';
1213
import 'channel.dart';
1314
import 'database.dart';
@@ -329,7 +330,7 @@ final class DatabaseClient implements WebSqlite {
329330
final DatabaseController _localController;
330331
final Future<JSAny?> Function(JSAny?) _handleCustomRequest;
331332

332-
final Lock _startWorkersLock = Lock();
333+
final Mutex _startWorkers = Mutex();
333334
bool _startedWorkers = false;
334335
WorkerConnection? _connectionToDedicated;
335336
WorkerConnection? _connectionToShared;
@@ -347,7 +348,7 @@ final class DatabaseClient implements WebSqlite {
347348
});
348349

349350
Future<void> startWorkers() {
350-
return _startWorkersLock.synchronized(() async {
351+
return _startWorkers.withCriticalSection(() async {
351352
if (_startedWorkers) {
352353
return;
353354
}
@@ -406,7 +407,7 @@ final class DatabaseClient implements WebSqlite {
406407
}
407408

408409
Future<WorkerConnection> _connectToDedicatedInShared() {
409-
return _startWorkersLock.synchronized(() async {
410+
return _startWorkers.withCriticalSection(() async {
410411
if (_connectionToDedicatedInShared case final conn?) {
411412
return conn;
412413
}
@@ -421,7 +422,7 @@ final class DatabaseClient implements WebSqlite {
421422
}
422423

423424
Future<WorkerConnection> _connectToLocal() async {
424-
return _startWorkersLock.synchronized(() async {
425+
return _startWorkers.withCriticalSection(() async {
425426
if (_connectionToLocal case final conn?) {
426427
return conn;
427428
}

sqlite3_web/lib/src/shared.dart

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,6 @@ Future<void> deleteDatabaseInIndexedDb(String databaseName) async {
6464
await idb.deleteDatabase(databaseName).complete<JSAny?>();
6565
}
6666

67-
/// A single asynchronous lock implemented by future-chaining.
68-
class Lock {
69-
Future<void>? _last;
70-
71-
/// Waits for previous [synchronized]-calls on this [Lock] to complete, and
72-
/// then calls [block] before further [synchronized] calls are allowed.
73-
Future<T> synchronized<T>(FutureOr<T> Function() block) {
74-
final previous = _last;
75-
// This completer may not be sync: It must complete just after
76-
// callBlockAndComplete completes.
77-
final blockCompleted = Completer<void>();
78-
_last = blockCompleted.future;
79-
80-
Future<T> callBlockAndComplete() {
81-
return Future.sync(block).whenComplete(blockCompleted.complete);
82-
}
83-
84-
if (previous != null) {
85-
return previous.then((_) => callBlockAndComplete());
86-
} else {
87-
return callBlockAndComplete();
88-
}
89-
}
90-
}
91-
9267
/// Collects all drift OPFS databases.
9368
Future<List<String>> opfsDatabases() async {
9469
final storage = storageManager;

sqlite3_web/lib/src/worker.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ final class WorkerRunner {
657657
Future<WasmSqlite3>? _sqlite3;
658658
Uri? _wasmUri;
659659

660-
final Lock _compatibilityCheckLock = Lock();
660+
final Mutex _compatibilityCheck = Mutex();
661661
CompatibilityResult? _compatibilityResult;
662662

663663
/// For shared workers, a dedicated inner worker allowing tabs to connect to
@@ -696,7 +696,7 @@ final class WorkerRunner {
696696
}
697697

698698
Future<CompatibilityResult> checkCompatibility(CompatibilityCheck check) {
699-
return _compatibilityCheckLock.synchronized(() async {
699+
return _compatibilityCheck.withCriticalSection(() async {
700700
if (_compatibilityResult != null) {
701701
// todo: We may have to update information about existing databases
702702
// as they come and go

0 commit comments

Comments
 (0)