-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediator.rs
79 lines (65 loc) · 1.91 KB
/
mediator.rs
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
use std::{fmt, rc::Rc};
use crate::{
foundation::patterns::facade::BaseFacade,
prelude::{Facade, Interest, Mediator, Notification, Notifier, NotifyContext, Singleton, View},
};
/// A base [Mediator] implementation.
pub struct BaseMediator<Body> {
// The view component
view_component: Option<Rc<dyn View<Body>>>,
}
impl<Body> BaseMediator<Body> {
/// The name of the [Mediator].
///
/// Typically, a [Mediator] will be written to serve
/// one specific control or group controls and so,
/// will not have a need to be dynamically named.
/// Constructor.
pub fn new(view_component: Option<Rc<dyn View<Body>>>) -> Self {
Self { view_component }
}
}
impl<Body> Mediator<Body> for BaseMediator<Body>
where
Body: fmt::Debug + 'static,
{
fn view_component(&self) -> Option<Rc<dyn View<Body>>> {
self.view_component.as_ref().map(|c| c.clone())
}
fn handle_notification(&self, _notification: Rc<dyn Notification<Body>>) {}
fn list_notification_interests(&self) -> &[Interest] {
&[]
}
fn on_register(&self) {}
fn on_remove(&self) {}
fn set_view_component(&mut self, view_component: Option<Rc<dyn View<Body>>>) {
self.view_component = view_component;
}
}
impl<Body> NotifyContext for BaseMediator<Body>
where
Body: fmt::Debug + 'static,
{
fn id(&self) -> u64 {
0x01
}
}
impl<Body> Notifier<Body> for BaseMediator<Body>
where
Body: fmt::Debug + 'static,
{
fn send(&self, interest: Interest, body: Option<Body>) {
log::error!("You should implement yourself Mediator");
BaseFacade::<Body>::global().send(interest, body);
}
}
impl<Body> fmt::Debug for BaseMediator<Body>
where
Body: fmt::Debug + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EmployeesMediator")
// .field("x", &self.x)
.finish()
}
}