Skip to content

Commit

Permalink
Add worker
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasAlaif committed Nov 28, 2023
1 parent a73a1c3 commit 09d15c5
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 13 deletions.
3 changes: 2 additions & 1 deletion axiom-profiler-GUI/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "yew-testing"
name = "axiom-profiler-gui"
version = "0.1.0"
edition = "2021"

Expand Down Expand Up @@ -32,3 +32,4 @@ gloo-console = "0.3.0"
num-format = "0.4.4"
material-yew = "0.3.0"
paste = "1.0.14"
yew-agent = "0.2.0"
2 changes: 2 additions & 0 deletions axiom-profiler-GUI/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<title>Axiom Profiler</title>
<link data-trunk rel="icon" type="image/x-icon" href="/assets/favicon.ico">
<link data-trunk rel="css" href="/style.css">
<link data-trunk rel="rust" href="Cargo.toml" data-bin="app" data-type="main" />
<link data-trunk rel="rust" href="Cargo.toml" data-bin="worker" data-type="worker" />
</head>
<body>

Expand Down
6 changes: 6 additions & 0 deletions axiom-profiler-GUI/src/bin/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
wasm_logger::init(wasm_logger::Config::default());
log::debug!("App is starting");
// yew::Renderer::<Main>::new().render();
yew::Renderer::<axiom_profiler_gui::App>::new().render();
}
6 changes: 6 additions & 0 deletions axiom-profiler-GUI/src/bin/worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use yew_agent::PrivateWorker;
use axiom_profiler_gui::results::worker::Worker;

fn main() {
Worker::register();
}
11 changes: 2 additions & 9 deletions axiom-profiler-GUI/src/main.rs → axiom-profiler-GUI/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use results::svg_result::SVGResult;
// mod svg_result;
// mod toggle_switch;
// mod graph_container;
mod results;
pub mod results;
mod utils;
// mod select_dropdown;
pub enum Msg {
Expand Down Expand Up @@ -152,7 +152,7 @@ impl ParserData {
}

#[function_component(App)]
fn app() -> Html {
pub fn app() -> Html {
html! {
<>
// <div>
Expand Down Expand Up @@ -199,10 +199,3 @@ enum Route {
// </BrowserRouter>
// }
// }

fn main() {
wasm_logger::init(wasm_logger::Config::default());
log::debug!("App is starting");
// yew::Renderer::<Main>::new().render();
yew::Renderer::<App>::new().render();
}
1 change: 1 addition & 0 deletions axiom-profiler-GUI/src/results/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod svg_result;
pub mod filters;
pub mod graph;
pub mod worker;

34 changes: 32 additions & 2 deletions axiom-profiler-GUI/src/results/svg_result.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use self::colors::HSVColour;
use super::filters::{
use super::{filters::{
filter_chain::{FilterChain, Msg as FilterChainMsg},
graph_filters::Filter,
};
}, worker::Worker};
use super::graph::graph_container::GraphContainer;
use material_yew::WeakComponentLink;
use num_format::{Locale, ToFormattedString};
Expand Down Expand Up @@ -33,6 +33,7 @@ pub enum Msg {
ApplyFilter(Filter),
ResetGraph,
GetUserPermission,
WorkerOutput(super::worker::WorkerOutput),
}

pub struct UserPermission {
Expand All @@ -59,6 +60,7 @@ pub struct SVGResult {
selected_insts: FxHashMap<NodeIndex, InstInfo>,
filter_chain_link: WeakComponentLink<FilterChain>,
on_node_select: Callback<usize>,
worker: Option<Box<dyn yew_agent::Bridge<Worker>>>,
}

#[derive(Properties, PartialEq)]
Expand All @@ -83,11 +85,15 @@ impl Component for SVGResult {
selected_insts: FxHashMap::default(),
filter_chain_link: WeakComponentLink::default(),
on_node_select: ctx.link().callback(Msg::UpdateSelectedNodes),
worker: Some(Self::create_worker(ctx.link().clone())),
}
}

fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::WorkerOutput(out) => {
false
}
Msg::ApplyFilter(filter) => {
log::debug!("Applying filter {}", filter);
filter.apply(&mut self.inst_graph);
Expand Down Expand Up @@ -243,6 +249,30 @@ impl Component for SVGResult {
}
}

impl SVGResult {
/// Deletes the old worker with its queue of messages and creates a new one.
/// Any enqueued work will still continue to run (there is no way to cancel this
/// at the moment, see https://github.com/rustwasm/gloo/issues/408) but will not
/// send a `WorkerOutput` message on completion.
pub fn reset_worker(&mut self, link: yew::html::Scope<Self>) {
// The old worker is dropped when overwritten here. Not sure we need the option?
self.worker = Some(Self::create_worker(link));
}
/// Sends an input to the worker to process.
pub fn send_worker_input(&mut self, input: super::worker::WorkerInput) {
self.worker.as_mut().unwrap().send(input);
}

/// Used internally.
fn create_worker(link: yew::html::Scope<Self>) -> Box<dyn yew_agent::Bridge<Worker>> {
use yew_agent::Bridged;
let cb = std::rc::Rc::new(
move |e| link.send_message(Msg::WorkerOutput(e))
);
Worker::bridge(cb)
}
}

struct QuantIdxToColourMap {
total_nr_of_quants: usize,
coprime: NonZeroUsize,
Expand Down
59 changes: 59 additions & 0 deletions axiom-profiler-GUI/src/results/worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use serde::{Deserialize, Serialize};
use yew_agent::{HandlerId, Private, WorkerLink};

pub struct Worker {
link: WorkerLink<Self>,
}

#[derive(Serialize, Deserialize)]
pub struct WorkerInput {
pub n: u32,
}

#[derive(Serialize, Deserialize)]
pub struct WorkerOutput {
pub input: u32,
pub value: u32,
}

impl yew_agent::Worker for Worker {
type Message = ();
type Input = WorkerInput;
type Output = WorkerOutput;
type Reach = Private<Self>;

fn create(link: WorkerLink<Self>) -> Self {
Self { link }
}

fn update(&mut self, _msg: Self::Message) {
// no messaging
}

fn handle_input(&mut self, msg: Self::Input, id: HandlerId) {
// this runs in a web worker
// and does not block the main
// browser thread!

let n = msg.n;

fn fib(n: u32) -> u32 {
if n <= 1 {
1
} else {
fib(n - 1) + fib(n - 2)
}
}

let output = Self::Output { input: n, value: fib(n) };
self.link.respond(id, output);
}

fn name_of_resource() -> &'static str {
"worker.js"
}

fn resource_path_is_relative() -> bool {
true
}
}
6 changes: 5 additions & 1 deletion axiom-profiler-GUI/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
flex: 1;
}

.node {
cursor: pointer;
}

.node:hover {
opacity: 0.6
}

.edge:hover * {
opacity: 0.4
}
}

0 comments on commit 09d15c5

Please sign in to comment.