Skip to content

Commit

Permalink
Immediately listen inputs and move the wait for node state after event (
Browse files Browse the repository at this point in the history
#687)

* Immediatly listen inputs and move the wait for node state after event

* Fix test

* Fix format
  • Loading branch information
ademar111190 authored Oct 19, 2023
1 parent 5631f90 commit 00cc983
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions lib/bloc/input/input_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import 'package:c_breez/bloc/input/input_state.dart';
import 'package:c_breez/models/invoice.dart';
import 'package:c_breez/services/device.dart';
import 'package:c_breez/services/lightning_links.dart';
import 'package:logging/logging.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart';

class InputBloc extends Cubit<InputState> {
Expand All @@ -33,7 +33,6 @@ class InputBloc extends Cubit<InputState> {

void _initializeInputBloc() async {
_log.info("initializeInputBloc");
await _breezLib.nodeStateStream.firstWhere((nodeState) => nodeState != null);
_watchIncomingInvoices().listen((inputState) => emit(inputState!));
}

Expand All @@ -43,6 +42,7 @@ class InputBloc extends Cubit<InputState> {
}

Future trackPayment(String paymentHash) async {
_log.info("trackPayment: $paymentHash");
await _breezLib.invoicePaidStream.firstWhere((invoice) {
_log.info("invoice paid: ${invoice.paymentHash} we are waiting for "
"$paymentHash, same: ${invoice.paymentHash == paymentHash}");
Expand All @@ -51,6 +51,7 @@ class InputBloc extends Cubit<InputState> {
}

Stream<InputState?> _watchIncomingInvoices() {
_log.info("watchIncomingInvoices");
return Rx.merge([
_decodeInvoiceController.stream.doOnData((event) => _log.info("decodeInvoiceController: $event")),
_lightningLinks.linksNotifications
Expand All @@ -63,6 +64,8 @@ class InputBloc extends Cubit<InputState> {
.doOnData((event) => _log.info("clipboardStream: $event")),
]).asyncMap((input) async {
_log.info("Incoming input: '$input'");
// wait for node state to be available
await _waitForNodeState();
// Emit an empty InputState with isLoading to display a loader on UI layer
emit(const InputState.loading());
try {
Expand All @@ -76,6 +79,7 @@ class InputBloc extends Cubit<InputState> {
}

Future<InputState> handlePaymentRequest(InputType_Bolt11 inputData, InputSource source) async {
_log.info("handlePaymentRequest: $inputData source: $source");
final LNInvoice lnInvoice = inputData.invoice;

NodeState? nodeState = await _breezLib.nodeInfo();
Expand Down Expand Up @@ -118,5 +122,14 @@ class InputBloc extends Cubit<InputState> {
return result;
}

Future<InputType> parseInput({required String input}) async => await _breezLib.parseInput(input: input);
Future<InputType> parseInput({required String input}) async {
_log.info("parseInput: $input");
return await _breezLib.parseInput(input: input);
}

Future<void> _waitForNodeState() async {
_log.info("waitForNodeState");
await _breezLib.nodeStateStream.firstWhere((nodeState) => nodeState != null);
_log.info("waitForNodeState: done");
}
}
2 changes: 1 addition & 1 deletion test/mock/breez_bridge_mock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class BreezSDKMock extends Mock implements BreezSDK {
Stream<InvoicePaidDetails> get invoicePaidStream => invoicePaidController.stream;

@override
final nodeStateController = StreamController<NodeState?>.broadcast();
final nodeStateController = BehaviorSubject<NodeState?>();

@override
Stream<NodeState?> get nodeStateStream => nodeStateController.stream;
Expand Down
2 changes: 1 addition & 1 deletion test/unit_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void setUpLogger() {
Logger.root.onRecord.listen((record) {
// Dart analyzer doesn't understand that here we are in debug mode so we have to use kDebugMode again
if (kDebugMode) {
print("[${record.loggerName}] {${record.level.name}} (${record.time}) : ${record.message}");
debugPrint("[${record.loggerName}] {${record.level.name}} (${record.time}) : ${record.message}");
}
});
}
Expand Down

0 comments on commit 00cc983

Please sign in to comment.