From d6b97b8e787f7aa8e68c9a041ab6a4f1061395a9 Mon Sep 17 00:00:00 2001 From: Lukas Lihotzki Date: Fri, 21 Aug 2020 17:20:26 +0200 Subject: [PATCH] feat: safe dispose while _sync --- lib/src/client.dart | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 73f495a..e5623c9 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -675,18 +675,19 @@ class Client extends MatrixApi { _lastSyncError = e; return null; }); - if (_disposed) return; final hash = _syncRequest.hashCode; final syncResp = await _syncRequest; + if (_disposed) return; if (syncResp == null) throw _lastSyncError; if (hash != _syncRequest.hashCode) return; if (database != null) { - await database.transaction(() async { + _currentTransaction = database.transaction(() async { await handleSync(syncResp); if (prevBatch != syncResp.nextBatch) { await database.storePrevBatch(syncResp.nextBatch, id); } }); + await _currentTransaction; } else { await handleSync(syncResp); } @@ -1445,11 +1446,17 @@ class Client extends MatrixApi { } bool _disposed = false; + Future _currentTransaction = Future.sync(() => {}); /// Stops the synchronization and closes the database. After this /// you can safely make this Client instance null. Future dispose({bool closeDatabase = false}) async { _disposed = true; + try { + await _currentTransaction; + } catch (_) { + // No-OP + } if (closeDatabase) await database?.close(); database = null; return;