summaryrefslogtreecommitdiffstats
path: root/web/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-12-04 04:01:12 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-12-04 04:01:12 +0100
commit561c3641c6ac62333550c3017561fa7a18663f00 (patch)
tree3e0a502ab8e37679899002e4664a148fc474a087 /web/src
parent4293dcb2540144cc69a9f1370103bb780eca69f3 (diff)
parent6c145bbb239e87569bf4aa797ea7f8d34e25cf62 (diff)
downloadiced-561c3641c6ac62333550c3017561fa7a18663f00.tar.gz
iced-561c3641c6ac62333550c3017561fa7a18663f00.tar.bz2
iced-561c3641c6ac62333550c3017561fa7a18663f00.zip
Merge branch 'master' into feature/image-from-bytes
Diffstat (limited to 'web/src')
-rw-r--r--web/src/lib.rs42
1 files changed, 29 insertions, 13 deletions
diff --git a/web/src/lib.rs b/web/src/lib.rs
index 8239ffc9..782bcf93 100644
--- a/web/src/lib.rs
+++ b/web/src/lib.rs
@@ -138,19 +138,9 @@ pub trait Application {
Self: 'static + Sized,
{
let (app, command) = Self::new();
- let mut instance = Instance::new(app);
- instance.spawn(command);
-
- let window = web_sys::window().unwrap();
-
- let document = window.document().unwrap();
- document.set_title(&instance.title);
-
- let body = document.body().unwrap();
- let vdom = dodrio::Vdom::new(&body, instance);
-
- vdom.forget();
+ let instance = Instance::new(app);
+ instance.run(command);
}
}
@@ -158,6 +148,7 @@ pub trait Application {
struct Instance<Message> {
title: String,
ui: Rc<RefCell<Box<dyn Application<Message = Message>>>>,
+ vdom: Rc<RefCell<Option<dodrio::VdomWeak>>>,
}
impl<Message> Instance<Message>
@@ -168,6 +159,7 @@ where
Self {
title: ui.title(),
ui: Rc::new(RefCell::new(Box::new(ui))),
+ vdom: Rc::new(RefCell::new(None)),
}
}
@@ -192,11 +184,35 @@ where
for future in command.futures() {
let mut instance = self.clone();
- let future = future.map(move |message| instance.update(message));
+
+ let future = future.map(move |message| {
+ instance.update(message);
+
+ if let Some(ref vdom) = *instance.vdom.borrow() {
+ vdom.schedule_render();
+ }
+ });
wasm_bindgen_futures::spawn_local(future);
}
}
+
+ fn run(mut self, command: Command<Message>) {
+ let window = web_sys::window().unwrap();
+
+ let document = window.document().unwrap();
+ document.set_title(&self.title);
+
+ let body = document.body().unwrap();
+
+ let weak = self.vdom.clone();
+ self.spawn(command);
+
+ let vdom = dodrio::Vdom::new(&body, self);
+ *weak.borrow_mut() = Some(vdom.weak());
+
+ vdom.forget();
+ }
}
impl<Message> dodrio::Render for Instance<Message>