Skip to content

Commit

Permalink
use_bridge hook for agents (#2125)
Browse files Browse the repository at this point in the history
* Add Redirect Comp.

* Fix router behaviour.

* Fix output.

* Fix pr-flow.

* Remove Redirect.

* Readd 77b46bf.

* Add use_bridge hook.

* Add documentation.
  • Loading branch information
futursolo authored Nov 12, 2021
1 parent 39886d4 commit fb9c398
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
56 changes: 56 additions & 0 deletions packages/yew-agent/src/hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::cell::RefCell;
use std::rc::Rc;

use crate::*;
use yew::prelude::*;

/// State handle for [`use_bridge`] hook
pub struct UseBridgeHandle<T>
where
T: Bridged,
{
inner: Rc<RefCell<Box<dyn Bridge<T>>>>,
}

impl<T> UseBridgeHandle<T>
where
T: Bridged,
{
/// Send a message to an agent.
pub fn send(&self, msg: T::Input) {
let mut bridge = self.inner.borrow_mut();
bridge.send(msg);
}
}

/// A hook to bridge to an Agent.
///
/// This hooks will only bridge the agent once over the entire component lifecycle.
///
/// Takes a callback as the only argument. The callback will be updated on every render to make
/// sure captured values (if any) are up to date.
pub fn use_bridge<T, F>(on_output: F) -> UseBridgeHandle<T>
where
T: Bridged,
F: Fn(T::Output) + 'static,
{
let on_output = Rc::new(on_output);

let on_output_clone = on_output.clone();
let on_output_ref = use_ref(move || on_output_clone);

// Refresh the callback on every render.
{
let mut on_output_ref = on_output_ref.borrow_mut();
*on_output_ref = on_output;
}

let bridge = use_ref(move || {
T::bridge(Callback::from(move |output| {
let on_output = on_output_ref.borrow().clone();
on_output(output);
}))
});

UseBridgeHandle { inner: bridge }
}
2 changes: 2 additions & 0 deletions packages/yew-agent/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! This module contains types to support multi-threading and state management.
mod hooks;
mod link;
mod local;
mod pool;
pub mod utils;
mod worker;

pub use hooks::{use_bridge, UseBridgeHandle};
pub use link::AgentLink;
pub(crate) use link::*;
pub use local::{Context, Job};
Expand Down
2 changes: 2 additions & 0 deletions website/docs/concepts/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ In order for agents to run concurrently, Yew uses

A bridge allows bi-directional communication between an agent and a component. Bridges also allow agents to communicate with one another.

A `use_bridge` hook is also provided to create bridges in a function component.

### Dispatchers

A dispatcher allows uni-directional communication between a component and an agent. A dispatcher allows a component to send messages to an agent.
Expand Down

0 comments on commit fb9c398

Please sign in to comment.