Skip to content

Commit

Permalink
fix: add staggering to isolate spawning to avoid freezing of app
Browse files Browse the repository at this point in the history
  • Loading branch information
kumulynja committed Jan 7, 2025
1 parent c518ffb commit ab50bc9
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lib/_pkg/payjoin/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -324,31 +324,44 @@ class PayjoinManager {
final (senderSessions, senderErr) = await _payjoinStorage.readAllSenders();
if (senderErr != null) throw senderErr;

debugPrint('Found ${receiverSessions.length} receiver sessions');
debugPrint('Found ${senderSessions.length} sender sessions');

final filteredReceivers = receiverSessions
.where((session) =>
session.walletId == wallet.id &&
session.status != PayjoinSessionStatus.success &&
session.status != PayjoinSessionStatus.unrecoverable)
.toList();
debugPrint('Filtered receivers: ${filteredReceivers.length}');

final filteredSenders = senderSessions.where((session) {
return session.walletId == wallet.id &&
session.status != PayjoinSessionStatus.success &&
session.status != PayjoinSessionStatus.unrecoverable;
}).toList();

final spawnedReceivers = filteredReceivers.map((session) {
return spawnReceiver(
isTestnet: session.isTestnet,
receiver: session.receiver,
wallet: wallet,
debugPrint('Filtered senders: ${filteredSenders.length}');

var i = 0; // Use this to stagger the spawning of sessions
final spawnedReceivers = filteredReceivers.map((session) async {
return await Future.delayed(
Duration(seconds: 1 * i++),
() => spawnReceiver(
isTestnet: session.isTestnet,
receiver: session.receiver,
wallet: wallet,
),
);
});
final spawnedSenders = filteredSenders.map((session) {
return spawnSender(
isTestnet: session.isTestnet,
sender: session.sender,
wallet: wallet,
pjUri: session.pjUri,
final spawnedSenders = filteredSenders.map((session) async {
return await Future.delayed(
Duration(seconds: 1 * i++),
() => spawnSender(
isTestnet: session.isTestnet,
sender: session.sender,
wallet: wallet,
pjUri: session.pjUri,
),
);
});

Expand Down

0 comments on commit ab50bc9

Please sign in to comment.