Skip to content

Commit

Permalink
Merge pull request #385 from kumulynja/pj-send-events
Browse files Browse the repository at this point in the history
Pj send events
  • Loading branch information
i5hi authored Dec 24, 2024
2 parents 0545a5d + c559d99 commit f7450cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/_pkg/payjoin/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class PayjoinSenderPostMessageASuccessEvent extends PayjoinEvent {
PayjoinSenderPostMessageASuccessEvent();
}

class PayjoinFailureEvent extends PayjoinEvent {
final Object? error;
PayjoinFailureEvent({this.error});
}

class PayjoinEventListener extends StatefulWidget {
const PayjoinEventListener({required this.child, super.key});
final Widget child;
Expand Down
13 changes: 8 additions & 5 deletions lib/_pkg/payjoin/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ class PayjoinManager {
);
await _cleanupSession(sessionId);
} else if (message is Err) {
// TODO propagate this error to the UI
PayjoinEventBus().emit(
PayjoinFailureEvent(error: message),
);
await _cleanupSession(sessionId);
}
}
Expand Down Expand Up @@ -445,11 +447,11 @@ Future<String?> _runSender(Sender sender, {required SendPort sendPort}) async {

// Attempt V2
final postRes = await _postRequest(dio, postReq);
// TODO: make an abstract class for these send port messages and subclasses for each type
sendPort.send({'type': 'request_posted'});
final getCtx = await postReqCtx.processResponse(
response: postRes.data as List<int>,
);
// TODO: make an abstract class for these send port messages and subclasses for each type
sendPort.send({'type': 'request_posted'});

while (true) {
try {
Expand All @@ -469,15 +471,16 @@ Future<String?> _runSender(Sender sender, {required SendPort sendPort}) async {
}
} catch (e) {
// If V2 fails, attempt V1
return await _runSenderV1(sender, dio);
return await _runSenderV1(sender, dio, sendPort);
}
}

/// Attempt to send payjoin using the V1 protocol.
Future<String> _runSenderV1(Sender sender, Dio dio) async {
Future<String> _runSenderV1(Sender sender, Dio dio, SendPort sendPort) async {
try {
final (req, v1Ctx) = await sender.extractV1();
final response = await _postRequest(dio, req);
sendPort.send({'type': 'request_posted'});
final proposalPsbt =
await v1Ctx.processResponse(response: response.data as List<int>);
return proposalPsbt;
Expand Down
15 changes: 12 additions & 3 deletions lib/send/bloc/send_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,18 @@ class SendCubit extends Cubit<SendState> {
// Subscribe to payjoin events to update the send state
_pjEventSubscription = PayjoinEventBus().stream.listen((event) {
if (event is PayjoinSenderPostMessageASuccessEvent) {
emit(state.copyWith(
isPayjoinPostSuccess: true,
));
emit(
state.copyWith(
isPayjoinPostSuccess: true,
),
);
} else if (event is PayjoinFailureEvent) {
emit(
state.copyWith(
errSending: event.error.toString(),
sending: false,
),
);
}
});
}
Expand Down

0 comments on commit f7450cd

Please sign in to comment.