Skip to content

Commit 7557d82

Browse files
committed
Fix tests
1 parent 71f279b commit 7557d82

File tree

6 files changed

+19
-47
lines changed

6 files changed

+19
-47
lines changed

packages/sqlite_async/lib/src/native/database/native_sqlite_connection_impl.dart

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -303,40 +303,13 @@ Future<void> _sqliteConnectionIsolateInner(_SqliteConnectionParams params,
303303
final server = params.portServer;
304304
final commandPort = ReceivePort();
305305

306-
Timer? updateDebouncer;
307-
Set<String> updatedTables = {};
306+
db.throttledUpdatedTables.listen((changedTables) {
307+
client.fire(UpdateNotification(changedTables));
308+
});
309+
308310
int? txId;
309311
Object? txError;
310312

311-
void maybeFireUpdates() {
312-
// We keep buffering the set of updated tables until we are not
313-
// in a transaction. Firing transactions inside a transaction
314-
// has multiple issues:
315-
// 1. Watched queries would detect changes to the underlying tables,
316-
// but the data would not be visible to queries yet.
317-
// 2. It would trigger many more notifications than required.
318-
//
319-
// This still includes updates for transactions that are rolled back.
320-
// We could handle those better at a later stage.
321-
322-
if (updatedTables.isNotEmpty && db.autocommit) {
323-
client.fire(UpdateNotification(updatedTables));
324-
updatedTables.clear();
325-
}
326-
updateDebouncer?.cancel();
327-
updateDebouncer = null;
328-
}
329-
330-
db.updates.listen((event) {
331-
updatedTables.add(event.tableName);
332-
333-
// This handles two cases:
334-
// 1. Update arrived after _SqliteIsolateClose (not sure if this could happen).
335-
// 2. Long-running _SqliteIsolateClosure that should fire updates while running.
336-
updateDebouncer ??=
337-
Timer(const Duration(milliseconds: 1), maybeFireUpdates);
338-
});
339-
340313
ResultSet runStatement(_SqliteIsolateStatement data) {
341314
if (data.sql == 'BEGIN' || data.sql == 'BEGIN IMMEDIATE') {
342315
if (txId != null) {
@@ -388,8 +361,6 @@ Future<void> _sqliteConnectionIsolateInner(_SqliteConnectionParams params,
388361
throw sqlite.SqliteException(
389362
0, 'Transaction must be closed within the read or write lock');
390363
}
391-
// We would likely have received updates by this point - fire now.
392-
maybeFireUpdates();
393364
return null;
394365
case _SqliteIsolateStatement():
395366
return task.timeSync(
@@ -399,11 +370,7 @@ Future<void> _sqliteConnectionIsolateInner(_SqliteConnectionParams params,
399370
parameters: data.args,
400371
);
401372
case _SqliteIsolateClosure():
402-
try {
403-
return await data.cb(db);
404-
} finally {
405-
maybeFireUpdates();
406-
}
373+
return await data.cb(db);
407374
case _SqliteIsolateConnectionClose():
408375
db.dispose();
409376
return null;

packages/sqlite_async/lib/src/utils/shared_utils.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ extension ThrottledUpdates on CommonDatabase {
123123

124124
controller = StreamController(onListen: () {
125125
txSubscription = commits.listen((_) {
126+
print('did commit');
126127
maybeFireUpdates();
127128
}, onError: (error) {
128129
controller?.addError(error);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ final class UpdateNotificationStreams {
2626
.map((e) => (e as JSString).toDart)
2727
.toSet());
2828

29-
_updates[customRequest.rawSql.toDart]?.add(notification);
29+
final controller = _updates[customRequest.rawSql.toDart];
30+
controller?.add(notification);
3031
}
3132

3233
return null;
@@ -50,6 +51,8 @@ final class UpdateNotificationStreams {
5051
id,
5152
[false],
5253
));
54+
55+
_updates.remove(id);
5356
};
5457

5558
return controller.stream;

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:sqlite_async/web.dart';
1010
import 'database.dart';
1111
import 'worker/worker_utils.dart';
1212

13-
Map<String, FutureOr<WebSqlite>> webSQLiteImplementations = {};
13+
Map<String, FutureOr<WebSqlite>> _webSQLiteImplementations = {};
1414

1515
/// Web implementation of [AbstractDefaultSqliteOpenFactory]
1616
class DefaultSqliteOpenFactory
@@ -20,13 +20,13 @@ class DefaultSqliteOpenFactory
2020
final cacheKey = sqliteOptions.webSqliteOptions.wasmUri +
2121
sqliteOptions.webSqliteOptions.workerUri;
2222

23-
if (webSQLiteImplementations.containsKey(cacheKey)) {
24-
return webSQLiteImplementations[cacheKey]!;
23+
if (_webSQLiteImplementations.containsKey(cacheKey)) {
24+
return _webSQLiteImplementations[cacheKey]!;
2525
}
2626

27-
webSQLiteImplementations[cacheKey] =
27+
_webSQLiteImplementations[cacheKey] =
2828
openWebSqlite(sqliteOptions.webSqliteOptions);
29-
return webSQLiteImplementations[cacheKey]!;
29+
return _webSQLiteImplementations[cacheKey]!;
3030
});
3131

3232
DefaultSqliteOpenFactory(
@@ -42,6 +42,7 @@ class DefaultSqliteOpenFactory
4242
wasmModule: Uri.parse(sqliteOptions.webSqliteOptions.wasmUri),
4343
worker: Uri.parse(sqliteOptions.webSqliteOptions.workerUri),
4444
controller: AsyncSqliteController(),
45+
handleCustomRequest: handleCustomRequest,
4546
);
4647
}
4748

packages/sqlite_async/lib/web.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ typedef WebDatabaseEndpoint = ({
2929
String? lockName,
3030
});
3131

32+
final UpdateNotificationStreams _updateStreams = UpdateNotificationStreams();
33+
3234
/// An additional interface for [SqliteOpenFactory] exposing additional
3335
/// functionality that is only relevant when compiling to the web.
3436
///
3537
/// The [DefaultSqliteOpenFactory] class implements this interface only when
3638
/// compiling for the web.
3739
abstract mixin class WebSqliteOpenFactory
3840
implements SqliteOpenFactory<CommonDatabase> {
39-
final UpdateNotificationStreams _updateStreams = UpdateNotificationStreams();
40-
4141
/// Handles a custom request sent from the worker to the client.
4242
Future<JSAny?> handleCustomRequest(JSAny? request) {
4343
return _updateStreams.handleRequest(request);

scripts/sqlite3_wasm_download.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ library;
44
import 'dart:io';
55

66
final sqliteUrl =
7-
'https://github.com/simolus3/sqlite3.dart/releases/download/sqlite3-2.4.3/sqlite3.wasm';
7+
'https://github.com/simolus3/sqlite3.dart/releases/download/sqlite3-2.8.0/sqlite3.wasm';
88

99
void main() async {
1010
// Create assets directory if it doesn't exist

0 commit comments

Comments
 (0)