Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add staggering to isolate spawning to avoid freezing of app #416

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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