Support async batch callbacks (#483)

This commit is contained in:
Simon Binder 2020-04-10 12:42:09 +02:00
parent 72e65611a7
commit 424c2febda
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 26 additions and 2 deletions

View File

@ -405,8 +405,13 @@ mixin QueryEngine on DatabaseConnectionUser {
final engine = _resolvedEngine;
final batch = Batch._(engine, engine is! Transaction);
runInBatch(batch);
return batch._commit();
final result = runInBatch(batch);
if (result is Future) {
return result.then((_) => batch._commit());
} else {
return batch._commit();
}
}
/// Runs [calculation] in a forked [Zone] that has its [_resolvedEngine] set

View File

@ -94,6 +94,25 @@ void main() {
'js': [Skip('Blocked by https://github.com/dart-lang/mockito/issues/198')]
});
test('supports async batch functions', () async {
await db.batch((batch) async {
batch.insert(
db.categories, CategoriesCompanion.insert(description: 'first'));
await Future.delayed(Duration.zero);
batch.insert(
db.categories, CategoriesCompanion.insert(description: 'second'));
});
verify(executor.transactions.runBatched([
BatchedStatement('INSERT INTO categories (`desc`) VALUES (?)', [
['first'],
['second']
]),
]));
});
test('updates stream queries', () async {
await db.batch((b) {
b.insert(db.todosTable, TodoEntry(id: 3, content: 'content'));