yew_stdweb/agent/
mod.rs

1//! This module contains types to support multi-threading and state management.
2
3mod link;
4mod local;
5mod pool;
6mod worker;
7
8pub use link::AgentLink;
9pub(crate) use link::*;
10pub use local::{Context, Job};
11pub(crate) use pool::*;
12pub use pool::{Dispatched, Dispatcher};
13pub use worker::{Private, Public, Threaded};
14
15use crate::callback::Callback;
16use serde::{Deserialize, Serialize};
17use std::fmt;
18use std::ops::{Deref, DerefMut};
19
20/// Declares the behavior of the agent.
21pub trait Agent: Sized + 'static {
22    /// Reach capability of the agent.
23    type Reach: Discoverer<Agent = Self>;
24    /// Type of an input message.
25    type Message;
26    /// Incoming message type.
27    type Input;
28    /// Outgoing message type.
29    type Output;
30
31    /// Creates an instance of an agent.
32    fn create(link: AgentLink<Self>) -> Self;
33
34    /// This method called on every update message.
35    fn update(&mut self, msg: Self::Message);
36
37    /// This method called on when a new bridge created.
38    fn connected(&mut self, _id: HandlerId) {}
39
40    /// This method called on every incoming message.
41    fn handle_input(&mut self, msg: Self::Input, id: HandlerId);
42
43    /// This method called on when a new bridge destroyed.
44    fn disconnected(&mut self, _id: HandlerId) {}
45
46    /// This method called when the agent is destroyed.
47    fn destroy(&mut self) {}
48
49    /// Represents the name of loading resorce for remote workers which
50    /// have to live in a separate files.
51    fn name_of_resource() -> &'static str {
52        "main.js"
53    }
54
55    /// Signifies if resource is a module.
56    /// This has pending browser support.
57    fn is_module() -> bool {
58        false
59    }
60}
61
62/// Id of responses handler.
63#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone, Copy)]
64pub struct HandlerId(usize, bool);
65
66impl HandlerId {
67    fn new(id: usize, respondable: bool) -> Self {
68        HandlerId(id, respondable)
69    }
70    fn raw_id(self) -> usize {
71        self.0
72    }
73    /// Indicates if a handler id corresponds to callback in the Agent runtime.
74    pub fn is_respondable(self) -> bool {
75        self.1
76    }
77}
78
79/// Determine a visibility of an agent.
80#[doc(hidden)]
81pub trait Discoverer {
82    type Agent: Agent;
83
84    /// Spawns an agent and returns `Bridge` implementation.
85    fn spawn_or_join(
86        _callback: Option<Callback<<Self::Agent as Agent>::Output>>,
87    ) -> Box<dyn Bridge<Self::Agent>>;
88}
89
90/// Bridge to a specific kind of worker.
91pub trait Bridge<AGN: Agent> {
92    /// Send a message to an agent.
93    fn send(&mut self, msg: AGN::Input);
94}
95
96/// This trait allows registering or getting the address of a worker.
97pub trait Bridged: Agent + Sized + 'static {
98    /// Creates a messaging bridge between a worker and the component.
99    fn bridge(callback: Callback<Self::Output>) -> Box<dyn Bridge<Self>>;
100}
101
102impl<T> Bridged for T
103where
104    T: Agent,
105    <T as Agent>::Reach: Discoverer<Agent = T>,
106{
107    fn bridge(callback: Callback<Self::Output>) -> Box<dyn Bridge<Self>> {
108        Self::Reach::spawn_or_join(Some(callback))
109    }
110}