-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep the entity declaration in the backend
The front end simply needs to understand how to event source state, and we want to keep the entity management at the backend.
- Loading branch information
Showing
4 changed files
with
72 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,45 @@ | ||
// Handle temperature sensor entity concerns | ||
// Handle temperature sensor state concerns | ||
|
||
use std::collections::VecDeque; | ||
|
||
use akka_persistence_rs::{ | ||
effect::{emit_event, reply, unhandled, Effect, EffectExt}, | ||
entity::{Context, EventSourcedBehavior}, | ||
}; | ||
use akka_persistence_rs::entity::Context; | ||
use serde::{Deserialize, Serialize}; | ||
use smol_str::SmolStr; | ||
use tokio::sync::oneshot; | ||
|
||
// Declare the entity and its behavior | ||
// Declare the state and how it is to be sourced from events | ||
|
||
#[derive(Default)] | ||
pub struct State { | ||
pub history: VecDeque<u32>, | ||
pub secret: SecretDataValue, | ||
} | ||
|
||
pub type SecretDataValue = SmolStr; | ||
|
||
pub enum Command { | ||
Get { reply_to: oneshot::Sender<Vec<u32>> }, | ||
Post { temperature: u32 }, | ||
Register { secret: SecretDataValue }, | ||
} | ||
|
||
#[derive(Clone, Deserialize, Serialize)] | ||
pub enum Event { | ||
Registered { secret: SecretDataValue }, | ||
TemperatureRead { temperature: u32 }, | ||
} | ||
|
||
pub struct Behavior; | ||
|
||
const MAX_HISTORY_EVENTS: usize = 10; | ||
|
||
impl EventSourcedBehavior for Behavior { | ||
type State = State; | ||
|
||
type Command = Command; | ||
|
||
type Event = Event; | ||
|
||
fn for_command( | ||
_context: &Context, | ||
state: &Self::State, | ||
command: Self::Command, | ||
) -> Box<dyn Effect<Self>> { | ||
match command { | ||
Command::Get { reply_to } if !state.secret.is_empty() => { | ||
reply(reply_to, state.history.clone().into()).boxed() | ||
} | ||
impl State { | ||
// We provide an event sourcing function so that we can | ||
// source state from events whether we are using the entity | ||
// manager or not. | ||
|
||
Command::Post { temperature } if !state.secret.is_empty() => { | ||
emit_event(Event::TemperatureRead { temperature }).boxed() | ||
} | ||
|
||
Command::Register { secret } => emit_event(Event::Registered { secret }).boxed(), | ||
|
||
_ => unhandled(), | ||
} | ||
} | ||
|
||
fn on_event(_context: &Context, state: &mut Self::State, event: Self::Event) { | ||
pub fn on_event(&mut self, _context: &Context, event: Event) { | ||
match event { | ||
Event::Registered { secret } => { | ||
state.secret = secret; | ||
self.secret = secret; | ||
} | ||
Event::TemperatureRead { temperature } => { | ||
if state.history.len() == MAX_HISTORY_EVENTS { | ||
state.history.pop_front(); | ||
if self.history.len() == MAX_HISTORY_EVENTS { | ||
self.history.pop_front(); | ||
} | ||
state.history.push_back(temperature); | ||
self.history.push_back(temperature); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub type SecretDataValue = SmolStr; | ||
|
||
#[derive(Clone, Deserialize, Serialize)] | ||
pub enum Event { | ||
Registered { secret: SecretDataValue }, | ||
TemperatureRead { temperature: u32 }, | ||
} |