From 104a6052e630ed91513c9addbc088810cb9a895a Mon Sep 17 00:00:00 2001 From: pm47 Date: Wed, 21 Jun 2023 13:58:05 +0200 Subject: [PATCH] add an initialized flow The purpose is to signal when the peer has loaded channels from the database, allowing the caller to differentiate between "channels not yet loaded" and "no channels at all". --- src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt index 215c48d26..9ca651368 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt @@ -123,7 +123,7 @@ class Peer( // channels map, indexed by channel id // note that a channel starts with a temporary id then switches to its final id once accepted - private val _channelsFlow = MutableStateFlow>(HashMap()) + private val _channelsFlow = MutableStateFlow>(emptyMap()) val channelsFlow: StateFlow> get() = _channelsFlow private var _channels by _channelsFlow @@ -152,6 +152,10 @@ class Peer( val currentTipFlow = MutableStateFlow?>(null) val onChainFeeratesFlow = MutableStateFlow(null) + // signals when the peer has loaded channels from the database, allowing to differentiate between "channels not yet loaded" and "no channels at all". + private val _initialized = MutableStateFlow(false) + val initialized get() = _initialized.asSharedFlow() + private val _channelLogger = nodeParams.loggerFactory.newLogger(ChannelState::class) private suspend fun ChannelState.process(cmd: ChannelCommand): Pair> { val state = this @@ -209,6 +213,7 @@ class Peer( launch { // we don't restore closed channels val channels = db.channels.listLocalChannels().filterNot { it is Closed }.associateBy { it.channelId } + _initialized.emit(true) channels.values.forEach { logger.info { "restoring channel ${it.channelId} from local storage" } val state = WaitForInit