Skip to content

Commit

Permalink
Port Oneshot Agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Aug 10, 2023
1 parent b1644c2 commit a19a1fa
Show file tree
Hide file tree
Showing 18 changed files with 376 additions and 504 deletions.
71 changes: 69 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/web_worker_fib/src/agent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use js_sys::Uint8Array;
use serde::{Deserialize, Serialize};
use wasm_bindgen::JsValue;
use yew_agent::{task, Codec};
use yew_agent::{oneshot, Codec};

pub struct Postcard;

Expand All @@ -23,8 +23,8 @@ impl Codec for Postcard {
}
}

#[task(FibonacciTask)]
pub async fn calculate_fibonacci(n: u32) -> u32 {
#[oneshot]
pub async fn FibonacciTask(n: u32) -> u32 {
fn fib(n: u32) -> u32 {
if n <= 1 {
1
Expand Down
8 changes: 4 additions & 4 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::task::{use_task, TaskProvider};
use yew_agent::oneshot::{use_bridge_oneshot, 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_task::<FibonacciTask>();
let fib_task = use_bridge_oneshot::<FibonacciTask>();

let clicker_value = use_state_eq(|| 0);

Expand Down Expand Up @@ -73,8 +73,8 @@ fn Main() -> Html {
#[function_component]
pub fn App() -> Html {
html! {
<TaskProvider<FibonacciTask, Postcard> path="/worker.js">
<OneshotProvider<FibonacciTask, Postcard> path="/worker.js">
<Main />
</TaskProvider<FibonacciTask, Postcard>>
</OneshotProvider<FibonacciTask, Postcard>>
}
}
2 changes: 1 addition & 1 deletion packages/yew-agent-macro/src/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn oneshot_impl(name: AgentName, mut agent_fn: AgentFn<OneshotFn>) -> syn::R
} else {
quote! { #fn_name #fn_generics (#in_ident) }
};
let crate_name = quote! { ::yew_agent };
let crate_name = quote! { yew_agent };

let quoted = quote! {
#(#struct_attrs)*
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version = "1.60.0"

[dependencies]
yew = { version = "0.20.0", path = "../yew" }
gloo-worker = { version = "0.2", features = ["futures"] }
gloo-worker = { version = "0.3", features = ["futures"] }
wasm-bindgen = "0.2"
serde = { version = "1", features = ["derive"] }
futures = "0.3"
Expand Down
31 changes: 15 additions & 16 deletions packages/yew-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! There're a couple kinds of agents:
//!
//! #### Task
//! #### Oneshot
//!
//! A kind of agent that for each input, a single output is expected.
//!
Expand Down Expand Up @@ -58,10 +58,9 @@
//!
//! #### Task
//!
//! See: [`use_task`](task::use_task), [`use_memorized_task`](task::use_memorized_task)
//! See: [`use_task`](task::use_task)
//!
//! Unlike other agents, tasks provides a `use_task` hook for mutation-like usage and a
//! `use_memoized_task` hook for query like usage.
//! Unlike other agents, tasks provides a `use_task` hook to execute tasks on demand.
#![deny(
clippy::all,
Expand All @@ -74,16 +73,16 @@

extern crate self as yew_agent;

pub mod reactor;
pub mod task;
// pub mod reactor;
pub mod oneshot;
pub mod worker;

#[doc(inline)]
pub use gloo::worker::{Bincode, Codec, Registrable};
pub use gloo_worker::{Bincode, Codec, Registrable, Spawnable};
/// A procedural macro to create oneshot agents.
pub use yew_agent_macro::oneshot;
/// A procedural macro to create reactor agents.
pub use yew_agent_macro::reactor;
/// A procedural macro to create task agents.
pub use yew_agent_macro::task;

mod reach;
pub mod scope_ext;
Expand All @@ -99,16 +98,16 @@ pub mod prelude {
//! Prelude module to be imported when working with `yew-agent`.
//!
//! This module re-exports the frequently used types from the crate.
pub use crate::oneshot::{use_bridge_oneshot, UseBridgeOneshotHandle};
pub use crate::reach::Reach;
pub use crate::reactor::{
use_reactor_bridge, use_reactor_subscription, ReactorOutput, UseReactorBridgeHandle,
UseReactorSubscriptionHandle,
};
pub use crate::scope_ext::{AgentScopeExt, ReactorBridgeHandle, WorkerBridgeHandle};
pub use crate::task::{use_memorized_task, use_task, UseTaskHandle};
// pub use crate::reactor::{
// use_reactor_bridge, use_reactor_subscription, ReactorOutput, UseReactorBridgeHandle,
// UseReactorSubscriptionHandle,
// };
pub use crate::scope_ext::{AgentScopeExt, /* ReactorBridgeHandle, */ WorkerBridgeHandle};
pub use crate::worker::{
use_worker_bridge, use_worker_subscription, UseWorkerBridgeHandle,
UseWorkerSubscriptionHandle,
};
pub use crate::Registrable;
pub use crate::{Registrable, Spawnable};
}
54 changes: 54 additions & 0 deletions packages/yew-agent/src/oneshot/hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use yew::prelude::*;

use super::provider::OneshotProviderState;
use super::Oneshot;

/// Handle for [use_bridge_oneshot]
#[derive(Debug)]
pub struct UseBridgeOneshotHandle<T>
where
T: Oneshot + 'static,
{
state: OneshotProviderState<T>,
}

impl<T> UseBridgeOneshotHandle<T>
where
T: Oneshot + 'static,
{
/// Runs an oneshot agent.
pub async fn run(&self, input: T::Input) -> T::Output {
self.state.create_bridge().run(input).await
}
}

impl<T> Clone for UseBridgeOneshotHandle<T>
where
T: Oneshot + 'static,
{
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
}
}
}

impl<T> PartialEq for UseBridgeOneshotHandle<T>
where
T: Oneshot,
{
fn eq(&self, rhs: &Self) -> bool {
self.state == rhs.state
}
}

/// A hook to bridge to an oneshot agent.
#[hook]
pub fn use_bridge_oneshot<T>() -> UseBridgeOneshotHandle<T>
where
T: Oneshot + 'static,
{
let state = use_context::<OneshotProviderState<T>>().expect("failed to find worker context");

UseBridgeOneshotHandle { state }
}
10 changes: 10 additions & 0 deletions packages/yew-agent/src/oneshot/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! This module provides task agent implementation.
mod hooks;
mod provider;

#[doc(inline)]
pub use gloo_worker::oneshot::{Oneshot, OneshotBridge, OneshotRegistrar, OneshotSpawner};
pub use hooks::{use_bridge_oneshot, UseBridgeOneshotHandle};
pub use provider::OneshotProvider;
pub(crate) use provider::OneshotProviderState;
Loading

0 comments on commit a19a1fa

Please sign in to comment.