diff options
author | 2020-03-30 18:00:15 +0200 | |
---|---|---|
committer | 2020-03-30 18:10:15 +0200 | |
commit | c4c5216e3b69d732b0518d510f95675a4ba7010b (patch) | |
tree | 25e14890319bbab46aeb5133ef1c694959b9f694 /web | |
parent | 6e9ab1cd6f5358d323040379e3aadbed2cc4f7f8 (diff) | |
download | iced-c4c5216e3b69d732b0518d510f95675a4ba7010b.tar.gz iced-c4c5216e3b69d732b0518d510f95675a4ba7010b.tar.bz2 iced-c4c5216e3b69d732b0518d510f95675a4ba7010b.zip |
Allow passing external state to `Application::new`
Diffstat (limited to 'web')
-rw-r--r-- | web/src/lib.rs | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/web/src/lib.rs b/web/src/lib.rs index 1de00545..bc46c541 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -94,11 +94,6 @@ pub use executor::Executor; /// An [`Application`](trait.Application.html) can execute asynchronous actions /// by returning a [`Command`](struct.Command.html) in some of its methods. pub trait Application { - /// The type of __messages__ your [`Application`] will produce. - /// - /// [`Application`]: trait.Application.html - type Message: Send; - /// The [`Executor`] that will run commands and subscriptions. /// /// The [`executor::WasmBindgen`] can be a good choice for the Web. @@ -107,6 +102,16 @@ pub trait Application { /// [`executor::Default`]: executor/struct.Default.html type Executor: Executor; + /// The type of __messages__ your [`Application`] will produce. + /// + /// [`Application`]: trait.Application.html + type Message: Send; + + /// The data needed to initialize your [`Application`]. + /// + /// [`Application`]: trait.Application.html + type Flags; + /// Initializes the [`Application`]. /// /// Here is where you should return the initial state of your app. @@ -117,7 +122,7 @@ pub trait Application { /// request, etc. /// /// [`Application`]: trait.Application.html - fn new() -> (Self, Command<Self::Message>) + fn new(flags: Self::Flags) -> (Self, Command<Self::Message>) where Self: Sized; @@ -165,21 +170,16 @@ pub trait Application { /// Runs the [`Application`]. /// /// [`Application`]: trait.Application.html - fn run() + fn run(flags: Self::Flags) where Self: 'static + Sized, { use futures::stream::StreamExt; - let (app, command) = Self::new(); - let window = web_sys::window().unwrap(); let document = window.document().unwrap(); let body = document.body().unwrap(); - let mut title = app.title(); - document.set_title(&title); - let (sender, receiver) = iced_futures::futures::channel::mpsc::unbounded(); @@ -187,6 +187,12 @@ pub trait Application { Self::Executor::new().expect("Create executor"), sender.clone(), ); + + let (app, command) = Self::new(flags); + + let mut title = app.title(); + document.set_title(&title); + runtime.spawn(command); let application = Rc::new(RefCell::new(app)); |