Spawn Command futures in iced_web

This commit is contained in:
Héctor Ramón Jiménez 2019-11-24 11:25:14 +01:00
parent 5b8f6948bb
commit 9f3abe9202
6 changed files with 36 additions and 18 deletions

View file

@ -83,7 +83,7 @@ pub trait Application: Sized {
/// The type of __messages__ your [`Application`] will produce. /// The type of __messages__ your [`Application`] will produce.
/// ///
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
type Message: std::fmt::Debug + Send; type Message: std::fmt::Debug + Send + Clone;
/// Initializes the [`Application`]. /// Initializes the [`Application`].
/// ///

View file

@ -81,7 +81,7 @@ pub trait Sandbox {
/// The type of __messages__ your [`Sandbox`] will produce. /// The type of __messages__ your [`Sandbox`] will produce.
/// ///
/// [`Sandbox`]: trait.Sandbox.html /// [`Sandbox`]: trait.Sandbox.html
type Message: std::fmt::Debug + Send; type Message: std::fmt::Debug + Send + Clone;
/// Initializes the [`Sandbox`]. /// Initializes the [`Sandbox`].
/// ///

View file

@ -18,6 +18,8 @@ maintenance = { status = "actively-developed" }
iced_core = { version = "0.1.0-alpha", path = "../core", features = ["command"] } iced_core = { version = "0.1.0-alpha", path = "../core", features = ["command"] }
dodrio = "0.1.0" dodrio = "0.1.0"
wasm-bindgen = "0.2.51" wasm-bindgen = "0.2.51"
wasm-bindgen-futures = "0.4"
futures = "0.3"
[dependencies.web-sys] [dependencies.web-sys]
version = "0.3.27" version = "0.3.27"

View file

@ -15,7 +15,7 @@ pub struct Bus<Message> {
impl<Message> Bus<Message> impl<Message> Bus<Message>
where where
Message: 'static, Message: 'static + Clone,
{ {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {

View file

@ -38,8 +38,8 @@ impl<'a, Message> Element<'a, Message> {
/// [`Element`]: struct.Element.html /// [`Element`]: struct.Element.html
pub fn map<F, B>(self, f: F) -> Element<'a, B> pub fn map<F, B>(self, f: F) -> Element<'a, B>
where where
Message: 'static, Message: 'static + Clone,
B: 'static, B: 'static + Clone,
F: 'static + Fn(Message) -> B, F: 'static + Fn(Message) -> B,
{ {
Element { Element {
@ -82,8 +82,8 @@ impl<'a, A, B> Map<'a, A, B> {
impl<'a, A, B> Widget<B> for Map<'a, A, B> impl<'a, A, B> Widget<B> for Map<'a, A, B>
where where
A: 'static, A: 'static + Clone,
B: 'static, B: 'static + Clone,
{ {
fn node<'b>( fn node<'b>(
&self, &self,

View file

@ -57,7 +57,7 @@
#![deny(unsafe_code)] #![deny(unsafe_code)]
#![deny(rust_2018_idioms)] #![deny(rust_2018_idioms)]
use dodrio::bumpalo; use dodrio::bumpalo;
use std::cell::RefCell; use std::{cell::RefCell, rc::Rc};
mod bus; mod bus;
mod element; mod element;
@ -87,7 +87,7 @@ pub trait Application {
/// The type of __messages__ your [`Application`] will produce. /// The type of __messages__ your [`Application`] will produce.
/// ///
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
type Message; type Message: Clone;
/// Initializes the [`Application`]. /// Initializes the [`Application`].
/// ///
@ -137,10 +137,10 @@ pub trait Application {
where where
Self: 'static + Sized, Self: 'static + Sized,
{ {
// TODO: Spawn command let (app, command) = Self::new();
let (app, _command) = Self::new(); let mut instance = Instance::new(app);
let instance = Instance::new(app); instance.spawn(command);
let window = web_sys::window().unwrap(); let window = web_sys::window().unwrap();
let document = window.document().unwrap(); let document = window.document().unwrap();
@ -151,26 +151,42 @@ pub trait Application {
} }
} }
#[derive(Clone)]
struct Instance<Message> { struct Instance<Message> {
ui: RefCell<Box<dyn Application<Message = Message>>>, ui: Rc<RefCell<Box<dyn Application<Message = Message>>>>,
} }
impl<Message> Instance<Message> { impl<Message> Instance<Message>
where
Message: 'static + Clone,
{
fn new(ui: impl Application<Message = Message> + 'static) -> Self { fn new(ui: impl Application<Message = Message> + 'static) -> Self {
Self { Self {
ui: RefCell::new(Box::new(ui)), ui: Rc::new(RefCell::new(Box::new(ui))),
} }
} }
fn update(&mut self, message: Message) { fn update(&mut self, message: Message) {
// TODO: Spawn command let command = self.ui.borrow_mut().update(message);
let _command = self.ui.borrow_mut().update(message);
self.spawn(command);
}
fn spawn(&mut self, command: Command<Message>) {
use futures::FutureExt;
for future in command.futures() {
let mut instance = self.clone();
let future = future.map(move |message| instance.update(message));
wasm_bindgen_futures::spawn_local(future);
}
} }
} }
impl<Message> dodrio::Render for Instance<Message> impl<Message> dodrio::Render for Instance<Message>
where where
Message: 'static, Message: 'static + Clone,
{ {
fn render<'a, 'bump>( fn render<'a, 'bump>(
&'a self, &'a self,