From 00cc9838ca2f6a800ad9c4ec25032679dc7a630b Mon Sep 17 00:00:00 2001 From: Ademar Date: Thu, 19 Oct 2023 13:55:54 -0300 Subject: [PATCH] Immediately listen inputs and move the wait for node state after event (#687) * Immediatly listen inputs and move the wait for node state after event * Fix test * Fix format --- lib/bloc/input/input_bloc.dart | 19 ++++++++++++++++--- test/mock/breez_bridge_mock.dart | 2 +- test/unit_logger.dart | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/bloc/input/input_bloc.dart b/lib/bloc/input/input_bloc.dart index 10a19da8e..0d9a08286 100644 --- a/lib/bloc/input/input_bloc.dart +++ b/lib/bloc/input/input_bloc.dart @@ -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 { @@ -33,7 +33,6 @@ class InputBloc extends Cubit { void _initializeInputBloc() async { _log.info("initializeInputBloc"); - await _breezLib.nodeStateStream.firstWhere((nodeState) => nodeState != null); _watchIncomingInvoices().listen((inputState) => emit(inputState!)); } @@ -43,6 +42,7 @@ class InputBloc extends Cubit { } 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}"); @@ -51,6 +51,7 @@ class InputBloc extends Cubit { } Stream _watchIncomingInvoices() { + _log.info("watchIncomingInvoices"); return Rx.merge([ _decodeInvoiceController.stream.doOnData((event) => _log.info("decodeInvoiceController: $event")), _lightningLinks.linksNotifications @@ -63,6 +64,8 @@ class InputBloc extends Cubit { .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 { @@ -76,6 +79,7 @@ class InputBloc extends Cubit { } Future handlePaymentRequest(InputType_Bolt11 inputData, InputSource source) async { + _log.info("handlePaymentRequest: $inputData source: $source"); final LNInvoice lnInvoice = inputData.invoice; NodeState? nodeState = await _breezLib.nodeInfo(); @@ -118,5 +122,14 @@ class InputBloc extends Cubit { return result; } - Future parseInput({required String input}) async => await _breezLib.parseInput(input: input); + Future parseInput({required String input}) async { + _log.info("parseInput: $input"); + return await _breezLib.parseInput(input: input); + } + + Future _waitForNodeState() async { + _log.info("waitForNodeState"); + await _breezLib.nodeStateStream.firstWhere((nodeState) => nodeState != null); + _log.info("waitForNodeState: done"); + } } diff --git a/test/mock/breez_bridge_mock.dart b/test/mock/breez_bridge_mock.dart index 7d8d14087..2babad111 100644 --- a/test/mock/breez_bridge_mock.dart +++ b/test/mock/breez_bridge_mock.dart @@ -117,7 +117,7 @@ class BreezSDKMock extends Mock implements BreezSDK { Stream get invoicePaidStream => invoicePaidController.stream; @override - final nodeStateController = StreamController.broadcast(); + final nodeStateController = BehaviorSubject(); @override Stream get nodeStateStream => nodeStateController.stream; diff --git a/test/unit_logger.dart b/test/unit_logger.dart index 319d7ae8e..6968f05c7 100644 --- a/test/unit_logger.dart +++ b/test/unit_logger.dart @@ -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}"); } }); }