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.
///
/// [`Application`]: trait.Application.html
type Message: std::fmt::Debug + Send;
type Message: std::fmt::Debug + Send + Clone;
/// Initializes the [`Application`].
///

View File

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

View File

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

View File

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

View File

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

View File

@ -57,7 +57,7 @@
#![deny(unsafe_code)]
#![deny(rust_2018_idioms)]
use dodrio::bumpalo;
use std::cell::RefCell;
use std::{cell::RefCell, rc::Rc};
mod bus;
mod element;
@ -87,7 +87,7 @@ pub trait Application {
/// The type of __messages__ your [`Application`] will produce.
///
/// [`Application`]: trait.Application.html
type Message;
type Message: Clone;
/// Initializes the [`Application`].
///
@ -137,10 +137,10 @@ pub trait Application {
where
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 document = window.document().unwrap();
@ -151,26 +151,42 @@ pub trait Application {
}
}
#[derive(Clone)]
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 {
Self {
ui: RefCell::new(Box::new(ui)),
ui: Rc::new(RefCell::new(Box::new(ui))),
}
}
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>
where
Message: 'static,
Message: 'static + Clone,
{
fn render<'a, 'bump>(
&'a self,