Skip to main content

euv_core/dynamic/state/
struct.rs

1use super::*;
2
3/// Stable monotonically assigned identifier for a host-owned business operation.
4#[derive(
5    Clone,
6    Copy,
7    Data,
8    Debug,
9    Default,
10    Deserialize,
11    Eq,
12    Hash,
13    New,
14    Ord,
15    PartialEq,
16    PartialOrd,
17    Serialize,
18)]
19pub struct OperationId {
20    /// Raw monotonic operation sequence value.
21    #[get(type(copy))]
22    pub(crate) raw: u64,
23}
24
25/// Typed command dispatched by a page or dynamic component to mutate host-owned authoritative state.
26#[derive(Clone, Data, Debug, Deserialize, New, PartialEq, Serialize)]
27pub struct BusinessCommand {
28    /// Target business entity identifier (e.g. `"order:42"`).
29    pub(crate) entity_id: String,
30    /// Strongly-named business command verb (e.g. `"SetQuantity"`, `"SaveOrder"`).
31    pub(crate) command_name: String,
32    /// Optional optimistic concurrency revision that must match live state before commit.
33    #[new(skip)]
34    #[serde(default)]
35    #[get(skip)]
36    pub(crate) expected_revision: Option<u64>,
37    /// Caller-supplied idempotency key reused across retries to prevent duplicate side effects.
38    #[new(skip)]
39    #[serde(default)]
40    pub(crate) idempotency_key: String,
41    /// Release generation active on the originating route when this command was issued.
42    #[new(skip)]
43    #[serde(default)]
44    #[get(type(copy))]
45    pub(crate) origin_generation: u64,
46    /// Typed JSON payload carrying command arguments.
47    pub(crate) payload: Value,
48}
49
50/// One incremental authoritative state delta retained by `BusinessStateStore` for `R -> R+n` catch-up.
51#[derive(Clone, Data, Debug, Deserialize, New, PartialEq, Serialize)]
52pub struct BusinessStateDelta {
53    /// Target business entity identifier.
54    pub(crate) entity_id: String,
55    /// Post-commit authoritative revision number.
56    #[get(type(copy))]
57    pub(crate) revision: u64,
58    /// Stable operation identifier that produced this delta.
59    #[get(type(copy))]
60    pub(crate) operation_id: OperationId,
61    /// Field names modified by this commit for field-filtered subscriber dispatch.
62    pub(crate) changed_fields: Vec<String>,
63    /// Post-commit authoritative state snapshot.
64    pub(crate) state_patch: Value,
65}
66
67/// Host-tracked in-flight business operation surviving component hot-swaps and rollbacks.
68#[derive(Clone, Data, Debug, Deserialize, New, PartialEq, Serialize)]
69pub struct InFlightOperation {
70    /// Stable operation identifier assigned upon host acceptance.
71    #[get(type(copy))]
72    pub(crate) operation_id: OperationId,
73    /// Target business entity identifier.
74    pub(crate) entity_id: String,
75    /// Command verb being executed.
76    pub(crate) command_name: String,
77    /// Deduplication idempotency key.
78    pub(crate) idempotency_key: String,
79    /// Release generation that initiated this operation.
80    #[get(type(copy))]
81    pub(crate) origin_generation: u64,
82    /// Optional expected revision captured when accepted.
83    #[get(skip)]
84    pub(crate) expected_revision: Option<u64>,
85    /// Whether this operation has already reached completion.
86    #[get(type(copy))]
87    pub(crate) completed: bool,
88}
89
90/// Field-filtered subscription record bound to a specific release generation.
91#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
92pub struct StateSubscription {
93    /// Unique subscription identifier.
94    #[get(type(copy))]
95    pub(crate) subscription_id: u64,
96    /// Watched business entity identifier.
97    pub(crate) entity_id: String,
98    /// Release generation owning this subscriber (cancelled when generation unmounts).
99    #[get(type(copy))]
100    pub(crate) generation_id: u64,
101    /// Subset of field names this subscriber cares about (empty means all fields).
102    pub(crate) watched_fields: Vec<String>,
103    /// Whether this subscription is still active.
104    #[new(skip)]
105    #[get(type(copy))]
106    pub(crate) active: bool,
107    /// Revisions delivered to this subscriber.
108    #[new(skip)]
109    pub(crate) delivered_revisions: Vec<u64>,
110}
111
112/// Authoritative record for a single host-owned business entity.
113#[derive(Clone, Debug)]
114pub(crate) struct EntityStateRecord {
115    /// Current authoritative revision number.
116    pub(crate) revision: u64,
117    /// Current authoritative JSON state.
118    pub(crate) state: Value,
119    /// Bounded recent delta history for `R -> R+n` catch-up.
120    pub(crate) deltas: VecDeque<BusinessStateDelta>,
121}
122
123/// Interior mutex-protected state of `BusinessStateStore`.
124#[derive(Debug)]
125pub(crate) struct BusinessStateStoreInner {
126    /// Maximum delta entries kept per entity.
127    pub(crate) delta_cap: usize,
128    /// Monotonic operation ID generator.
129    pub(crate) next_op_id: u64,
130    /// Monotonic subscription ID generator.
131    pub(crate) next_sub_id: u64,
132    /// Monotonic global revision across all entities.
133    pub(crate) global_revision: u64,
134    /// Authoritative entity state map keyed by entity ID.
135    pub(crate) entities: BTreeMap<String, EntityStateRecord>,
136    /// Completed idempotency outcomes keyed by `(entity_id, idempotency_key)`.
137    pub(crate) idempotency_cache: HashMap<(String, String), CommandOutcome>,
138    /// Active in-flight host-owned operations keyed by `OperationId`.
139    pub(crate) in_flight: BTreeMap<OperationId, InFlightOperation>,
140    /// Active field-filtered subscriptions keyed by `subscription_id`.
141    pub(crate) subscriptions: BTreeMap<u64, StateSubscription>,
142}
143
144/// Thread-safe host-owned authoritative business state store surviving hot-swaps and rollbacks.
145#[derive(Clone, Debug)]
146pub struct BusinessStateStore {
147    /// Shared interior store state.
148    pub(crate) inner: Arc<Mutex<BusinessStateStoreInner>>,
149}
150
151/// Stable nested instance identity (`route#tab/entity/slot[/child]`) immune to arena or render index changes.
152#[derive(
153    Clone, Data, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
154)]
155pub struct StableAddress {
156    /// Stable route instance identifier (e.g. `"A#tab-1"`).
157    pub(crate) route_instance: String,
158    /// Ordered ancestor logical business keys (e.g. `["order:42", "editor"]`).
159    pub(crate) ancestor_keys: Vec<String>,
160    /// Logical slot key within the parent component (e.g. `"address"`).
161    pub(crate) slot_key: String,
162    /// Optional stable child business key (never a row number or render index).
163    #[get(skip)]
164    #[serde(default)]
165    pub(crate) child_key: Option<String>,
166}
167
168/// Declarative per-instance UI interaction state captured at revision `R` for background migration.
169#[derive(Clone, Data, Debug, Default, Deserialize, New, PartialEq, Serialize)]
170pub struct ComponentUiStateSnapshot {
171    /// Stable nested address identifying this component instance.
172    pub(crate) address: StableAddress,
173    /// Local state schema version declared by the component.
174    #[get(type(copy))]
175    pub(crate) schema_version: u32,
176    /// Optional business-ID scroll anchor key.
177    #[new(skip)]
178    #[serde(default)]
179    #[get(skip)]
180    pub(crate) scroll_anchor_key: Option<String>,
181    /// Logical scroll offset relative to `scroll_anchor_key`.
182    #[new(skip)]
183    #[serde(default)]
184    #[get(type(copy))]
185    pub(crate) scroll_offset_y: f32,
186    /// Optional focused logical node key.
187    #[new(skip)]
188    #[serde(default)]
189    #[get(skip)]
190    pub(crate) focused_node_key: Option<String>,
191    /// Active unsaved form draft fields keyed by field name.
192    #[new(skip)]
193    #[serde(default)]
194    pub(crate) form_draft: BTreeMap<String, String>,
195    /// Additional serializable component-local UI state.
196    #[new(skip)]
197    #[serde(default)]
198    pub(crate) local_data: Value,
199}
200
201/// Immutable route subtree snapshot captured at business revision `R` before background migration.
202#[derive(Clone, Data, Debug, Default, Deserialize, New, PartialEq, Serialize)]
203pub struct NestedStateSnapshot {
204    /// Normalized route path owning this snapshot.
205    pub(crate) route: String,
206    /// Release generation active when this snapshot was captured.
207    #[get(type(copy))]
208    pub(crate) base_generation: u64,
209    /// Global authoritative business store revision `R` at snapshot time.
210    #[get(type(copy))]
211    pub(crate) business_revision: u64,
212    /// Read-only per-entity `(revision, state)` views captured at `R`.
213    #[new(skip)]
214    #[serde(default)]
215    pub(crate) entity_snapshots: BTreeMap<String, (u64, Value)>,
216    /// Captured per-instance UI state snapshots keyed by canonical `StableAddress`.
217    #[new(skip)]
218    #[serde(default)]
219    pub(crate) ui_states: BTreeMap<String, ComponentUiStateSnapshot>,
220}
221
222/// Default pure background state migrator supporting identity pass-through and explicit key renames.
223#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
224pub struct DefaultStateMigrator {
225    /// Explicit `(from_schema, to_schema) -> (old_field -> new_field)` key mapping table.
226    pub(crate) field_renames: BTreeMap<(u32, u32), BTreeMap<String, String>>,
227    /// Explicit `(from_schema, to_schema) -> (field_name -> default_value)` additions.
228    pub(crate) default_additions: BTreeMap<(u32, u32), BTreeMap<String, String>>,
229}
230
231/// Frame-boundary safety probe deciding whether an atomic swap may commit without disrupting interaction.
232#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
233pub struct InteractionSafePoint {
234    /// Whether a pointer drag, scroll fling, or gesture capture is active.
235    #[get(type(copy))]
236    pub(crate) active_gesture: bool,
237    /// Whether an IME composition session is currently open.
238    #[get(type(copy))]
239    pub(crate) ime_composing: bool,
240    /// Whether a route or overlay transition animation is running.
241    #[get(type(copy))]
242    pub(crate) active_transition: bool,
243    /// Number of non-interruptible blocking UI transactions currently running.
244    #[get(type(copy))]
245    pub(crate) blocking_in_flight_tx: usize,
246    /// Remaining frame budget in microseconds before the next VSync deadline.
247    #[get(type(copy))]
248    pub(crate) frame_budget_remaining_us: u64,
249}