Skip to content

Commit c06e4ec

Browse files
committed
Make flush optional.
1 parent ed2c3c0 commit c06e4ec

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

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

+17-5
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class WebDatabase
113113
@override
114114
Future<T> writeTransaction<T>(
115115
Future<T> Function(SqliteWriteContext tx) callback,
116-
{Duration? lockTimeout}) {
116+
{Duration? lockTimeout,
117+
bool? flush}) {
117118
return writeLock(
118119
(writeContext) =>
119120
internalWriteTransaction(writeContext, (context) async {
@@ -122,22 +123,25 @@ class WebDatabase
122123
return callback(_ExclusiveTransactionContext(this, writeContext));
123124
}),
124125
debugContext: 'writeTransaction()',
125-
lockTimeout: lockTimeout);
126+
lockTimeout: lockTimeout,
127+
flush: flush);
126128
}
127129

128130
@override
129131

130132
/// Internal writeLock which intercepts transaction context's to verify auto commit is not active
131133
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
132-
{Duration? lockTimeout, String? debugContext}) async {
134+
{Duration? lockTimeout, String? debugContext, bool? flush}) async {
133135
if (_mutex case var mutex?) {
134136
return await mutex.lock(() async {
135137
final context = _ExclusiveContext(this);
136138
try {
137139
return await callback(context);
138140
} finally {
139141
context.markClosed();
140-
await _database.fileSystem.flush();
142+
if (flush != false) {
143+
await this.flush();
144+
}
141145
}
142146
});
143147
} else {
@@ -149,12 +153,20 @@ class WebDatabase
149153
return await callback(context);
150154
} finally {
151155
context.markClosed();
152-
await _database.fileSystem.flush();
156+
if (flush != false) {
157+
await this.flush();
158+
}
153159
await _database.customRequest(
154160
CustomDatabaseMessage(CustomDatabaseMessageKind.releaseLock));
155161
}
156162
}
157163
}
164+
165+
@override
166+
Future<void> flush() async {
167+
await isInitialized;
168+
return _database.fileSystem.flush();
169+
}
158170
}
159171

160172
class _SharedContext implements SqliteReadContext {

packages/sqlite_async/lib/src/web/database/web_sqlite_database.dart

+12-4
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,32 @@ class SqliteDatabaseImpl
131131

132132
@override
133133
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
134-
{Duration? lockTimeout, String? debugContext}) async {
134+
{Duration? lockTimeout, String? debugContext, bool? flush}) async {
135135
await isInitialized;
136136
return _runZoned(() {
137137
return _connection.writeLock(callback,
138-
lockTimeout: lockTimeout, debugContext: debugContext);
138+
lockTimeout: lockTimeout, debugContext: debugContext, flush: flush);
139139
}, debugContext: debugContext ?? 'execute()');
140140
}
141141

142142
@override
143143
Future<T> writeTransaction<T>(
144144
Future<T> Function(SqliteWriteContext tx) callback,
145-
{Duration? lockTimeout}) async {
145+
{Duration? lockTimeout,
146+
bool? flush}) async {
146147
await isInitialized;
147148
return _runZoned(
148-
() => _connection.writeTransaction(callback, lockTimeout: lockTimeout),
149+
() => _connection.writeTransaction(callback,
150+
lockTimeout: lockTimeout, flush: flush),
149151
debugContext: 'writeTransaction()');
150152
}
151153

154+
@override
155+
Future<void> flush() async {
156+
await isInitialized;
157+
return _connection.flush();
158+
}
159+
152160
@override
153161
Future<void> close() async {
154162
await isInitialized;

packages/sqlite_async/lib/web.dart

+30
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,34 @@ abstract class WebSqliteConnection implements SqliteConnection {
6565
);
6666
return database;
6767
}
68+
69+
/// Same as [SqliteConnection.writeLock].
70+
///
71+
/// Has an additional [flush] (defaults to true). This can be set to false
72+
/// to delay flushing changes to the database file, losing durability guarantees.
73+
/// This only has an effect when IndexedDB storage is used.
74+
///
75+
/// See [flush] for details.
76+
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
77+
{Duration? lockTimeout, String? debugContext, bool? flush});
78+
79+
/// Same as [SqliteConnection.writeTransaction].
80+
///
81+
/// Has an additional [flush] (defaults to true). This can be set to false
82+
/// to delay flushing changes to the database file, losing durability guarantees.
83+
/// This only has an effect when IndexedDB storage is used.
84+
///
85+
/// See [flush] for details.
86+
Future<T> writeTransaction<T>(
87+
Future<T> Function(SqliteWriteContext tx) callback,
88+
{Duration? lockTimeout,
89+
bool? flush});
90+
91+
/// Flush changes to the underlying storage.
92+
///
93+
/// When this returns, all changes previously written will be persisted
94+
/// to storage.
95+
///
96+
/// This only has an effect when IndexedDB storage is used.
97+
Future<void> flush();
6898
}

0 commit comments

Comments
 (0)