diff --git a/lib/src/repositories/ton_wallet_repository/ton_wallet_repository_impl.dart b/lib/src/repositories/ton_wallet_repository/ton_wallet_repository_impl.dart index 05eb78e..99f764d 100644 --- a/lib/src/repositories/ton_wallet_repository/ton_wallet_repository_impl.dart +++ b/lib/src/repositories/ton_wallet_repository/ton_wallet_repository_impl.dart @@ -26,13 +26,6 @@ mixin TonWalletRepositoryImpl implements TonWalletRepository { /// Current transport of application TransportStrategy get currentTransport; - /// Last assets that were used for subscription. - /// This value is used during [updateSubscriptions] to detect which wallets - /// should be unsubscribed and which of them could be used. - @protected - @visibleForTesting - List? lastUpdatedAssets; - /// Subject that allows listening for wallets subscribing/unsubscribing final _walletsSubject = BehaviorSubject>.seeded({}); @@ -169,9 +162,8 @@ mixin TonWalletRepositoryImpl implements TonWalletRepository { @override Future updateSubscriptions(List assets) async { - final last = lastUpdatedAssets; final toSubscribe = []; - final toUnsubscribe = []; + final toUnsubscribe = []; // Stop last created operation if possible final oldOperation = _lastOperation; @@ -180,25 +172,19 @@ mixin TonWalletRepositoryImpl implements TonWalletRepository { await oldOperation.cancel(); } - if (last != null) { - toUnsubscribe.addAll( - // pick all elements from old list, which is not contains in a new list - last.where((l) => !assets.any((a) => a.address == l.address)), - ); - toSubscribe.addAll( - // pick all elements from new list, which is not contains in old list - assets.where((a) => !last.any((l) => l.address == a.address)), - ); - } else { - toSubscribe.addAll(assets); - } + toUnsubscribe.addAll( + // pick all elements from old list, which is not contains in a new list + wallets.where((l) => assets.every((a) => a.address != l.address)), + ); + toSubscribe.addAll( + // pick all elements from new list, which is not contains in old list + assets.where((a) => wallets.every((l) => l.address != a.address)), + ); for (final asset in toUnsubscribe) { unsubscribe(asset.address); } - lastUpdatedAssets = assets; - late CancelableOperation operation; operation = CancelableOperation.fromFuture(() async { @@ -225,15 +211,21 @@ mixin TonWalletRepositoryImpl implements TonWalletRepository { @override Future updateTransportSubscriptions() async { - closeAllSubscriptions(); + final assets = wallets + .map( + (e) => TonWalletAsset( + address: e.address, + publicKey: e.publicKey, + contract: e.walletType, + ), + ) + .toList(); - final last = lastUpdatedAssets; - if (last == null) return; + closeAllSubscriptions(); - // make null to avoid comparing for subscriptions - lastUpdatedAssets = null; + if (assets.isEmpty) return; - return updateSubscriptions(last); + return updateSubscriptions(assets); } @override