oxide_core 0.4.0

Rust engine primitives for Oxide (store, snapshot streams, error model, optional persistence).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::sync::{Arc, Mutex as StdMutex};

use tokio::sync::{Mutex, mpsc, watch};

use crate::engine::{Context, CoreResult, InitContext, OxideError, Reducer, StateChange, StateSnapshot};

#[cfg(feature = "navigation-binding")]
use crate::engine::{NavigationCtx, navigation_runtime};

#[cfg(feature = "state-persistence")]
use crate::persistence::{
    FilePersistenceWorker, PersistenceConfig, decode, default_persistence_path, encode,
    try_read_bytes,
};

// Core reducer runtime.
//
// Why: Oxide needs a single place that defines the transactional update semantics
// (commit vs no-op vs error) so every binding layer sees identical behavior.
//
// How: Guard state behind a mutex, broadcast snapshots through a watch channel,
// and run side-effects in a background loop that uses the same commit rule.
struct EngineState<R, StateSlice>
where
    R: Reducer<StateSlice>,
    StateSlice: Copy + PartialEq + Eq + Send + Sync + 'static,
{
    state: R::State,
    revision: u64,
    reducer: R,
}

#[cfg(feature = "state-persistence")]
struct PersistenceHooks<S> {
    worker: FilePersistenceWorker,
    encode: Box<dyn Fn(&S) -> CoreResult<Vec<u8>> + Send + Sync>,
}

struct Shared<R, StateSlice>
where
    R: Reducer<StateSlice>,
    StateSlice: Copy + PartialEq + Eq + Send + Sync + 'static,
{
    state: Mutex<EngineState<R, StateSlice>>,
    tx: watch::Sender<StateSnapshot<R::State, StateSlice>>,
    error_tx: watch::Sender<Option<OxideError>>,
    sideeffect_tx: mpsc::UnboundedSender<R::SideEffect>,
    #[cfg(feature = "frb-spawn")]
    _sideeffect_task: StdMutex<Option<flutter_rust_bridge::JoinHandle<()>>>,
    #[cfg(feature = "state-persistence")]
    persistence: Option<PersistenceHooks<R::State>>,
}

/// A reducer-driven engine that owns state, broadcasts snapshots, and runs side-effects.
///
/// `ReducerEngine` is the core runtime primitive used by Oxide's generated FRB surface:
///
/// - [`dispatch`](Self::dispatch) applies an action via [`Reducer::reduce`], updates state, and
///   broadcasts a [`StateSnapshot`] to subscribers.
/// - Side-effects can be enqueued by the reducer (via [`Reducer::init`]) or manually via
///   [`sideeffect_sender`](Self::sideeffect_sender); they are processed in a background task.
///
/// ## Update rule (clone-first semantics)
///
/// For both actions and side-effects, the engine applies updates transactionally:
///
/// 1. Clone the current state.
/// 2. Run reducer logic against the clone.
/// 3. If the reducer returns an error, discard the clone (state is unchanged).
/// 4. If the reducer returns [`StateChange::None`], discard the clone (no snapshot emitted).
/// 5. If the reducer returns [`StateChange::Full`], commit the clone and emit a snapshot.
///
/// # Concurrency
/// Actions are applied serially (behind an internal mutex). Snapshot updates are broadcast via
/// a Tokio [`tokio::sync::watch`] channel, so new subscribers immediately receive the latest snapshot.
///
/// # Runtime requirements
/// Creating an engine spawns a background task via Flutter Rust Bridge.
/// Call `initOxide()` from Dart (after `RustLib.init()`) before creating engines.
pub struct ReducerEngine<R, StateSlice = ()>
where
    R: Reducer<StateSlice>,
    StateSlice: Copy + PartialEq + Eq + Send + Sync + 'static,
{
    shared: Arc<Shared<R, StateSlice>>,
}

impl<R, StateSlice> Clone for ReducerEngine<R, StateSlice>
where
    R: Reducer<StateSlice>,
    StateSlice: Copy + PartialEq + Eq + Send + Sync + 'static,
{
    fn clone(&self) -> Self {
        Self {
            shared: Arc::clone(&self.shared),
        }
    }
}

impl<R, StateSlice> ReducerEngine<R, StateSlice>
where
    R: Reducer<StateSlice>,
    StateSlice: Copy + PartialEq + Eq + Send + Sync + 'static,
{
    /// Creates a new engine with the given reducer and initial state.
    pub async fn new(reducer: R, initial_state: R::State) -> CoreResult<Self> {
        Self::new_inner(reducer, initial_state, None).await
    }

    #[cfg(feature = "state-persistence")]
    /// Creates an engine that persists state snapshots to disk.
    ///
    /// When persistence is enabled, every emitted snapshot is encoded and queued for writing
    /// by a background persistence worker (using `config.min_interval` for write throttling).
    pub async fn new_persistent(
        reducer: R,
        initial_state: R::State,
        config: PersistenceConfig,
    ) -> CoreResult<Self>
    where
        R::State: crate::serde::Serialize + crate::serde::de::DeserializeOwned,
    {
        let path = default_persistence_path(&config.key);
        let restored_bytes = {
            #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
            {
                let read_path = path.clone();
                crate::runtime::spawn_blocking(move || try_read_bytes(&read_path))
                    .await
                    .ok()
                    .flatten()
            }

            #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
            {
                try_read_bytes(&path)
            }
        };
        let restored = restored_bytes.and_then(|bytes| decode(&bytes).ok());
        let state = restored.unwrap_or(initial_state);
        let worker = FilePersistenceWorker::new(path, config.min_interval)?;
        let persistence = PersistenceHooks {
            worker,
            encode: Box::new(|state| encode(state)),
        };
        Self::new_inner(reducer, state, Some(persistence)).await
    }

    async fn new_inner(
        mut reducer: R,
        initial_state: R::State,
        #[cfg(feature = "state-persistence")] persistence: Option<PersistenceHooks<R::State>>,
        #[cfg(not(feature = "state-persistence"))] _persistence: Option<()>,
    ) -> CoreResult<Self> {
        crate::runtime::ensure_initialized()?;

        let initial_snapshot = StateSnapshot {
            revision: 0,
            state: initial_state.clone(),
            slices: Vec::new(),
        };
        let (tx, _rx) = watch::channel(initial_snapshot);
        let (error_tx, _error_rx) = watch::channel::<Option<OxideError>>(None);

        let (sideeffect_tx, sideeffect_rx) = mpsc::unbounded_channel::<R::SideEffect>();
        let init_ctx = InitContext {
            sideeffect_tx: sideeffect_tx.clone(),
            #[cfg(feature = "frb-spawn")]
            thread_pool: crate::runtime::thread_pool()?,
        };
        reducer.init(init_ctx).await;

        let shared = Arc::new(Shared {
            state: Mutex::new(EngineState {
                state: initial_state,
                revision: 0,
                reducer,
            }),
            tx,
            error_tx,
            sideeffect_tx: sideeffect_tx.clone(),
            #[cfg(feature = "frb-spawn")]
            _sideeffect_task: StdMutex::new(None),
            #[cfg(feature = "state-persistence")]
            persistence,
        });

        #[cfg(feature = "frb-spawn")]
        {
            let handle = crate::runtime::spawn(sideeffect_loop(Arc::clone(&shared), sideeffect_rx));
            let mut slot = shared
                ._sideeffect_task
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            *slot = Some(handle);
        }

        Ok(Self { shared })
    }

    /// Returns a sender that can be used to enqueue side-effects for background processing.
    pub fn sideeffect_sender(&self) -> mpsc::UnboundedSender<R::SideEffect> {
        self.shared.sideeffect_tx.clone()
    }

    /// Dispatches an action and returns the resulting snapshot.
    ///
    /// If the reducer reports [`StateChange::None`], the current snapshot is returned and no
    /// new snapshot is broadcast.
    ///
    /// # Errors
    /// Returns any error produced by the reducer.
    pub async fn dispatch(
        &self,
        action: R::Action,
    ) -> CoreResult<StateSnapshot<R::State, StateSlice>> {
        let mut state = self.shared.state.lock().await;
        let before_snapshot = StateSnapshot {
            revision: state.revision,
            state: state.state.clone(),
            slices: Vec::new(),
        };

        #[cfg(feature = "navigation-binding")]
        let (runtime, route_ctx) = {
            let runtime = navigation_runtime()?;
            let route_ctx = runtime.current_route_context();
            (runtime, route_ctx)
        };

        // Apply reducer logic against a cloned state so failures never partially
        // mutate the committed state.
        let mut next_state = state.state.clone();
        let ctx = Context {
            input: &action,
            state_snapshot: &before_snapshot,
            #[cfg(feature = "navigation-binding")]
            nav: NavigationCtx::new(runtime, &route_ctx),
        };
        let change = state.reducer.reduce(&mut next_state, ctx)?;

        match change {
            // Why: "no externally-visible change" should not spam watchers.
            StateChange::None => Ok(before_snapshot),
            StateChange::Full => {
                state.state = next_state;
                state.revision = state.revision.saturating_add(1);

                let snapshot = StateSnapshot {
                    revision: state.revision,
                    state: state.state.clone(),
                    slices: Vec::new(),
                };
                let _ = self.shared.tx.send(snapshot.clone());
                self.persist_if_enabled(&snapshot);
                Ok(snapshot)
            }
            StateChange::Infer => {
                let slices = state.reducer.infer_slices(&state.state, &next_state);

                state.state = next_state;
                state.revision = state.revision.saturating_add(1);

                let snapshot = StateSnapshot {
                    revision: state.revision,
                    state: state.state.clone(),
                    slices,
                };
                let _ = self.shared.tx.send(snapshot.clone());
                self.persist_if_enabled(&snapshot);
                Ok(snapshot)
            }
            StateChange::Slices(slices) => {
                state.state = next_state;
                state.revision = state.revision.saturating_add(1);

                let snapshot = StateSnapshot {
                    revision: state.revision,
                    state: state.state.clone(),
                    slices: slices.to_vec(),
                };
                let _ = self.shared.tx.send(snapshot.clone());
                self.persist_if_enabled(&snapshot);
                Ok(snapshot)
            }
        }
    }

    /// Returns the current snapshot without dispatching an action.
    pub async fn current(&self) -> StateSnapshot<R::State, StateSlice> {
        let state = self.shared.state.lock().await;
        StateSnapshot {
            revision: state.revision,
            state: state.state.clone(),
            slices: Vec::new(),
        }
    }

    /// Subscribes to snapshot updates.
    ///
    /// The returned receiver immediately contains the latest snapshot and will be notified on
    /// every subsequent committed update.
    pub fn subscribe(&self) -> watch::Receiver<StateSnapshot<R::State, StateSlice>> {
        self.shared.tx.subscribe()
    }

    /// Subscribes to engine errors that cannot be returned from the originating call site.
    ///
    /// This includes:
    /// - background side-effect failures (`Reducer::effect`)
    /// - persistence encoding failures (when `state-persistence` is enabled)
    /// - missing runtime dependencies (e.g., navigation runtime not initialized)
    pub fn subscribe_errors(&self) -> watch::Receiver<Option<OxideError>> {
        self.shared.error_tx.subscribe()
    }

    fn persist_if_enabled(&self, _snapshot: &StateSnapshot<R::State, StateSlice>) {
        #[cfg(feature = "state-persistence")]
        {
            if let Some(persistence) = &self.shared.persistence {
                match (persistence.encode)(&_snapshot.state) {
                    Ok(bytes) => persistence.worker.queue(bytes),
                    Err(err) => {
                        let _ = self.shared.error_tx.send(Some(err));
                    }
                }
            }
        }
    }
}

async fn sideeffect_loop<R, StateSlice>(
    shared: Arc<Shared<R, StateSlice>>,
    mut rx: mpsc::UnboundedReceiver<R::SideEffect>,
) where
    R: Reducer<StateSlice>,
    StateSlice: Copy + PartialEq + Eq + Send + Sync + 'static,
{
    while let Some(effect) = rx.recv().await {
        let mut state = shared.state.lock().await;
        let before_snapshot = StateSnapshot {
            revision: state.revision,
            state: state.state.clone(),
            slices: Vec::new(),
        };

        #[cfg(feature = "navigation-binding")]
        let (runtime, route_ctx) = match navigation_runtime() {
            Ok(runtime) => {
                let route_ctx = runtime.current_route_context();
                (runtime, route_ctx)
            }
            Err(err) => {
                let _ = shared.error_tx.send(Some(err));
                continue;
            }
        };
        let mut next_state = state.state.clone();
        let ctx = Context {
            input: &effect,
            state_snapshot: &before_snapshot,
            #[cfg(feature = "navigation-binding")]
            nav: NavigationCtx::new(runtime, &route_ctx),
        };
        let change = match state.reducer.effect(&mut next_state, ctx) {
            Ok(change) => change,
            Err(err) => {
                let _ = shared.error_tx.send(Some(err));
                continue;
            }
        };

        let slices: Vec<StateSlice> = match change {
            StateChange::None => {
                continue;
            }
            StateChange::Full => Vec::new(),
            StateChange::Infer => state.reducer.infer_slices(&state.state, &next_state),
            StateChange::Slices(slices) => slices.to_vec(),
        };

        state.state = next_state;
        state.revision = state.revision.saturating_add(1);

        let snapshot = StateSnapshot {
            revision: state.revision,
            state: state.state.clone(),
            slices,
        };
        let _ = shared.tx.send(snapshot.clone());

        #[cfg(feature = "state-persistence")]
        {
            if let Some(persistence) = &shared.persistence {
                match (persistence.encode)(&snapshot.state) {
                    Ok(bytes) => persistence.worker.queue(bytes),
                    Err(err) => {
                        let _ = shared.error_tx.send(Some(err));
                    }
                }
            }
        }
    }
}