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
use std::{any::Any, fmt::Debug, rc::Rc};

use super::{Interest, Notification, NotifyContext, View};

/// The definition for a PureMVC [Mediator].
///
/// In PureMVC, [Mediator] implementors assume these responsibilities:
///
/// - Implement a common method which returns a list of all [Notification]'s
/// the [Mediator] has interest in.
/// - Implement a common notification (callback) method.
///
/// Additionally, [Mediator]'s typically:
///
/// - Act as an intermediary between one or more view components such as text boxes or
/// list controls, maintaining references and coordinating their behavior.
/// - In Flash-based apps, this is often the place where event listeners are
/// added to view components, and their handlers implemented.
/// - Respond to and generate [Notification]'s, interacting with of
/// the rest of the PureMVC app.
///
/// When an [Mediator] is registered with the [View],
/// the [View] will call the [Mediator]'s
/// [list_notification_interests] method. The [Mediator] will
/// return an [Vec] of [Notification] names which
/// it wishes to be notified about.
///
/// The [View] will then create an [Observer] object
/// encapsulating that [Mediator]'s [handle_notification] method
/// and register it as an [Observer] for each [Notification] name returned by
/// [list_notification_interests].
/// 
/// [Observer]: crate::prelude::Observer
/// [handle_notification]: Mediator::handle_notification
/// [list_notification_interests]: Mediator::list_notification_interests

pub trait Mediator<Body>: NotifyContext + Debug + Sized + Any
where
    Body: Debug + 'static,
{
    /// Get the [Mediator]'s view component.
    fn view_component(&self) -> Option<Rc<dyn View<Body>>>;

    /// Set the [Mediator]'s view component.
    fn set_view_component(&mut self, component: Option<Rc<dyn View<Body>>>);

    /// List [Notification] interests.
    fn list_notification_interests(&self) -> &[Interest];

    /// Handle an [Notification].
    fn handle_notification(&self, notification: Rc<dyn Notification<Body>>);

    /// Called by the [View] when the [Mediator] is registered
    fn on_register(&self);

    /// Called by the [View] when the [Mediator] is removed
    fn on_remove(&self);
}