1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
mod link;
mod local;
mod pool;
mod worker;
pub use link::AgentLink;
pub(crate) use link::*;
pub use local::{Context, Job};
pub(crate) use pool::*;
pub use pool::{Dispatched, Dispatcher};
pub use worker::{Private, Public, Threaded};
use crate::callback::Callback;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::ops::{Deref, DerefMut};
pub trait Agent: Sized + 'static {
type Reach: Discoverer<Agent = Self>;
type Message;
type Input;
type Output;
fn create(link: AgentLink<Self>) -> Self;
fn update(&mut self, msg: Self::Message);
fn connected(&mut self, _id: HandlerId) {}
fn handle_input(&mut self, msg: Self::Input, id: HandlerId);
fn disconnected(&mut self, _id: HandlerId) {}
fn destroy(&mut self) {}
fn name_of_resource() -> &'static str {
"main.js"
}
fn is_module() -> bool {
false
}
}
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone, Copy)]
pub struct HandlerId(usize, bool);
impl HandlerId {
fn new(id: usize, respondable: bool) -> Self {
HandlerId(id, respondable)
}
fn raw_id(self) -> usize {
self.0
}
pub fn is_respondable(self) -> bool {
self.1
}
}
#[doc(hidden)]
pub trait Discoverer {
type Agent: Agent;
fn spawn_or_join(
_callback: Option<Callback<<Self::Agent as Agent>::Output>>,
) -> Box<dyn Bridge<Self::Agent>>;
}
pub trait Bridge<AGN: Agent> {
fn send(&mut self, msg: AGN::Input);
}
pub trait Bridged: Agent + Sized + 'static {
fn bridge(callback: Callback<Self::Output>) -> Box<dyn Bridge<Self>>;
}
impl<T> Bridged for T
where
T: Agent,
<T as Agent>::Reach: Discoverer<Agent = T>,
{
fn bridge(callback: Callback<Self::Output>) -> Box<dyn Bridge<Self>> {
Self::Reach::spawn_or_join(Some(callback))
}
}