kovlet 0.1.0

Kovlet's portable sync client core: sign-in, sync, merge, outbox and files for Rust, Tauri, Swift and Kotlin apps
Documentation
//! The sync engine: per-space merge state ([`target`]) and the WebSocket sessions.

pub(crate) mod session;
pub mod target;

use std::sync::Arc;

use crate::api::Api;
use crate::auth::Auth;
use crate::files::Staging;
use crate::hlc::Clock;

pub use session::{Blocked, Event, SpaceStatus, SyncState};
pub use target::v2::SubscriptionStatus;

/// Spawns a background task as a boxed future: tokio's task machinery is
/// then instantiated once for all of them, not once per task type, which
/// keeps the mobile binaries within their size budget (ADR 0017 ยง1).
pub(crate) fn spawn(
    handle: &tokio::runtime::Handle,
    f: impl std::future::Future<Output = ()> + Send + 'static,
) -> tokio::task::JoinHandle<()> {
    let f: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>> = Box::pin(f);
    handle.spawn(f)
}

/// Runs blocking work on the pool as a boxed closure (one instantiation per
/// result type; see [`spawn`]).
pub(crate) fn spawn_blocking<R: Send + 'static>(
    handle: &tokio::runtime::Handle,
    f: impl FnOnce() -> R + Send + 'static,
) -> tokio::task::JoinHandle<R> {
    let f: Box<dyn FnOnce() -> R + Send> = Box::new(f);
    handle.spawn_blocking(f)
}

/// What every space's session shares with the client.
pub(crate) struct Ctx {
    pub handle: tokio::runtime::Handle,
    pub auth: Arc<Auth>,
    pub api: Api,
    pub clock: Arc<Clock>,
    pub events: tokio::sync::broadcast::Sender<Event>,
    pub staging: Staging,
    /// Client-wide bookkeeping (`meta_*`): the last-known spaces list.
    pub meta: Arc<dyn crate::store::SyncStore>,
    /// The sync protocol: `None` negotiates (2, falling back to 1), `Some(1)`
    /// or `Some(2)` pins it.
    pub protocol: Option<u32>,
    /// Protocol 2: a machine session acts as this end user (`hello.as_end_user`).
    pub as_end_user: Option<String>,
}