Skip to content

Commit

Permalink
Detach worker registration from a main initialization routine
Browse files Browse the repository at this point in the history
Workers become more compact with simple type specification.
  • Loading branch information
therustmonk committed Jun 9, 2018
1 parent 8339292 commit 07c6aec
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 233 deletions.
7 changes: 7 additions & 0 deletions examples/multi_thread/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### multi_thread

You should compile a worker which have to be spawned in a separate thread:

```sh
cargo web build --bin native_worker --target wasm32-unknown-unknown
```
14 changes: 3 additions & 11 deletions examples/multi_thread/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ extern crate multi_thread;

use yew::prelude::*;
use multi_thread::Model;
use multi_thread::worker;

fn main() {
web_logger::init();
match yew::initialize() {
Ambit::Application => {
App::<Model>::new().mount_to_body();
yew::run_loop();
}
Ambit::Agent => {
worker::Worker::register();
yew::run_agent();
}
}
yew::initialize();
App::<Model>::new().mount_to_body();
yew::run_loop();
}
14 changes: 14 additions & 0 deletions examples/multi_thread/src/bin/native_worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

extern crate web_logger;
extern crate yew;
extern crate multi_thread;

use yew::prelude::*;
use multi_thread::native_worker;

fn main() {
web_logger::init();
yew::initialize();
native_worker::Worker::register();
yew::run_loop();
}
8 changes: 4 additions & 4 deletions examples/multi_thread/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ extern crate serde_derive;
#[macro_use]
extern crate yew;

pub mod worker;
pub mod native_worker;
pub mod job;
pub mod context;

use yew::prelude::*;

pub struct Model {
worker: Box<Bridge<worker::Worker>>,
worker: Box<Bridge<native_worker::Worker>>,
job: Box<Bridge<job::Worker>>,
context: Box<Bridge<context::Worker>>,
context_2: Box<Bridge<context::Worker>>,
Expand All @@ -31,7 +31,7 @@ impl Component for Model {

fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
let callback = link.send_back(|_| Msg::DataReceived);
let worker = worker::Worker::bridge(callback);
let worker = native_worker::Worker::bridge(callback);

let callback = link.send_back(|_| Msg::DataReceived);
let job = job::Worker::bridge(callback);
Expand All @@ -48,7 +48,7 @@ impl Component for Model {
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::SendToWorker => {
self.worker.send(worker::Request::GetDataFromServer);
self.worker.send(native_worker::Request::GetDataFromServer);
}
Msg::SendToJob => {
self.job.send(job::Request::GetDataFromServer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ impl Agent for Worker {
}
}
}

fn name_of_resource() -> &'static str { "bin/native_worker.js" }
}
Loading

0 comments on commit 07c6aec

Please sign in to comment.