Skip to main content

origin_types/
events.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Event emission trait — shared by daemon (NoopEmitter) and Tauri app (TauriEmitter).
3//!
4//! Allows origin-core to emit UI events without depending on tauri.
5//! The Tauri app supplies its own implementation that wraps `tauri::Emitter`;
6//! tests and headless operation use `NoopEmitter`.
7
8use anyhow::Result;
9
10/// Trait for emitting events from core to external consumers.
11///
12/// Replaces `Option<tauri::AppHandle>` in MemoryDB and other modules that
13/// previously needed to push updates to the Tauri frontend.
14pub trait EventEmitter: Send + Sync {
15    /// Emit a named event with a string payload (typically JSON).
16    fn emit(&self, event: &str, payload: &str) -> Result<()>;
17}
18
19/// No-op emitter for testing and headless operation.
20///
21/// Always returns `Ok(())` without doing anything.
22pub struct NoopEmitter;
23
24impl EventEmitter for NoopEmitter {
25    fn emit(&self, _event: &str, _payload: &str) -> Result<()> {
26        Ok(())
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn noop_emitter_always_ok() {
36        let emitter = NoopEmitter;
37        assert!(emitter.emit("test-event", "{}").is_ok());
38        assert!(emitter.emit("", "").is_ok());
39    }
40
41    #[test]
42    fn noop_emitter_is_send_sync() {
43        fn assert_send_sync<T: Send + Sync>() {}
44        assert_send_sync::<NoopEmitter>();
45    }
46}