Merge branch 'krille/dispose-client' into 'master'

dispose client

See merge request famedly/famedlysdk!305
This commit is contained in:
Christian Pauly 2020-05-18 14:01:14 +00:00
commit d69277480e

View file

@ -971,7 +971,7 @@ class Client {
Future<dynamic> _syncRequest; Future<dynamic> _syncRequest;
Future<void> _sync() async { Future<void> _sync() async {
if (isLogged() == false) return; if (isLogged() == false || _disposed) return;
var action = '/client/r0/sync?filter=$syncFilters'; var action = '/client/r0/sync?filter=$syncFilters';
@ -981,11 +981,13 @@ class Client {
} }
try { try {
_syncRequest = jsonRequest(type: HTTPType.GET, action: action); _syncRequest = jsonRequest(type: HTTPType.GET, action: action);
if (_disposed) return;
final hash = _syncRequest.hashCode; final hash = _syncRequest.hashCode;
final syncResp = await _syncRequest; final syncResp = await _syncRequest;
if (hash != _syncRequest.hashCode) return; if (hash != _syncRequest.hashCode) return;
_timeoutFactor = 1; _timeoutFactor = 1;
final futures = handleSync(syncResp); final futures = handleSync(syncResp);
if (_disposed) return;
await database?.transaction(() async { await database?.transaction(() async {
for (final f in futures) { for (final f in futures) {
await f(); await f();
@ -2174,4 +2176,15 @@ class Client {
rethrow; rethrow;
} }
} }
bool _disposed = false;
/// Stops the synchronization and closes the database. After this
/// you can safely make this Client instance null.
Future<void> dispose() async {
_disposed = true;
await database?.close();
database = null;
return;
}
} }