pubnub/core/event_engine/state.rs
1use crate::{
2 core::event_engine::{EffectInvocation, Event, Transition},
3 lib::alloc::vec::Vec,
4};
5
6/// State machine state trait.
7///
8/// For transition, the state machine needs to know which effects should be
9/// dispatched during transition to target state in response to a specific
10/// event.
11///
12/// Types which are expected to be used as states should implement the trait.
13pub(crate) trait State: Clone + PartialEq {
14 type State: State;
15 type Invocation: EffectInvocation;
16 type Event: Event;
17
18 /// State enter effects invocations.
19 ///
20 /// The list of effect invocations that should be called when the event
21 /// engine enters state.
22 fn enter(&self) -> Option<Vec<Self::Invocation>>;
23
24 /// State exit effects invocations.
25 ///
26 /// The list of effect invocations that should be called when the event
27 /// engine leaves state.
28 fn exit(&self) -> Option<Vec<Self::Invocation>>;
29
30 /// System event handler.
31 ///
32 /// State has information about the next state into which the state machine
33 /// should switch and a list of effects invocations which should be
34 /// scheduled.
35 fn transition(
36 &self,
37 event: &<<Self as State>::Invocation as EffectInvocation>::Event,
38 ) -> Option<Transition<Self::State, Self::Invocation>>;
39
40 /// [`Transition`] build helper.
41 ///
42 /// Transition to a new state is a composite operation which includes
43 /// dispatching of [`exit`] effect invocations of receiver, followed by
44 /// dispatch of provided transition effect `invocations` and [`enter`]
45 /// effect invocations of target state.
46 fn transition_to(
47 &self,
48 state: Option<Self::State>,
49 invocations: Option<Vec<Self::Invocation>>,
50 ) -> Transition<Self::State, Self::Invocation>;
51}