feat: safe dispose while _sync

This commit is contained in:
Lukas Lihotzki 2020-08-21 17:20:26 +02:00
parent 09ffa09404
commit d6b97b8e78

View file

@ -675,18 +675,19 @@ class Client extends MatrixApi {
_lastSyncError = e; _lastSyncError = e;
return null; return null;
}); });
if (_disposed) return;
final hash = _syncRequest.hashCode; final hash = _syncRequest.hashCode;
final syncResp = await _syncRequest; final syncResp = await _syncRequest;
if (_disposed) return;
if (syncResp == null) throw _lastSyncError; if (syncResp == null) throw _lastSyncError;
if (hash != _syncRequest.hashCode) return; if (hash != _syncRequest.hashCode) return;
if (database != null) { if (database != null) {
await database.transaction(() async { _currentTransaction = database.transaction(() async {
await handleSync(syncResp); await handleSync(syncResp);
if (prevBatch != syncResp.nextBatch) { if (prevBatch != syncResp.nextBatch) {
await database.storePrevBatch(syncResp.nextBatch, id); await database.storePrevBatch(syncResp.nextBatch, id);
} }
}); });
await _currentTransaction;
} else { } else {
await handleSync(syncResp); await handleSync(syncResp);
} }
@ -1445,11 +1446,17 @@ class Client extends MatrixApi {
} }
bool _disposed = false; bool _disposed = false;
Future _currentTransaction = Future.sync(() => {});
/// Stops the synchronization and closes the database. After this /// Stops the synchronization and closes the database. After this
/// you can safely make this Client instance null. /// you can safely make this Client instance null.
Future<void> dispose({bool closeDatabase = false}) async { Future<void> dispose({bool closeDatabase = false}) async {
_disposed = true; _disposed = true;
try {
await _currentTransaction;
} catch (_) {
// No-OP
}
if (closeDatabase) await database?.close(); if (closeDatabase) await database?.close();
database = null; database = null;
return; return;