ruex/prelude/observer.rs
1use std::{fmt::Debug, rc::Rc};
2
3use super::Notification;
4
5/// Defines NotifyContext identity
6pub trait NotifyContext: Debug {
7 /// Retrieve context identity
8 fn id(&self) -> u64;
9}
10
11/// The definition for a PureMVC Observer.
12///
13/// In PureMVC, [Observer] implementors assume these responsibilities:
14///
15/// - Encapsulate the notification (callback) method of the interested object.
16/// - Encapsulate the notification context (this) of the interested object.
17/// - Provide methods for setting the interested object' notification method and context.
18/// - Provide a method for notifying the interested object.
19///
20///
21/// PureMVC does not rely upon underlying event
22/// models such as the one provided with Flash.
23///
24/// The Observer Pattern as implemented within
25/// PureMVC exists to support event driven communication
26/// between the application and the actors of the MVC triad.
27///
28/// An Observer is an object that encapsulates information
29/// about an interested object with a notification method that
30/// should be called when an [Notification] is broadcast. The Observer then
31/// acts as a proxy for notifying the interested object.
32///
33/// Observers can receive [Notification]'s by having their
34/// [notify](Observer::notify) method invoked, passing
35/// in an object implementing the [Notification] interface, such
36/// as a subclass of [Notification].
37
38pub trait Observer<Body>: Debug
39where
40 Body: Debug + 'static,
41{
42 /// Get the notification context.
43 fn context(&self) -> &Rc<dyn NotifyContext>;
44
45 /// Set the notification method.
46 ///
47 /// The notification method should take one parameter of type [Notification]
48 fn set_method(&mut self, notify_method: Box<dyn Fn(Rc<dyn Notification<Body>>)>);
49
50 /// Set the notification context.
51 fn set_context(&mut self, notify_context: Rc<dyn NotifyContext>);
52
53 /// Notify the interested object.
54 fn notify(&self, notification: Rc<dyn Notification<Body>>);
55
56 /// Compare the given object to the notificaiton context object.
57 fn compare_context(&self, object: &Rc<dyn NotifyContext>) -> bool;
58}