euv-core 0.48.3

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Stable monotonically assigned identifier for a host-owned business operation.
#[derive(
    Clone,
    Copy,
    Data,
    Debug,
    Default,
    Deserialize,
    Eq,
    Hash,
    New,
    Ord,
    PartialEq,
    PartialOrd,
    Serialize,
)]
pub struct OperationId {
    /// Raw monotonic operation sequence value.
    #[get(type(copy))]
    pub(crate) raw: u64,
}

/// Typed command dispatched by a page or dynamic component to mutate host-owned authoritative state.
#[derive(Clone, Data, Debug, Deserialize, New, PartialEq, Serialize)]
pub struct BusinessCommand {
    /// Target business entity identifier (e.g. `"order:42"`).
    pub(crate) entity_id: String,
    /// Strongly-named business command verb (e.g. `"SetQuantity"`, `"SaveOrder"`).
    pub(crate) command_name: String,
    /// Optional optimistic concurrency revision that must match live state before commit.
    #[new(skip)]
    #[serde(default)]
    #[get(skip)]
    pub(crate) expected_revision: Option<u64>,
    /// Caller-supplied idempotency key reused across retries to prevent duplicate side effects.
    #[new(skip)]
    #[serde(default)]
    pub(crate) idempotency_key: String,
    /// Release generation active on the originating route when this command was issued.
    #[new(skip)]
    #[serde(default)]
    #[get(type(copy))]
    pub(crate) origin_generation: u64,
    /// Typed JSON payload carrying command arguments.
    pub(crate) payload: Value,
}

/// One incremental authoritative state delta retained by `BusinessStateStore` for `R -> R+n` catch-up.
#[derive(Clone, Data, Debug, Deserialize, New, PartialEq, Serialize)]
pub struct BusinessStateDelta {
    /// Target business entity identifier.
    pub(crate) entity_id: String,
    /// Post-commit authoritative revision number.
    #[get(type(copy))]
    pub(crate) revision: u64,
    /// Stable operation identifier that produced this delta.
    #[get(type(copy))]
    pub(crate) operation_id: OperationId,
    /// Field names modified by this commit for field-filtered subscriber dispatch.
    pub(crate) changed_fields: Vec<String>,
    /// Post-commit authoritative state snapshot.
    pub(crate) state_patch: Value,
}

/// Host-tracked in-flight business operation surviving component hot-swaps and rollbacks.
#[derive(Clone, Data, Debug, Deserialize, New, PartialEq, Serialize)]
pub struct InFlightOperation {
    /// Stable operation identifier assigned upon host acceptance.
    #[get(type(copy))]
    pub(crate) operation_id: OperationId,
    /// Target business entity identifier.
    pub(crate) entity_id: String,
    /// Command verb being executed.
    pub(crate) command_name: String,
    /// Deduplication idempotency key.
    pub(crate) idempotency_key: String,
    /// Release generation that initiated this operation.
    #[get(type(copy))]
    pub(crate) origin_generation: u64,
    /// Optional expected revision captured when accepted.
    #[get(skip)]
    pub(crate) expected_revision: Option<u64>,
    /// Whether this operation has already reached completion.
    #[get(type(copy))]
    pub(crate) completed: bool,
}

/// Field-filtered subscription record bound to a specific release generation.
#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
pub struct StateSubscription {
    /// Unique subscription identifier.
    #[get(type(copy))]
    pub(crate) subscription_id: u64,
    /// Watched business entity identifier.
    pub(crate) entity_id: String,
    /// Release generation owning this subscriber (cancelled when generation unmounts).
    #[get(type(copy))]
    pub(crate) generation_id: u64,
    /// Subset of field names this subscriber cares about (empty means all fields).
    pub(crate) watched_fields: Vec<String>,
    /// Whether this subscription is still active.
    #[new(skip)]
    #[get(type(copy))]
    pub(crate) active: bool,
    /// Revisions delivered to this subscriber.
    #[new(skip)]
    pub(crate) delivered_revisions: Vec<u64>,
}

/// Authoritative record for a single host-owned business entity.
#[derive(Clone, Debug)]
pub(crate) struct EntityStateRecord {
    /// Current authoritative revision number.
    pub(crate) revision: u64,
    /// Current authoritative JSON state.
    pub(crate) state: Value,
    /// Bounded recent delta history for `R -> R+n` catch-up.
    pub(crate) deltas: VecDeque<BusinessStateDelta>,
}

/// Interior mutex-protected state of `BusinessStateStore`.
#[derive(Debug)]
pub(crate) struct BusinessStateStoreInner {
    /// Maximum delta entries kept per entity.
    pub(crate) delta_cap: usize,
    /// Monotonic operation ID generator.
    pub(crate) next_op_id: u64,
    /// Monotonic subscription ID generator.
    pub(crate) next_sub_id: u64,
    /// Monotonic global revision across all entities.
    pub(crate) global_revision: u64,
    /// Authoritative entity state map keyed by entity ID.
    pub(crate) entities: BTreeMap<String, EntityStateRecord>,
    /// Completed idempotency outcomes keyed by `(entity_id, idempotency_key)`.
    pub(crate) idempotency_cache: HashMap<(String, String), CommandOutcome>,
    /// Active in-flight host-owned operations keyed by `OperationId`.
    pub(crate) in_flight: BTreeMap<OperationId, InFlightOperation>,
    /// Active field-filtered subscriptions keyed by `subscription_id`.
    pub(crate) subscriptions: BTreeMap<u64, StateSubscription>,
}

/// Thread-safe host-owned authoritative business state store surviving hot-swaps and rollbacks.
#[derive(Clone, Debug)]
pub struct BusinessStateStore {
    /// Shared interior store state.
    pub(crate) inner: Arc<Mutex<BusinessStateStoreInner>>,
}

/// Stable nested instance identity (`route#tab/entity/slot[/child]`) immune to arena or render index changes.
#[derive(
    Clone, Data, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
)]
pub struct StableAddress {
    /// Stable route instance identifier (e.g. `"A#tab-1"`).
    pub(crate) route_instance: String,
    /// Ordered ancestor logical business keys (e.g. `["order:42", "editor"]`).
    pub(crate) ancestor_keys: Vec<String>,
    /// Logical slot key within the parent component (e.g. `"address"`).
    pub(crate) slot_key: String,
    /// Optional stable child business key (never a row number or render index).
    #[get(skip)]
    #[serde(default)]
    pub(crate) child_key: Option<String>,
}

/// Declarative per-instance UI interaction state captured at revision `R` for background migration.
#[derive(Clone, Data, Debug, Default, Deserialize, New, PartialEq, Serialize)]
pub struct ComponentUiStateSnapshot {
    /// Stable nested address identifying this component instance.
    pub(crate) address: StableAddress,
    /// Local state schema version declared by the component.
    #[get(type(copy))]
    pub(crate) schema_version: u32,
    /// Optional business-ID scroll anchor key.
    #[new(skip)]
    #[serde(default)]
    #[get(skip)]
    pub(crate) scroll_anchor_key: Option<String>,
    /// Logical scroll offset relative to `scroll_anchor_key`.
    #[new(skip)]
    #[serde(default)]
    #[get(type(copy))]
    pub(crate) scroll_offset_y: f32,
    /// Optional focused logical node key.
    #[new(skip)]
    #[serde(default)]
    #[get(skip)]
    pub(crate) focused_node_key: Option<String>,
    /// Active unsaved form draft fields keyed by field name.
    #[new(skip)]
    #[serde(default)]
    pub(crate) form_draft: BTreeMap<String, String>,
    /// Additional serializable component-local UI state.
    #[new(skip)]
    #[serde(default)]
    pub(crate) local_data: Value,
}

/// Immutable route subtree snapshot captured at business revision `R` before background migration.
#[derive(Clone, Data, Debug, Default, Deserialize, New, PartialEq, Serialize)]
pub struct NestedStateSnapshot {
    /// Normalized route path owning this snapshot.
    pub(crate) route: String,
    /// Release generation active when this snapshot was captured.
    #[get(type(copy))]
    pub(crate) base_generation: u64,
    /// Global authoritative business store revision `R` at snapshot time.
    #[get(type(copy))]
    pub(crate) business_revision: u64,
    /// Read-only per-entity `(revision, state)` views captured at `R`.
    #[new(skip)]
    #[serde(default)]
    pub(crate) entity_snapshots: BTreeMap<String, (u64, Value)>,
    /// Captured per-instance UI state snapshots keyed by canonical `StableAddress`.
    #[new(skip)]
    #[serde(default)]
    pub(crate) ui_states: BTreeMap<String, ComponentUiStateSnapshot>,
}

/// Default pure background state migrator supporting identity pass-through and explicit key renames.
#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
pub struct DefaultStateMigrator {
    /// Explicit `(from_schema, to_schema) -> (old_field -> new_field)` key mapping table.
    pub(crate) field_renames: BTreeMap<(u32, u32), BTreeMap<String, String>>,
    /// Explicit `(from_schema, to_schema) -> (field_name -> default_value)` additions.
    pub(crate) default_additions: BTreeMap<(u32, u32), BTreeMap<String, String>>,
}

/// Frame-boundary safety probe deciding whether an atomic swap may commit without disrupting interaction.
#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
pub struct InteractionSafePoint {
    /// Whether a pointer drag, scroll fling, or gesture capture is active.
    #[get(type(copy))]
    pub(crate) active_gesture: bool,
    /// Whether an IME composition session is currently open.
    #[get(type(copy))]
    pub(crate) ime_composing: bool,
    /// Whether a route or overlay transition animation is running.
    #[get(type(copy))]
    pub(crate) active_transition: bool,
    /// Number of non-interruptible blocking UI transactions currently running.
    #[get(type(copy))]
    pub(crate) blocking_in_flight_tx: usize,
    /// Remaining frame budget in microseconds before the next VSync deadline.
    #[get(type(copy))]
    pub(crate) frame_budget_remaining_us: u64,
}