|
| 1 | +use crate::{ |
| 2 | + core::event_engine::{Effect, EffectHandler, EffectInvocation}, |
| 3 | + lib::alloc::{rc::Rc, vec, vec::Vec}, |
| 4 | +}; |
| 5 | +use phantom_type::PhantomType; |
| 6 | +use spin::rwlock::RwLock; |
| 7 | + |
| 8 | +/// State machine effects dispatcher. |
| 9 | +#[allow(dead_code)] |
| 10 | +pub(crate) struct EffectDispatcher<EH, EF, EI> |
| 11 | +where |
| 12 | + EI: EffectInvocation<Effect = EF>, |
| 13 | + EH: EffectHandler<EI, EF>, |
| 14 | + EF: Effect<Invocation = EI>, |
| 15 | +{ |
| 16 | + /// Effect invocation handler. |
| 17 | + /// |
| 18 | + /// Handler responsible for providing actual implementation of |
| 19 | + handler: EH, |
| 20 | + |
| 21 | + /// Dispatched effects managed by dispatcher. |
| 22 | + /// |
| 23 | + /// There are effects whose lifetime should be managed by the dispatcher. |
| 24 | + /// State machines may have some effects that are exclusive and can only run |
| 25 | + /// one type of them at once. The dispatcher handles such effects |
| 26 | + /// and cancels them when required. |
| 27 | + managed: RwLock<Vec<Rc<EF>>>, |
| 28 | + |
| 29 | + _invocation: PhantomType<EI>, |
| 30 | +} |
| 31 | + |
| 32 | +impl<EH, EF, EI> EffectDispatcher<EH, EF, EI> |
| 33 | +where |
| 34 | + EI: EffectInvocation<Effect = EF>, |
| 35 | + EH: EffectHandler<EI, EF>, |
| 36 | + EF: Effect<Invocation = EI>, |
| 37 | +{ |
| 38 | + /// Create new effects dispatcher. |
| 39 | + pub fn new(handler: EH) -> Self { |
| 40 | + EffectDispatcher { |
| 41 | + handler, |
| 42 | + managed: RwLock::new(vec![]), |
| 43 | + _invocation: Default::default(), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// Dispatch effect associated with `invocation`. |
| 48 | + pub fn dispatch<F>(&self, invocation: &EI, mut f: F) |
| 49 | + where |
| 50 | + F: FnMut(Option<Vec<EI::Event>>), |
| 51 | + { |
| 52 | + if let Some(effect) = self.handler.create(invocation) { |
| 53 | + let effect = Rc::new(effect); |
| 54 | + |
| 55 | + if invocation.managed() { |
| 56 | + let mut managed = self.managed.write(); |
| 57 | + managed.push(effect.clone()); |
| 58 | + } |
| 59 | + |
| 60 | + // Placeholder for effect invocation. |
| 61 | + effect.run(|events| { |
| 62 | + // Try remove effect from list of managed. |
| 63 | + self.remove_managed_effect(&effect); |
| 64 | + |
| 65 | + // Notify about effect run completion. |
| 66 | + // Placeholder for effect events processing (pass to effects handler). |
| 67 | + f(events); |
| 68 | + }); |
| 69 | + } else if invocation.cancelling() { |
| 70 | + self.cancel_effect(invocation); |
| 71 | + |
| 72 | + // Placeholder for effect events processing (pass to effects handler). |
| 73 | + f(None); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Handle effect cancellation. |
| 78 | + /// |
| 79 | + /// Effects with managed lifecycle can be cancelled by corresponding effect |
| 80 | + /// invocations. |
| 81 | + fn cancel_effect(&self, invocation: &EI) { |
| 82 | + let mut managed = self.managed.write(); |
| 83 | + if let Some(position) = managed.iter().position(|e| invocation.cancelling_effect(e)) { |
| 84 | + managed.remove(position).cancel(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Remove managed effect. |
| 89 | + fn remove_managed_effect(&self, effect: &EF) { |
| 90 | + let mut managed = self.managed.write(); |
| 91 | + if let Some(position) = managed.iter().position(|ef| ef.id() == effect.id()) { |
| 92 | + managed.remove(position); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod should { |
| 99 | + use super::*; |
| 100 | + use crate::core::event_engine::Event; |
| 101 | + |
| 102 | + enum TestEvent {} |
| 103 | + |
| 104 | + impl Event for TestEvent { |
| 105 | + fn id(&self) -> &str { |
| 106 | + "no_id" |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + enum TestEffect { |
| 111 | + One, |
| 112 | + Two, |
| 113 | + Three, |
| 114 | + } |
| 115 | + |
| 116 | + impl Effect for TestEffect { |
| 117 | + type Invocation = TestInvocation; |
| 118 | + |
| 119 | + fn id(&self) -> String { |
| 120 | + match self { |
| 121 | + Self::One => "EFFECT_ONE".into(), |
| 122 | + Self::Two => "EFFECT_TWO".into(), |
| 123 | + Self::Three => "EFFECT_THREE".into(), |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + fn run<F>(&self, mut f: F) |
| 128 | + where |
| 129 | + F: FnMut(Option<Vec<TestEvent>>), |
| 130 | + { |
| 131 | + match self { |
| 132 | + Self::Three => {} |
| 133 | + _ => f(None), |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + fn cancel(&self) { |
| 138 | + // Do nothing. |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + enum TestInvocation { |
| 143 | + One, |
| 144 | + Two, |
| 145 | + Three, |
| 146 | + CancelThree, |
| 147 | + } |
| 148 | + |
| 149 | + impl EffectInvocation for TestInvocation { |
| 150 | + type Effect = TestEffect; |
| 151 | + type Event = TestEvent; |
| 152 | + |
| 153 | + fn id(&self) -> &str { |
| 154 | + match self { |
| 155 | + Self::One => "EFFECT_ONE_INVOCATION", |
| 156 | + Self::Two => "EFFECT_TWO_INVOCATION", |
| 157 | + Self::Three => "EFFECT_THREE_INVOCATION", |
| 158 | + Self::CancelThree => "EFFECT_THREE_CANCEL_INVOCATION", |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + fn managed(&self) -> bool { |
| 163 | + matches!(self, Self::Two | Self::Three) |
| 164 | + } |
| 165 | + |
| 166 | + fn cancelling(&self) -> bool { |
| 167 | + matches!(self, Self::CancelThree) |
| 168 | + } |
| 169 | + |
| 170 | + fn cancelling_effect(&self, effect: &Self::Effect) -> bool { |
| 171 | + match self { |
| 172 | + TestInvocation::CancelThree => matches!(effect, TestEffect::Three), |
| 173 | + _ => false, |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + struct TestEffectHandler {} |
| 179 | + |
| 180 | + impl EffectHandler<TestInvocation, TestEffect> for TestEffectHandler { |
| 181 | + fn create(&self, invocation: &TestInvocation) -> Option<TestEffect> { |
| 182 | + match invocation { |
| 183 | + TestInvocation::One => Some(TestEffect::One), |
| 184 | + TestInvocation::Two => Some(TestEffect::Two), |
| 185 | + TestInvocation::Three => Some(TestEffect::Three), |
| 186 | + _ => None, |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + #[test] |
| 192 | + fn run_not_managed_effect() { |
| 193 | + let mut called = false; |
| 194 | + let dispatcher = EffectDispatcher::new(TestEffectHandler {}); |
| 195 | + dispatcher.dispatch(&TestInvocation::One, |_| { |
| 196 | + called = true; |
| 197 | + }); |
| 198 | + |
| 199 | + assert!(called, "Expected to call effect for TestInvocation::One"); |
| 200 | + assert_eq!( |
| 201 | + dispatcher.managed.read().len(), |
| 202 | + 0, |
| 203 | + "Non managed effects shouldn't be stored" |
| 204 | + ); |
| 205 | + } |
| 206 | + |
| 207 | + #[test] |
| 208 | + fn run_managed_effect() { |
| 209 | + let mut called = false; |
| 210 | + let dispatcher = EffectDispatcher::new(TestEffectHandler {}); |
| 211 | + dispatcher.dispatch(&TestInvocation::Two, |_| { |
| 212 | + called = true; |
| 213 | + }); |
| 214 | + |
| 215 | + assert!(called, "Expected to call effect for TestInvocation::Two"); |
| 216 | + assert_eq!( |
| 217 | + dispatcher.managed.read().len(), |
| 218 | + 0, |
| 219 | + "Managed effect should be removed on completion" |
| 220 | + ); |
| 221 | + } |
| 222 | + |
| 223 | + #[test] |
| 224 | + fn cancel_managed_effect() { |
| 225 | + let mut called_managed = false; |
| 226 | + let mut cancelled_managed = false; |
| 227 | + let dispatcher = EffectDispatcher::new(TestEffectHandler {}); |
| 228 | + dispatcher.dispatch(&TestInvocation::Three, |_| { |
| 229 | + called_managed = true; |
| 230 | + }); |
| 231 | + |
| 232 | + assert!( |
| 233 | + !called_managed, |
| 234 | + "Expected that effect for TestInvocation::Three won't be called" |
| 235 | + ); |
| 236 | + assert_eq!( |
| 237 | + dispatcher.managed.read().len(), |
| 238 | + 1, |
| 239 | + "Managed effect shouldn't complete run because doesn't have completion call" |
| 240 | + ); |
| 241 | + |
| 242 | + dispatcher.dispatch(&TestInvocation::CancelThree, |_| { |
| 243 | + cancelled_managed = true; |
| 244 | + }); |
| 245 | + |
| 246 | + assert!( |
| 247 | + cancelled_managed, |
| 248 | + "Expected to call effect for TestInvocation::CancelThree" |
| 249 | + ); |
| 250 | + assert!( |
| 251 | + !called_managed, |
| 252 | + "Expected that effect for TestInvocation::Three won't be called" |
| 253 | + ); |
| 254 | + assert_eq!( |
| 255 | + dispatcher.managed.read().len(), |
| 256 | + 0, |
| 257 | + "Managed effect should be cancelled" |
| 258 | + ); |
| 259 | + } |
| 260 | +} |
0 commit comments