feat(qconnect-core): add queue domain model, reducer and pending lifecycle

-cx
This commit is contained in:
vicrodh committed 2026-02-21 12:17:36 -06:00
1 parent 373220c379
commit 18b1bd76e0
7 files changed
+840

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
use crate::queue::QueueVersion;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PendingQueueAction {
pub uuid: String,
pub queue_version_ref: QueueVersion,
pub emit_result_event: bool,
pub is_ask_for_state_action: bool,
pub concurrency_error: bool,
pub sent_at_ms: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PendingCorrelation {
NoPending,
EventWithoutActionUuid,
Matched,
Concurrent,
}
#[derive(Debug, Error)]
pub enum PendingActionError {
#[error("pending queue action already active: {0}")]
AlreadyPending(String),
}
#[derive(Debug, Default)]
pub struct PendingActionSlot {
current: Option<PendingQueueAction>,
}
impl PendingActionSlot {
pub fn start(&mut self, action: PendingQueueAction) -> Result<(), PendingActionError> {
if let Some(existing) = &self.current {
return Err(PendingActionError::AlreadyPending(existing.uuid.clone()));
}
self.current = Some(action);
Ok(())
}
pub fn current(&self) -> Option<&PendingQueueAction> {
self.current.as_ref()
}
pub fn current_mut(&mut self) -> Option<&mut PendingQueueAction> {
self.current.as_mut()
}
pub fn correlate(&self, event_action_uuid: Option<&str>) -> PendingCorrelation {
let Some(pending) = &self.current else {
return PendingCorrelation::NoPending;
};
let Some(event_uuid) = event_action_uuid else {
return PendingCorrelation::EventWithoutActionUuid;
};
if pending.uuid == event_uuid {
PendingCorrelation::Matched
} else {
PendingCorrelation::Concurrent
}
}
pub fn mark_concurrency_error(&mut self) -> bool {
if let Some(current) = &mut self.current {
current.concurrency_error = true;
return true;
}
false
}
pub fn clear(&mut self) -> Option<PendingQueueAction> {
self.current.take()
}
}