clean up single-record open remnants

This commit is contained in:
Christien Rioux 2024-02-25 10:03:41 -05:00
parent e262b0f777
commit ce4e19f88d
2 changed files with 45 additions and 28 deletions

View File

@ -42,10 +42,14 @@ class AccountRepository {
valueFromJson: (obj) => obj == null ? null : TypedKey.fromJson(obj), valueFromJson: (obj) => obj == null ? null : TypedKey.fromJson(obj),
valueToJson: (val) => val?.toJson()); valueToJson: (val) => val?.toJson());
//////////////////////////////////////////////////////////////
/// Fields
final TableDBValue<IList<LocalAccount>> _localAccounts; final TableDBValue<IList<LocalAccount>> _localAccounts;
final TableDBValue<IList<UserLogin>> _userLogins; final TableDBValue<IList<UserLogin>> _userLogins;
final TableDBValue<TypedKey?> _activeLocalAccount; final TableDBValue<TypedKey?> _activeLocalAccount;
final StreamController<AccountRepositoryChange> _streamController; final StreamController<AccountRepositoryChange> _streamController;
final Map<TypedKey, DHTRecord> _openedAccountRecords = {};
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
/// Singleton initialization /// Singleton initialization
@ -59,6 +63,10 @@ class AccountRepository {
await _openLoggedInDHTRecords(); await _openLoggedInDHTRecords();
} }
Future<void> close() async {
await _closeLoggedInDHTRecords();
}
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
/// Streams /// Streams
@ -132,9 +140,8 @@ class AccountRepository {
} }
// Pull the account DHT key, decode it and return it // Pull the account DHT key, decode it and return it
final pool = DHTRecordPool.instance; final accountRecord =
final accountRecord = pool _getAccountRecord(userLogin.accountRecordInfo.accountRecord.recordKey);
.getOpenedRecord(userLogin.accountRecordInfo.accountRecord.recordKey);
if (accountRecord == null) { if (accountRecord == null) {
// Account could not be read or decrypted from DHT // Account could not be read or decrypted from DHT
return AccountInfo( return AccountInfo(
@ -367,7 +374,6 @@ class AccountRepository {
Future<void> logout(TypedKey? accountMasterRecordKey) async { Future<void> logout(TypedKey? accountMasterRecordKey) async {
// Resolve which user to log out // Resolve which user to log out
//final userLogins = await _userLogins.get();
final activeLocalAccount = await _activeLocalAccount.get(); final activeLocalAccount = await _activeLocalAccount.get();
final logoutUser = accountMasterRecordKey ?? activeLocalAccount; final logoutUser = accountMasterRecordKey ?? activeLocalAccount;
if (logoutUser == null) { if (logoutUser == null) {
@ -382,10 +388,9 @@ class AccountRepository {
} }
// Close DHT records for this account // Close DHT records for this account
final pool = DHTRecordPool.instance;
final accountRecordKey = final accountRecordKey =
logoutUserLogin.accountRecordInfo.accountRecord.recordKey; logoutUserLogin.accountRecordInfo.accountRecord.recordKey;
final accountRecord = pool.getOpenedRecord(accountRecordKey); final accountRecord = _openedAccountRecords.remove(accountRecordKey);
await accountRecord?.close(); await accountRecord?.close();
// Remove user from active logins list // Remove user from active logins list
@ -396,39 +401,52 @@ class AccountRepository {
} }
Future<void> _openLoggedInDHTRecords() async { Future<void> _openLoggedInDHTRecords() async {
final pool = DHTRecordPool.instance;
// For all user logins if they arent open yet // For all user logins if they arent open yet
final userLogins = await _userLogins.get(); final userLogins = await _userLogins.get();
for (final userLogin in userLogins) { for (final userLogin in userLogins) {
//// Account record key ///////////////////////////// await _openAccountRecord(userLogin);
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();
}
} }
} }
Future<void> _closeLoggedInDHTRecords() async { Future<void> _closeLoggedInDHTRecords() async {
final pool = DHTRecordPool.instance;
final userLogins = await _userLogins.get(); final userLogins = await _userLogins.get();
for (final userLogin in userLogins) { for (final userLogin in userLogins) {
//// Account record key ///////////////////////////// //// Account record key /////////////////////////////
final accountRecordKey = final accountRecordKey =
userLogin.accountRecordInfo.accountRecord.recordKey; userLogin.accountRecordInfo.accountRecord.recordKey;
final accountRecord = pool.getOpenedRecord(accountRecordKey); await _closeAccountRecord(accountRecordKey);
await accountRecord?.close();
} }
} }
Future<DHTRecord> _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<void> _closeAccountRecord(TypedKey accountRecordKey) async {
final accountRecord = _openedAccountRecords.remove(accountRecordKey);
await accountRecord?.close();
}
} }

View File

@ -2,7 +2,6 @@ import 'dart:async';
import 'dart:math'; import 'dart:math';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:async_tools/async_tools.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart'; import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:freezed_annotation/freezed_annotation.dart';