diff --git a/chronik/chronik-indexer/src/indexer.rs b/chronik/chronik-indexer/src/indexer.rs --- a/chronik/chronik-indexer/src/indexer.rs +++ b/chronik/chronik-indexer/src/indexer.rs @@ -411,9 +411,11 @@ hash: block.db_block.hash, height: block.db_block.height, }); - for tx in &block.txs { - subs.handle_tx_event(tx, TxMsgType::Confirmed, &token_id_aux); - } + subs.handle_block_tx_events( + &block.txs, + TxMsgType::Confirmed, + &token_id_aux, + ); Ok(()) } @@ -512,9 +514,11 @@ } else { TokenIdGroupAux::default() }; - for tx in &block.txs { - subs.handle_tx_event(tx, TxMsgType::Finalized, &token_id_aux); - } + subs.handle_block_tx_events( + &block.txs, + TxMsgType::Finalized, + &token_id_aux, + ); Ok(()) } diff --git a/chronik/chronik-indexer/src/subs.rs b/chronik/chronik-indexer/src/subs.rs --- a/chronik/chronik-indexer/src/subs.rs +++ b/chronik/chronik-indexer/src/subs.rs @@ -83,6 +83,24 @@ .handle_tx_event(tx, token_id_aux, msg_type); } + /// Send out msg_type updates for the txs of the block to subscribers. + pub fn handle_block_tx_events( + &mut self, + txs: &[Tx], + msg_type: TxMsgType, + token_id_aux: &TokenIdGroupAux, + ) { + if self.subs_script.is_empty() && self.subs_token_id.is_empty() { + // Short-circuit if no subscriptions + return; + } + for tx in txs { + self.subs_script.handle_tx_event(tx, &(), msg_type); + self.subs_token_id + .handle_tx_event(tx, token_id_aux, msg_type); + } + } + pub(crate) fn broadcast_block_msg(&self, msg: BlockMsg) { if self.subs_block.receiver_count() > 0 { if let Err(err) = self.subs_block.send(msg) { diff --git a/chronik/chronik-indexer/src/subs_group.rs b/chronik/chronik-indexer/src/subs_group.rs --- a/chronik/chronik-indexer/src/subs_group.rs +++ b/chronik/chronik-indexer/src/subs_group.rs @@ -111,4 +111,9 @@ } } } + + /// Whether there are no subscribers for this group + pub fn is_empty(&self) -> bool { + self.subs.is_empty() + } }