-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "yew-worker-prime" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
yew-agent = { path = "../../packages/yew-agent" } | ||
yew = { path = "../../packages/yew", features = ["csr"] } | ||
futures = "0.3.25" | ||
primes = "0.3.0" | ||
serde = { version = "1.0.147", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Web Worker Prime | ||
|
||
[![Demo](https://img.shields.io/website?label=demo&url=https%3A%2F%2Fexamples.yew.rs%2Fweb_worker_prime)](https://examples.yew.rs/web_worker_prime) | ||
|
||
Calculate primes until stop button is pressed, without blocking the main thread. | ||
|
||
## Concepts | ||
|
||
The example illustrates how to use reactor agents to offload CPU bound tasks to a worker thread in a Yew application. | ||
|
||
## Running | ||
|
||
Run this application with the trunk development server: | ||
|
||
```bash | ||
trunk serve --open | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Yew • Web Worker Prime</title> | ||
|
||
<link data-trunk rel="rust" href="Cargo.toml" data-bin="app" data-type="main" data-weak-refs /> | ||
<link data-trunk rel="rust" href="Cargo.toml" data-bin="worker" data-type="worker" data-weak-refs /> | ||
</head> | ||
|
||
<body> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::time::Duration; | ||
|
||
use futures::sink::SinkExt; | ||
use futures::{FutureExt, StreamExt}; | ||
use serde::{Deserialize, Serialize}; | ||
use yew::platform::time::sleep; | ||
use yew_agent::prelude::*; | ||
|
||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
pub enum ControlSignal { | ||
Start, | ||
Stop, | ||
} | ||
|
||
#[reactor] | ||
pub async fn Prime(mut scope: ReactorScope<ControlSignal, u64>) { | ||
while let Some(m) = scope.next().await { | ||
if m == ControlSignal::Start { | ||
'inner: for i in 1.. { | ||
// This is not the most efficient way to calculate prime, | ||
// but this example is here to demonstrate how primes can be | ||
// sent to the application in an ascending order. | ||
if primes::is_prime(i) { | ||
scope.send(i).await.unwrap(); | ||
} | ||
|
||
futures::select! { | ||
m = scope.next() => { | ||
if m == Some(ControlSignal::Stop) { | ||
break 'inner; | ||
} | ||
}, | ||
_ = sleep(Duration::from_millis(100)).fuse() => {}, | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
yew::Renderer::<yew_worker_prime::App>::new().render(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use yew_agent::Registrable; | ||
use yew_worker_prime::agent::Prime; | ||
|
||
fn main() { | ||
Prime::registrar().register(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
pub mod agent; | ||
use agent::{ControlSignal, Prime}; | ||
use yew::prelude::*; | ||
use yew_agent::reactor::{use_reactor_subscription, ReactorProvider}; | ||
|
||
#[function_component] | ||
fn Main() -> Html { | ||
let prime_sub = use_reactor_subscription::<Prime>(); | ||
let started = use_state_eq(|| false); | ||
let skip_len = use_state_eq(|| 0); | ||
|
||
let result_s = prime_sub | ||
.iter() | ||
// Skip results in previous runs. | ||
.skip(*skip_len) | ||
.fold("".to_string(), |mut output, item| { | ||
if !output.is_empty() { | ||
output.push_str(", "); | ||
} | ||
|
||
output.push_str(&item.to_string()); | ||
|
||
output | ||
}); | ||
|
||
let start_prime_calc = use_callback( | ||
(prime_sub.clone(), started.setter(), skip_len.setter()), | ||
|_input, (prime_sub, started_setter, skip_len)| { | ||
skip_len.set(prime_sub.len()); | ||
prime_sub.send(ControlSignal::Start); | ||
started_setter.set(true); | ||
}, | ||
); | ||
|
||
let stop_prime_calc = use_callback( | ||
(prime_sub, started.setter()), | ||
|_input, (prime_sub, started_setter)| { | ||
prime_sub.send(ControlSignal::Stop); | ||
started_setter.set(false); | ||
}, | ||
); | ||
|
||
html! { | ||
<> | ||
<h1>{"Find Prime"}</h1> | ||
<p>{"This page demonstrates how to calculate prime in a web worker."}</p> | ||
if *started { | ||
<button onclick={stop_prime_calc}>{"Stop"}</button> | ||
} else { | ||
<button onclick={start_prime_calc}>{"Start"}</button> | ||
} | ||
<div id="result">{result_s}</div> | ||
</> | ||
} | ||
} | ||
|
||
#[function_component] | ||
pub fn App() -> Html { | ||
html! { | ||
<ReactorProvider<Prime> path="/worker.js"> | ||
<Main /> | ||
</ReactorProvider<Prime>> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters