Skip to content

Commit

Permalink
Migrate reactor to gloo-worker.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Aug 19, 2023
1 parent a19a1fa commit 4465203
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 451 deletions.
2 changes: 1 addition & 1 deletion packages/yew-agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
readme = "../../README.md"
description = "Agents for Yew"
license = "MIT OR Apache-2.0"
rust-version = "1.60.0"
rust-version = "1.64.0"

[dependencies]
yew = { version = "0.20.0", path = "../yew" }
Expand Down
20 changes: 9 additions & 11 deletions packages/yew-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! #### Oneshot
//!
//! A kind of agent that for each input, a single output is expected.
//! A kind of agent that for each input, a single output is returned.
//!
//! #### Reactor
//!
Expand Down Expand Up @@ -73,22 +73,20 @@

extern crate self as yew_agent;

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

#[doc(inline)]
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;

mod reach;
pub mod scope_ext;

pub use reach::Reach;

mod utils;

#[doc(hidden)]
pub mod __vendored {
pub use futures;
Expand All @@ -98,12 +96,12 @@ 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::oneshot::{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::reactor::{
use_reactor_bridge, use_reactor_subscription, ReactorEvent, UseReactorBridgeHandle,
UseReactorSubscriptionHandle,
};
pub use crate::scope_ext::{AgentScopeExt, /* ReactorBridgeHandle, */ WorkerBridgeHandle};
pub use crate::worker::{
use_worker_bridge, use_worker_subscription, UseWorkerBridgeHandle,
Expand Down
2 changes: 2 additions & 0 deletions packages/yew-agent/src/oneshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pub use gloo_worker::oneshot::{Oneshot, OneshotBridge, OneshotRegistrar, Oneshot
pub use hooks::{use_bridge_oneshot, UseBridgeOneshotHandle};
pub use provider::OneshotProvider;
pub(crate) use provider::OneshotProviderState;
/// A procedural macro to create oneshot agents.
pub use yew_agent_macro::oneshot;
37 changes: 16 additions & 21 deletions packages/yew-agent/src/oneshot/provider.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use core::fmt;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::atomic::{AtomicUsize, Ordering};

use gloo_worker::oneshot::OneshotSpawner;
use serde::{Deserialize, Serialize};
use yew::prelude::*;

use super::{Oneshot, OneshotBridge};
use super::{Oneshot, OneshotBridge, OneshotSpawner};
use crate::utils::get_next_id;
use crate::worker::WorkerProviderProps;
use crate::{Bincode, Codec, Reach};

pub(crate) struct OneshotProviderState<T>
where
T: Oneshot + 'static,
{
ctr: usize,
id: usize,
spawn_bridge_fn: Rc<dyn Fn() -> OneshotBridge<T>>,
reach: Reach,
held_bridge: Rc<RefCell<Option<OneshotBridge<T>>>>,
Expand Down Expand Up @@ -59,31 +58,29 @@ where
}
}

impl<W> Clone for OneshotProviderState<W>
impl<T> Clone for OneshotProviderState<T>
where
W: Oneshot,
T: Oneshot,
{
fn clone(&self) -> Self {
Self {
ctr: self.ctr,
id: self.id,
spawn_bridge_fn: self.spawn_bridge_fn.clone(),
reach: self.reach,
held_bridge: self.held_bridge.clone(),
}
}
}

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

static CTR: AtomicUsize = AtomicUsize::new(0);

/// A Oneshot Agent Provider.
///
/// This component provides its children access to an oneshot agent.
Expand All @@ -95,25 +92,23 @@ where
T::Output: Serialize + for<'de> Deserialize<'de> + 'static,
CODEC: Codec + 'static,
{
// Creates a spawning function so CODEC is can be erased from contexts.
let spawn_bridge_fn: Rc<dyn Fn() -> OneshotBridge<T>> = {
let path = props.path.clone();
Rc::new(move || OneshotSpawner::<T>::new().encoding::<CODEC>().spawn(&path))
};

let WorkerProviderProps {
children,
path,
lazy,
reach,
} = props.clone();

// Creates a spawning function so CODEC is can be erased from contexts.
let spawn_bridge_fn: Rc<dyn Fn() -> OneshotBridge<T>> = {
let path = path.clone();
Rc::new(move || OneshotSpawner::<T>::new().encoding::<CODEC>().spawn(&path))
};

let state = {
use_memo((path, lazy, reach), move |(_path, lazy, reach)| {
let ctr = CTR.fetch_add(1, Ordering::SeqCst);

let state = OneshotProviderState::<T> {
ctr,
id: get_next_id(),
spawn_bridge_fn,
reach: *reach,
held_bridge: Rc::default(),
Expand Down
Loading

0 comments on commit 4465203

Please sign in to comment.