From 6d46833eb2a068bd3655859ea828dad04293e5ba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 4 Feb 2020 03:28:47 +0100 Subject: Support event subscriptions in `iced_web` Also improves the overall web runtime, avoiding nested update loops. --- web/src/bus.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'web/src/bus.rs') diff --git a/web/src/bus.rs b/web/src/bus.rs index b3984aff..c66e9659 100644 --- a/web/src/bus.rs +++ b/web/src/bus.rs @@ -1,5 +1,4 @@ -use crate::Instance; - +use iced_futures::futures::channel::mpsc; use std::rc::Rc; /// A publisher of messages. @@ -9,13 +8,13 @@ use std::rc::Rc; /// [`Application`]: trait.Application.html #[allow(missing_debug_implementations)] pub struct Bus { - publish: Rc>, + publish: Rc ()>>, } impl Clone for Bus { fn clone(&self) -> Self { - Self { - publish: Rc::clone(&self.publish), + Bus { + publish: self.publish.clone(), } } } @@ -24,12 +23,10 @@ impl Bus where Message: 'static, { - pub(crate) fn new() -> Self { + pub(crate) fn new(publish: mpsc::UnboundedSender) -> Self { Self { - publish: Rc::new(Box::new(|message, root| { - let app = root.unwrap_mut::>(); - - app.update(message) + publish: Rc::new(Box::new(move |message| { + publish.unbounded_send(message).expect("Send message"); })), } } @@ -37,8 +34,8 @@ where /// Publishes a new message for the [`Application`]. /// /// [`Application`]: trait.Application.html - pub fn publish(&self, message: Message, root: &mut dyn dodrio::RootRender) { - (self.publish)(message, root); + pub fn publish(&self, message: Message) { + (self.publish)(message) } /// Creates a new [`Bus`] that applies the given function to the messages @@ -52,9 +49,7 @@ where let publish = self.publish.clone(); Bus { - publish: Rc::new(Box::new(move |message, root| { - publish(mapper(message), root) - })), + publish: Rc::new(Box::new(move |message| publish(mapper(message)))), } } } -- cgit