diff --git a/src/html/mod.rs b/src/html/mod.rs index 95e10ebe984..597dc57eb40 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -224,6 +224,19 @@ where } } + /// This method sends batch of messages back to the component's loop + pub fn send_back_batch(&mut self, function: F) -> Callback + where + F: Fn(IN) -> Vec + 'static, + { + let scope = self.scope.clone(); + let closure = move |input| { + let messages = function(input); + scope.clone().send_message_batch(messages); + }; + closure.into() + } + /// This method sends messages back to the component's loop. pub fn send_back(&mut self, function: F) -> Callback where diff --git a/src/html/scope.rs b/src/html/scope.rs index 280a5dd05c5..cf94a0f9530 100644 --- a/src/html/scope.rs +++ b/src/html/scope.rs @@ -13,6 +13,8 @@ pub type NodeCell = Rc>>; pub(crate) enum ComponentUpdate { /// Wraps messages for a component. Message(COMP::Message), + /// Wraps batch of messages for a component. + MessageBatch(Vec), /// Wraps properties for a component. Properties(COMP::Properties), } @@ -59,6 +61,11 @@ where pub fn send_message(&mut self, msg: COMP::Message) { self.update(ComponentUpdate::Message(msg)); } + + /// send batch of messages to the component + pub fn send_message_batch(&mut self, messages: Vec) { + self.update(ComponentUpdate::MessageBatch(messages)); + } } enum ComponentState { @@ -249,7 +256,10 @@ where self.shared_state.replace(match current_state { ComponentState::Created(mut this) => { let should_update = match self.update { - ComponentUpdate::Message(msg) => this.component.update(msg), + ComponentUpdate::Message(message) => this.component.update(message), + ComponentUpdate::MessageBatch(messages) => messages + .into_iter() + .fold(false, |acc, msg| this.component.update(msg) || acc), ComponentUpdate::Properties(props) => this.component.change(props), }; let next_state = if should_update { this.update() } else { this };