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, PubNubError},
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    /// Delayed heartbeat effect invocation.
24    DelayedHeartbeat {
25        /// User input with channels and groups.
26        ///
27        /// Object contains list of channels and groups for which `user_id`
28        /// presence should be announced.
29        input: PresenceInput,
30
31        /// Delayed heartbeat retry attempt.
32        ///
33        /// Used to track overall number of delayed heartbeat retry attempts.
34        attempts: u8,
35
36        /// Delayed heartbeat attempt failure reason.
37        reason: PubNubError,
38    },
39
40    /// Cancel delayed heartbeat effect invocation.
41    CancelDelayedHeartbeat,
42
43    /// Leave effect invocation.
44    Leave {
45        /// User input with channels and groups.
46        ///
47        /// Object contains list of channels and groups for which `user_id`
48        /// should leave.
49        input: PresenceInput,
50    },
51
52    /// Delay effect invocation.
53    Wait {
54        /// User input with channels and groups.
55        ///
56        /// Object contains list of channels and groups for which `user_id`
57        /// presence should be announced.
58        input: PresenceInput,
59    },
60
61    /// Cancel delay effect invocation.
62    CancelWait,
63
64    /// Terminate Presence Event Engine processing loop.
65    TerminateEventEngine,
66}
67
68impl EffectInvocation for PresenceEffectInvocation {
69    type Effect = PresenceEffect;
70    type Event = PresenceEvent;
71
72    fn id(&self) -> &str {
73        match self {
74            Self::Heartbeat { .. } => "HEARTBEAT",
75            Self::DelayedHeartbeat { .. } => "DELAYED_HEARTBEAT",
76            Self::CancelDelayedHeartbeat => "CANCEL_DELAYED_HEARTBEAT",
77            Self::Leave { .. } => "LEAVE",
78            Self::Wait { .. } => "WAIT",
79            Self::CancelWait => "CANCEL_WAIT",
80            Self::TerminateEventEngine => "TERMINATE_EVENT_ENGINE",
81        }
82    }
83
84    fn is_managed(&self) -> bool {
85        matches!(self, Self::Wait { .. } | Self::DelayedHeartbeat { .. })
86    }
87
88    fn is_cancelling(&self) -> bool {
89        matches!(self, Self::CancelDelayedHeartbeat | Self::CancelWait)
90    }
91
92    fn cancelling_effect(&self, effect: &Self::Effect) -> bool {
93        (matches!(effect, PresenceEffect::DelayedHeartbeat { .. })
94            && matches!(self, Self::CancelDelayedHeartbeat { .. }))
95            || (matches!(effect, PresenceEffect::Wait { .. })
96                && matches!(self, Self::CancelWait { .. }))
97    }
98
99    fn is_terminating(&self) -> bool {
100        matches!(self, Self::TerminateEventEngine)
101    }
102}
103
104impl Display for PresenceEffectInvocation {
105    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
106        match self {
107            Self::Heartbeat { .. } => write!(f, "HEARTBEAT"),
108            Self::DelayedHeartbeat { .. } => write!(f, "DELAYED_HEARTBEAT"),
109            Self::CancelDelayedHeartbeat => write!(f, "CANCEL_DELAYED_HEARTBEAT"),
110            Self::Leave { .. } => write!(f, "LEAVE"),
111            Self::Wait { .. } => write!(f, "WAIT"),
112            Self::CancelWait => write!(f, "CANCEL_WAIT"),
113            Self::TerminateEventEngine => write!(f, "TERMINATE_EVENT_ENGINE"),
114        }
115    }
116}