From 986dd74c654e36e3c7c59bcafc20b4135f24c670 Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Mon, 2 Oct 2023 15:01:02 +0700 Subject: [PATCH] fixup! fixup! Fix clear DB when logout in web --- lib/src/client.dart | 14 ++-- .../database/hive_collections_database.dart | 71 ++++++++++--------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index a8968218..72fb1dcc 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -109,7 +109,7 @@ class Client extends MatrixApi { final Duration sendTimelineEventTimeout; - bool _kIsWeb = false; + bool _supportDeleteCache = false; Future Function( MatrixImageFileResizeArguments)? customImageResizer; @@ -1035,7 +1035,7 @@ class Client extends MatrixApi { await abortSync(); await dispose(closeDatabase: false); - final export = await database!.exportDump(isWeb: _kIsWeb); + final export = await database!.exportDump(isWeb: _supportDeleteCache); await clear(); return export; @@ -1056,7 +1056,7 @@ class Client extends MatrixApi { _database ??= await databaseBuilder!.call(this); - final success = await database!.importDump(export, isWeb: _kIsWeb); + final success = await database!.importDump(export, isWeb: _supportDeleteCache); if (success) { // closing including DB @@ -1590,7 +1590,7 @@ class Client extends MatrixApi { Logs().outputEvents.clear(); try { await abortSync(); - await database?.clear(isWeb: _kIsWeb); + await database?.clear(isWeb: _supportDeleteCache); _backgroundSync = true; } catch (e, s) { Logs().e('Unable to clear database', e, s); @@ -3055,7 +3055,7 @@ class Client extends MatrixApi { Logs().e('Unable to migrate inbound group sessions!', e, s); } - await legacyDatabase.clear(isWeb: _kIsWeb); + await legacyDatabase.clear(isWeb: _supportDeleteCache); } await legacyDatabase?.close(); _initLock = false; @@ -3067,8 +3067,8 @@ class Client extends MatrixApi { } } - set isSupportPlatformWeb(bool kIsWeb) { - _kIsWeb = kIsWeb; + set isSupportDeleteCache(bool kIsWeb) { + _supportDeleteCache = kIsWeb; } } diff --git a/lib/src/database/hive_collections_database.dart b/lib/src/database/hive_collections_database.dart index 0df83f92..c1b10604 100644 --- a/lib/src/database/hive_collections_database.dart +++ b/lib/src/database/hive_collections_database.dart @@ -244,43 +244,44 @@ class HiveCollectionsDatabase extends DatabaseApi { @override Future clear({ bool isWeb = false, - }) async { - await _clientBox.clear(); - await _accountDataBox.clear(); - await _roomsBox.clear(); - await _roomStateBox.clear(); - await _roomMembersBox.clear(); - await _toDeviceQueueBox.clear(); - await _roomAccountDataBox.clear(); - await _inboundGroupSessionsBox.clear(); - await _outboundGroupSessionsBox.clear(); - await _olmSessionsBox.clear(); - await _userDeviceKeysBox.clear(); - await _userDeviceKeysOutdatedBox.clear(); - await _userCrossSigningKeysBox.clear(); - await _ssssCacheBox.clear(); - await _presencesBox.clear(); - await _timelineFragmentsBox.clear(); - await _eventsBox.clear(); - await _seenDeviceIdsBox.clear(); - await _seenDeviceKeysBox.clear(); - if (!isWeb) { - await _collection.deleteFromDisk(); - } - } + }) => + transaction(() async { + await _clientBox.clear(); + await _accountDataBox.clear(); + await _roomsBox.clear(); + await _roomStateBox.clear(); + await _roomMembersBox.clear(); + await _toDeviceQueueBox.clear(); + await _roomAccountDataBox.clear(); + await _inboundGroupSessionsBox.clear(); + await _outboundGroupSessionsBox.clear(); + await _olmSessionsBox.clear(); + await _userDeviceKeysBox.clear(); + await _userDeviceKeysOutdatedBox.clear(); + await _userCrossSigningKeysBox.clear(); + await _ssssCacheBox.clear(); + await _presencesBox.clear(); + await _timelineFragmentsBox.clear(); + await _eventsBox.clear(); + await _seenDeviceIdsBox.clear(); + await _seenDeviceKeysBox.clear(); + if (!isWeb) { + await _collection.deleteFromDisk(); + } + }); @override - Future clearCache() async { - await _roomsBox.clear(); - await _accountDataBox.clear(); - await _roomStateBox.clear(); - await _roomMembersBox.clear(); - await _eventsBox.clear(); - await _timelineFragmentsBox.clear(); - await _outboundGroupSessionsBox.clear(); - await _presencesBox.clear(); - await _clientBox.delete('prev_batch'); - } + Future clearCache() => transaction(() async { + await _roomsBox.clear(); + await _accountDataBox.clear(); + await _roomStateBox.clear(); + await _roomMembersBox.clear(); + await _eventsBox.clear(); + await _timelineFragmentsBox.clear(); + await _outboundGroupSessionsBox.clear(); + await _presencesBox.clear(); + await _clientBox.delete('prev_batch'); + }); @override Future clearSSSSCache() => _ssssCacheBox.clear();