Skip to content

Commit 1e64f5d

Browse files
authored
Merge pull request #77 from powersync-ja/web-flush
[Web] Flush at the end of each writeLock
2 parents 3cdd5dd + ce0750e commit 1e64f5d

File tree

8 files changed

+98
-10
lines changed

8 files changed

+98
-10
lines changed

CHANGELOG.md

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,34 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## 2024-11-06
7+
8+
### Changes
9+
10+
---
11+
12+
Packages with breaking changes:
13+
14+
- There are no breaking changes in this release.
15+
16+
Packages with other changes:
17+
18+
- [`sqlite_async` - `v0.11.0`](#sqlite_async---v0110)
19+
- [`drift_sqlite_async` - `v0.2.0-alpha.4`](#drift_sqlite_async---v020-alpha4)
20+
21+
Packages with dependency updates only:
22+
23+
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
24+
25+
- `drift_sqlite_async` - `v0.2.0-alpha.4`
26+
27+
---
28+
29+
#### `sqlite_async` - `v0.11.0`
30+
31+
- Automatically flush IndexedDB storage to fix durability issues
32+
33+
634
## 2024-11-01
735

836
### Changes

packages/drift_sqlite_async/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.0-alpha.4
2+
3+
- Update a dependency to the latest release.
4+
15
## 0.2.0-alpha.3
26

37
- Bump `sqlite_async` to v0.10.1

packages/drift_sqlite_async/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: drift_sqlite_async
2-
version: 0.2.0-alpha.3
2+
version: 0.2.0-alpha.4
33
homepage: https://github.com/powersync-ja/sqlite_async.dart
44
repository: https://github.com/powersync-ja/sqlite_async.dart
55
description: Use Drift with a sqlite_async database, allowing both to be used in the same application.
@@ -15,7 +15,7 @@ environment:
1515
sdk: ">=3.0.0 <4.0.0"
1616
dependencies:
1717
drift: ">=2.19.0 <3.0.0"
18-
sqlite_async: ^0.10.1
18+
sqlite_async: ^0.11.0
1919
dev_dependencies:
2020
build_runner: ^2.4.8
2121
drift_dev: ">=2.19.0 <3.0.0"

packages/sqlite_async/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.11.0
2+
3+
- Automatically flush IndexedDB storage to fix durability issues
4+
15
## 0.10.1
26

37
- For database setups not using a shared worker, use a `BroadcastChannel` to share updates across different tabs.

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

+17-3
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,21 +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();
142+
if (flush != false) {
143+
await this.flush();
144+
}
140145
}
141146
});
142147
} else {
@@ -148,11 +153,20 @@ class WebDatabase
148153
return await callback(context);
149154
} finally {
150155
context.markClosed();
156+
if (flush != false) {
157+
await this.flush();
158+
}
151159
await _database.customRequest(
152160
CustomDatabaseMessage(CustomDatabaseMessageKind.releaseLock));
153161
}
154162
}
155163
}
164+
165+
@override
166+
Future<void> flush() async {
167+
await isInitialized;
168+
return _database.fileSystem.flush();
169+
}
156170
}
157171

158172
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
}

packages/sqlite_async/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: sqlite_async
22
description: High-performance asynchronous interface for SQLite on Dart and Flutter.
3-
version: 0.10.1
3+
version: 0.11.0
44
repository: https://github.com/powersync-ja/sqlite_async.dart
55
environment:
66
sdk: ">=3.4.0 <4.0.0"

0 commit comments

Comments
 (0)