Skip to content

Commit

Permalink
Enable Agents to send messages to themselves. (#1215)
Browse files Browse the repository at this point in the history
* Agents can now send messages to themselves

* Agents callbacks now accept Into<Message>

* Clean old comment

* Remove AgentLink's send_message_batch
  • Loading branch information
totorigolo committed May 14, 2020
1 parent 657c917 commit 6ef7cec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 0 additions & 1 deletion examples/npm_and_rest/src/gravatar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ impl GravatarService {
if meta.status.is_success() {
callback.emit(data)
} else {
// format_err! is a macro in crate `failure`
callback.emit(Err(anyhow!(
"{}: error getting profile https://gravatar.com/",
meta.status
Expand Down
18 changes: 14 additions & 4 deletions yew/src/agent/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ impl<AGN: Agent> AgentLink<AGN> {
self.responder.respond(id, output);
}

/// Send a message to the agent
pub fn send_message<T>(&self, msg: T)
where
T: Into<AGN::Message>,
{
self.scope.send(AgentLifecycleEvent::Message(msg.into()));
}

/// Create a callback which will send a message to the agent when invoked.
pub fn callback<F, IN>(&self, function: F) -> Callback<IN>
pub fn callback<F, IN, M>(&self, function: F) -> Callback<IN>
where
F: Fn(IN) -> AGN::Message + 'static,
M: Into<AGN::Message>,
F: Fn(IN) -> M + 'static,
{
let scope = self.scope.clone();
let closure = move |input| {
let output = function(input);
let output = function(input).into();
scope.send(AgentLifecycleEvent::Message(output));
};
closure.into()
Expand Down Expand Up @@ -87,6 +96,7 @@ impl<AGN: Agent> AgentScope<AGN> {
let shared_agent = Rc::new(RefCell::new(AgentRunnable::new()));
AgentScope { shared_agent }
}

/// Schedule message for sending to agent
pub fn send(&self, update: AgentLifecycleEvent<AGN>) {
let envelope = AgentEnvelope {
Expand Down Expand Up @@ -128,7 +138,7 @@ pub(crate) enum AgentLifecycleEvent<AGN: Agent> {
Message(AGN::Message),
/// Client connected
Connected(HandlerId),
/// Received mesasge from Client
/// Received message from Client
Input(AGN::Input, HandlerId),
/// Client disconnected
Disconnected(HandlerId),
Expand Down
5 changes: 4 additions & 1 deletion yew/src/html/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ impl<COMP: Component> Scope<COMP> {
self.update(ComponentUpdate::Message(msg.into()), false);
}

/// Send a batch of messages to the component
/// Send a batch of messages to the component.
///
/// This is useful for reducing re-renders of the components because the messages are handled
/// together and the view function is called only once if needed.
pub fn send_message_batch(&self, messages: Vec<COMP::Message>) {
self.update(ComponentUpdate::MessageBatch(messages), false);
}
Expand Down

0 comments on commit 6ef7cec

Please sign in to comment.