Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to emit multiple messages in one callback #660

Merged
merged 5 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) use scope::ComponentUpdate;
pub use scope::{NodeCell, Scope};

use crate::callback::Callback;
use crate::html::scope::ManyOrSingleMessage::{Many, Single};
use crate::virtual_dom::{VChild, VList, VNode};
use std::fmt;

Expand Down Expand Up @@ -224,6 +225,20 @@ where
}
}

/// This method sends a bunch of messages back to the component's loop
jstarry marked this conversation as resolved.
Show resolved Hide resolved
/// It will trigger a bunch update in one render loop
jstarry marked this conversation as resolved.
Show resolved Hide resolved
pub fn send_bunch_back<F, IN>(&mut self, function: F) -> Callback<IN>
jstarry marked this conversation as resolved.
Show resolved Hide resolved
where
F: Fn(IN) -> Vec<COMP::Message> + 'static,
{
let scope = self.scope.clone();
let closure = move |input| {
let messages = function(input);
scope.clone().send_messages(messages);
};
closure.into()
}

/// This method sends messages back to the component's loop.
pub fn send_back<F, IN>(&mut self, function: F) -> Callback<IN>
where
Expand Down
20 changes: 17 additions & 3 deletions src/html/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ use stdweb::web::{Element, Node};
/// Holder for the element.
pub type NodeCell = Rc<RefCell<Option<Node>>>;

pub(crate) enum ManyOrSingleMessage<COMP: Component> {
Single(COMP::Message),
Many(Vec<COMP::Message>),
}
/// Updates for a `Components` instance. Used by scope sender.
pub(crate) enum ComponentUpdate<COMP: Component> {
/// Wraps messages for a component.
Message(COMP::Message),
Message(ManyOrSingleMessage<COMP>),
jstarry marked this conversation as resolved.
Show resolved Hide resolved
/// Wraps properties for a component.
Properties(COMP::Properties),
}
Expand Down Expand Up @@ -57,7 +61,12 @@ where

/// Send a message to the component
pub fn send_message(&mut self, msg: COMP::Message) {
self.update(ComponentUpdate::Message(msg));
self.update(ComponentUpdate::Message(Single(msg)));
}

/// send many messages to the component
jstarry marked this conversation as resolved.
Show resolved Hide resolved
pub fn send_messages(&mut self, msgs: Vec<COMP::Message>) {
jstarry marked this conversation as resolved.
Show resolved Hide resolved
self.update(ComponentUpdate::Message(Many(msgs)));
}
}

Expand Down Expand Up @@ -249,7 +258,12 @@ 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) => match message {
Many(msgs) => msgs
.into_iter()
.fold(false, |acc, msg| this.component.update(msg) || acc),
Single(msg) => this.component.update(msg),
},
ComponentUpdate::Properties(props) => this.component.change(props),
};
let next_state = if should_update { this.update() } else { this };
Expand Down