Streams properly deal with aliased tables

Fixes #16
This commit is contained in:
Simon Binder 2019-04-19 23:54:57 +02:00
parent 8bed506e0d
commit 7a44224370
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
2 changed files with 19 additions and 3 deletions

View File

@ -10,7 +10,7 @@ const _listEquality = ListEquality<dynamic>();
/// statement is reading its data and how to execute the query.
class QueryStreamFetcher<T> {
/// The set of tables this query reads from. If any of these tables changes,
/// the stream must fetch its again.
/// the stream must fetch its data again.
final Set<TableInfo> readsFrom;
/// Key that can be used to check whether two fetchers will yield the same
@ -89,8 +89,13 @@ class StreamQueryStore {
/// from that table.
Future<void> handleTableUpdates(Set<TableInfo> tables) async {
final activeStreams = List<QueryStream>.from(_activeStreams);
final affectedStreams = activeStreams
.where((stream) => stream._fetcher.readsFrom.any(tables.contains));
final updatedNames = tables.map((t) => t.actualTableName).toSet();
final affectedStreams = activeStreams.where((stream) {
return stream._fetcher.readsFrom.any((table) {
return updatedNames.contains(table.actualTableName);
});
});
for (var stream in affectedStreams) {
await stream.fetchAndEmitData();

View File

@ -32,6 +32,17 @@ void main() {
verify(executor.runSelect(any, any)).called(2);
});
test('streams recognize aliased tables', () {
final first = db.alias(db.users, 'one');
final second = db.alias(db.users, 'two');
db.select(first).watch().listen((_) {});
db.markTablesUpdated({second});
verify(executor.runSelect(any, any)).called(2);
});
test('equal statements yield identical streams', () {
final firstStream = (db.select(db.users).watch())..listen((_) {});
final secondStream = (db.select(db.users).watch())..listen((_) {});