Skip to content

Commit 8e3edb8

Browse files
committed
Skip second mutex for OPFS implementations
1 parent 80cd039 commit 8e3edb8

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/sqlite_async/lib/src/web/web_sqlite_open_factory.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,19 @@ class DefaultSqliteOpenFactory
6363
final workers = await _initialized;
6464
final connection = await connectToWorker(workers, path);
6565

66-
// When the database is accessed through a shared worker, we implement
67-
// mutexes over custom messages sent through the shared worker. In other
68-
// cases, we need to implement a mutex locally.
69-
final mutex = connection.access == AccessMode.throughSharedWorker
66+
// When the database is hosted in a shared worker, we don't need a local
67+
// mutex since that worker will hand out leases for us.
68+
// Additionally, package:sqlite3_web uses navigator locks internally for
69+
// OPFS databases.
70+
// Technically, the only other implementation (IndexedDB in a local context
71+
// or a dedicated worker) is inherently unsafe to use across tabs. But
72+
// wrapping those in a mutex and flushing the file system helps a little bit
73+
// (still something we're trying to avoid).
74+
final hasSqliteWebMutex =
75+
connection.access == AccessMode.throughSharedWorker ||
76+
connection.storage == StorageMode.opfs;
77+
78+
final mutex = hasSqliteWebMutex
7079
? null
7180
: MutexImpl(identifier: path); // Use the DB path as a mutex identifier
7281

0 commit comments

Comments
 (0)