Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/sqlite_async/lib/src/web/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class WebDatabase
@override
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout}) {
{Duration? lockTimeout,
bool? flush}) {
return writeLock(
(writeContext) =>
internalWriteTransaction(writeContext, (context) async {
Expand All @@ -122,21 +123,25 @@ class WebDatabase
return callback(_ExclusiveTransactionContext(this, writeContext));
}),
debugContext: 'writeTransaction()',
lockTimeout: lockTimeout);
lockTimeout: lockTimeout,
flush: flush);
}

@override

/// Internal writeLock which intercepts transaction context's to verify auto commit is not active
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext}) async {
{Duration? lockTimeout, String? debugContext, bool? flush}) async {
if (_mutex case var mutex?) {
return await mutex.lock(() async {
final context = _ExclusiveContext(this);
try {
return await callback(context);
} finally {
context.markClosed();
if (flush != false) {
await this.flush();
}
}
});
} else {
Expand All @@ -148,11 +153,20 @@ class WebDatabase
return await callback(context);
} finally {
context.markClosed();
if (flush != false) {
await this.flush();
}
await _database.customRequest(
CustomDatabaseMessage(CustomDatabaseMessageKind.releaseLock));
}
}
}

@override
Future<void> flush() async {
await isInitialized;
return _database.fileSystem.flush();
}
}

class _SharedContext implements SqliteReadContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,32 @@ class SqliteDatabaseImpl

@override
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext}) async {
{Duration? lockTimeout, String? debugContext, bool? flush}) async {
await isInitialized;
return _runZoned(() {
return _connection.writeLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
lockTimeout: lockTimeout, debugContext: debugContext, flush: flush);
}, debugContext: debugContext ?? 'execute()');
}

@override
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout}) async {
{Duration? lockTimeout,
bool? flush}) async {
await isInitialized;
return _runZoned(
() => _connection.writeTransaction(callback, lockTimeout: lockTimeout),
() => _connection.writeTransaction(callback,
lockTimeout: lockTimeout, flush: flush),
debugContext: 'writeTransaction()');
}

@override
Future<void> flush() async {
await isInitialized;
return _connection.flush();
}

@override
Future<void> close() async {
await isInitialized;
Expand Down
30 changes: 30 additions & 0 deletions packages/sqlite_async/lib/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,34 @@ abstract class WebSqliteConnection implements SqliteConnection {
);
return database;
}

/// Same as [SqliteConnection.writeLock].
///
/// Has an additional [flush] (defaults to true). This can be set to false
/// to delay flushing changes to the database file, losing durability guarantees.
/// This only has an effect when IndexedDB storage is used.
///
/// See [flush] for details.
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext, bool? flush});

/// Same as [SqliteConnection.writeTransaction].
///
/// Has an additional [flush] (defaults to true). This can be set to false
/// to delay flushing changes to the database file, losing durability guarantees.
/// This only has an effect when IndexedDB storage is used.
///
/// See [flush] for details.
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout,
bool? flush});

/// Flush changes to the underlying storage.
///
/// When this returns, all changes previously written will be persisted
/// to storage.
///
/// This only has an effect when IndexedDB storage is used.
Future<void> flush();
}
Loading