yew_stdweb/agent/pool.rs
1use super::*;
2use crate::scheduler::Shared;
3use log::warn;
4use slab::Slab;
5
6pub(crate) type Last = bool;
7
8/// Type alias to a sharable Slab that owns optional callbacks that emit messages of the type of the specified Agent.
9pub(crate) type SharedOutputSlab<AGN> = Shared<Slab<Option<Callback<<AGN as Agent>::Output>>>>;
10
11/// The slab contains the callback, the id is used to look up the callback,
12/// and the output is the message that will be sent via the callback.
13pub(crate) fn locate_callback_and_respond<AGN: Agent>(
14 slab: &SharedOutputSlab<AGN>,
15 id: HandlerId,
16 output: AGN::Output,
17) {
18 let callback = {
19 let slab = slab.borrow();
20 match slab.get(id.raw_id()).cloned() {
21 Some(callback) => callback,
22 None => {
23 warn!("Id of handler does not exist in the slab: {}.", id.raw_id());
24 return;
25 }
26 }
27 };
28 match callback {
29 Some(callback) => callback.emit(output),
30 None => warn!("The Id of the handler: {}, while present in the slab, is not associated with a callback.", id.raw_id()),
31 }
32}
33
34/// A newtype around a bridge to indicate that it is distinct from a normal bridge
35pub struct Dispatcher<T>(pub(crate) Box<dyn Bridge<T>>);
36
37impl<T> fmt::Debug for Dispatcher<T> {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.write_str("Dispatcher<_>")
40 }
41}
42
43impl<T> Deref for Dispatcher<T> {
44 type Target = dyn Bridge<T>;
45
46 fn deref(&self) -> &Self::Target {
47 self.0.deref()
48 }
49}
50impl<T> DerefMut for Dispatcher<T> {
51 fn deref_mut(&mut self) -> &mut Self::Target {
52 self.0.deref_mut()
53 }
54}
55
56/// This trait allows the creation of a dispatcher to an existing agent that will not send replies when messages are sent.
57pub trait Dispatched: Agent + Sized + 'static {
58 /// Creates a dispatcher to the agent that will not send messages back.
59 ///
60 /// # Note
61 /// Dispatchers don't have `HandlerId`s and therefore `Agent::handle` will be supplied `None`
62 /// for the `id` parameter, and `connected` and `disconnected` will not be called.
63 ///
64 /// # Important
65 /// Because the Agents using Context or Public reaches use the number of existing bridges to
66 /// keep track of if the agent itself should exist, creating dispatchers will not guarantee that
67 /// an Agent will exist to service requests sent from Dispatchers. You **must** keep at least one
68 /// bridge around if you wish to use a dispatcher. If you are using agents in a write-only manner,
69 /// then it is suggested that you create a bridge that handles no-op responses as high up in the
70 /// component hierarchy as possible - oftentimes the root component for simplicity's sake.
71 fn dispatcher() -> Dispatcher<Self>;
72}
73
74#[doc(hidden)]
75pub trait Dispatchable {}
76
77impl<T> Dispatched for T
78where
79 T: Agent,
80 <T as Agent>::Reach: Discoverer<Agent = T>,
81 <T as Agent>::Reach: Dispatchable,
82{
83 fn dispatcher() -> Dispatcher<T> {
84 Dispatcher(Self::Reach::spawn_or_join(None))
85 }
86}