Skip to content

Commit

Permalink
Remove unneeded checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Aug 20, 2023
1 parent 49785fe commit fdad38d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 32 deletions.
3 changes: 2 additions & 1 deletion examples/web_worker_fib/src/agent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use js_sys::Uint8Array;
use serde::{Deserialize, Serialize};
use wasm_bindgen::JsValue;
use yew_agent::{oneshot, Codec};
use yew_agent::prelude::*;
use yew_agent::Codec;

pub struct Postcard;

Expand Down
4 changes: 2 additions & 2 deletions examples/web_worker_fib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ pub mod agent;
use web_sys::HtmlInputElement;
use yew::platform::spawn_local;
use yew::prelude::*;
use yew_agent::oneshot::{use_bridge_oneshot, OneshotProvider};
use yew_agent::oneshot::{use_oneshot_bridge, OneshotProvider};

use crate::agent::{FibonacciTask, Postcard};

#[function_component]
fn Main() -> Html {
let input_value = use_state_eq(|| 44);
let output = use_state(|| "Try out some fibonacci calculations!".to_string());
let fib_task = use_bridge_oneshot::<FibonacciTask>();
let fib_task = use_oneshot_bridge::<FibonacciTask>();

let clicker_value = use_state_eq(|| 0);

Expand Down
45 changes: 16 additions & 29 deletions packages/yew-agent/src/reactor/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,11 @@ where
let worker_state = use_context::<ReactorProviderState<R>>()
.expect_throw("cannot find a provider for current agent.");

let on_output = {
let current_bridge_id = ctr.inner;
Rc::new(move |event: ReactorEvent<R>, event_bridge_id: usize| {
// If a new bridge is created, then we discard messages from the previous bridge.
if current_bridge_id != event_bridge_id {
return;
}

on_output(event);
})
};
let on_output = Rc::new(on_output);

let on_output_ref = {
let on_output_clone = on_output.clone();
use_mut_ref(move || on_output_clone)
let on_output = on_output.clone();
use_mut_ref(move || on_output)
};

// Refresh the callback on every render.
Expand All @@ -145,26 +135,23 @@ where
*on_output_ref = on_output;
}

let tx = {
use_memo((worker_state, ctr.inner), |(state, ctr)| {
let bridge = state.create_bridge();
let ctr = *ctr;
let tx = use_memo((worker_state, ctr.inner), |(state, _ctr)| {
let bridge = state.create_bridge();

let (tx, mut rx) = bridge.split();

spawn_local(async move {
while let Some(m) = rx.next().await {
let on_output = on_output_ref.borrow().clone();
on_output(ReactorEvent::<R>::Output(m), ctr);
}
let (tx, mut rx) = bridge.split();

spawn_local(async move {
while let Some(m) = rx.next().await {
let on_output = on_output_ref.borrow().clone();
on_output(ReactorEvent::<R>::Finished, ctr);
});
on_output(ReactorEvent::<R>::Output(m));
}

RwLock::new(tx)
})
};
let on_output = on_output_ref.borrow().clone();
on_output(ReactorEvent::<R>::Finished);
});

RwLock::new(tx)
});

UseReactorBridgeHandle {
tx: tx.clone(),
Expand Down

0 comments on commit fdad38d

Please sign in to comment.