diff --git a/lib/account_manager/repository/account_repository/account_repository.dart b/lib/account_manager/repository/account_repository/account_repository.dart index 40ce315..872393e 100644 --- a/lib/account_manager/repository/account_repository/account_repository.dart +++ b/lib/account_manager/repository/account_repository/account_repository.dart @@ -42,10 +42,14 @@ class AccountRepository { valueFromJson: (obj) => obj == null ? null : TypedKey.fromJson(obj), valueToJson: (val) => val?.toJson()); + ////////////////////////////////////////////////////////////// + /// Fields + final TableDBValue> _localAccounts; final TableDBValue> _userLogins; final TableDBValue _activeLocalAccount; final StreamController _streamController; + final Map _openedAccountRecords = {}; ////////////////////////////////////////////////////////////// /// Singleton initialization @@ -59,6 +63,10 @@ class AccountRepository { await _openLoggedInDHTRecords(); } + Future close() async { + await _closeLoggedInDHTRecords(); + } + ////////////////////////////////////////////////////////////// /// Streams @@ -132,9 +140,8 @@ class AccountRepository { } // Pull the account DHT key, decode it and return it - final pool = DHTRecordPool.instance; - final accountRecord = pool - .getOpenedRecord(userLogin.accountRecordInfo.accountRecord.recordKey); + final accountRecord = + _getAccountRecord(userLogin.accountRecordInfo.accountRecord.recordKey); if (accountRecord == null) { // Account could not be read or decrypted from DHT return AccountInfo( @@ -367,7 +374,6 @@ class AccountRepository { Future logout(TypedKey? accountMasterRecordKey) async { // Resolve which user to log out - //final userLogins = await _userLogins.get(); final activeLocalAccount = await _activeLocalAccount.get(); final logoutUser = accountMasterRecordKey ?? activeLocalAccount; if (logoutUser == null) { @@ -382,10 +388,9 @@ class AccountRepository { } // Close DHT records for this account - final pool = DHTRecordPool.instance; final accountRecordKey = logoutUserLogin.accountRecordInfo.accountRecord.recordKey; - final accountRecord = pool.getOpenedRecord(accountRecordKey); + final accountRecord = _openedAccountRecords.remove(accountRecordKey); await accountRecord?.close(); // Remove user from active logins list @@ -396,39 +401,52 @@ class AccountRepository { } Future _openLoggedInDHTRecords() async { - final pool = DHTRecordPool.instance; - // For all user logins if they arent open yet final userLogins = await _userLogins.get(); for (final userLogin in userLogins) { - //// Account record key ///////////////////////////// - final accountRecordKey = - userLogin.accountRecordInfo.accountRecord.recordKey; - final existingAccountRecord = pool.getOpenedRecord(accountRecordKey); - if (existingAccountRecord == null) { - final localAccount = - fetchLocalAccount(userLogin.accountMasterRecordKey); - - // Record not yet open, do it - final record = await pool.openOwned( - userLogin.accountRecordInfo.accountRecord, - parent: localAccount!.identityMaster.identityRecordKey); - // Watch the record's only (default) key - await record.watch(); - } + await _openAccountRecord(userLogin); } } Future _closeLoggedInDHTRecords() async { - final pool = DHTRecordPool.instance; - final userLogins = await _userLogins.get(); for (final userLogin in userLogins) { //// Account record key ///////////////////////////// final accountRecordKey = userLogin.accountRecordInfo.accountRecord.recordKey; - final accountRecord = pool.getOpenedRecord(accountRecordKey); - await accountRecord?.close(); + await _closeAccountRecord(accountRecordKey); } } + + Future _openAccountRecord(UserLogin userLogin) async { + final accountRecordKey = + userLogin.accountRecordInfo.accountRecord.recordKey; + + final existingAccountRecord = _openedAccountRecords[accountRecordKey]; + if (existingAccountRecord != null) { + return existingAccountRecord; + } + final localAccount = fetchLocalAccount(userLogin.accountMasterRecordKey)!; + + // Record not yet open, do it + final pool = DHTRecordPool.instance; + final record = await pool.openOwned( + userLogin.accountRecordInfo.accountRecord, + parent: localAccount.identityMaster.identityRecordKey); + + _openedAccountRecords[accountRecordKey] = record; + + // Watch the record's only (default) key + await record.watch(); + + return record; + } + + DHTRecord? _getAccountRecord(TypedKey accountRecordKey) => + _openedAccountRecords[accountRecordKey]; + + Future _closeAccountRecord(TypedKey accountRecordKey) async { + final accountRecord = _openedAccountRecords.remove(accountRecordKey); + await accountRecord?.close(); + } } diff --git a/packages/veilid_support/lib/dht_support/src/dht_record_pool.dart b/packages/veilid_support/lib/dht_support/src/dht_record_pool.dart index 736329e..bdc4946 100644 --- a/packages/veilid_support/lib/dht_support/src/dht_record_pool.dart +++ b/packages/veilid_support/lib/dht_support/src/dht_record_pool.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'dart:math'; import 'dart:typed_data'; -import 'package:async_tools/async_tools.dart'; import 'package:equatable/equatable.dart'; import 'package:fast_immutable_collections/fast_immutable_collections.dart'; import 'package:freezed_annotation/freezed_annotation.dart';