pubnub/dx/presence/event_engine/
invocation.rs

1//! Heartbeat Event Engine effect invocation module.
2//!
3//! The module contains the [`PresenceEffectInvocation`] type, which describes
4//! available event engine effect invocations.
5
6use crate::{
7    core::event_engine::EffectInvocation,
8    lib::core::fmt::{Display, Formatter, Result},
9    presence::event_engine::{PresenceEffect, PresenceEvent, PresenceInput},
10};
11
12#[derive(Debug)]
13pub(crate) enum PresenceEffectInvocation {
14    /// Heartbeat effect invocation.
15    Heartbeat {
16        /// User input with channels and groups.
17        ///
18        /// Object contains list of channels and groups for which `user_id`
19        /// presence should be announced.
20        input: PresenceInput,
21    },
22
23    /// Leave effect invocation.
24    Leave {
25        /// User input with channels and groups.
26        ///
27        /// Object contains list of channels and groups for which `user_id`
28        /// should leave.
29        input: PresenceInput,
30    },
31
32    /// Delay effect invocation.
33    Wait {
34        /// User input with channels and groups.
35        ///
36        /// Object contains list of channels and groups for which `user_id`
37        /// presence should be announced.
38        input: PresenceInput,
39    },
40
41    /// Cancel delay effect invocation.
42    CancelWait,
43
44    /// Terminate Presence Event Engine processing loop.
45    TerminateEventEngine,
46}
47
48impl EffectInvocation for PresenceEffectInvocation {
49    type Effect = PresenceEffect;
50    type Event = PresenceEvent;
51
52    fn id(&self) -> &str {
53        match self {
54            Self::Heartbeat { .. } => "HEARTBEAT",
55            Self::Leave { .. } => "LEAVE",
56            Self::Wait { .. } => "WAIT",
57            Self::CancelWait => "CANCEL_WAIT",
58            Self::TerminateEventEngine => "TERMINATE_EVENT_ENGINE",
59        }
60    }
61
62    fn is_managed(&self) -> bool {
63        matches!(self, Self::Wait { .. })
64    }
65
66    fn is_cancelling(&self) -> bool {
67        matches!(self, Self::CancelWait)
68    }
69
70    fn cancelling_effect(&self, effect: &Self::Effect) -> bool {
71        matches!(effect, PresenceEffect::Wait { .. }) && matches!(self, Self::CancelWait { .. })
72    }
73
74    fn is_terminating(&self) -> bool {
75        matches!(self, Self::TerminateEventEngine)
76    }
77}
78
79impl Display for PresenceEffectInvocation {
80    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
81        match self {
82            Self::Heartbeat { .. } => write!(f, "HEARTBEAT"),
83            Self::Leave { .. } => write!(f, "LEAVE"),
84            Self::Wait { .. } => write!(f, "WAIT"),
85            Self::CancelWait => write!(f, "CANCEL_WAIT"),
86            Self::TerminateEventEngine => write!(f, "TERMINATE_EVENT_ENGINE"),
87        }
88    }
89}