use origin_app::Application;
use origin_events::{PlatformEvent, RecvError};
use std::sync::Arc;
use tauri::{AppHandle, Emitter};
pub(crate) const PLATFORM_EVENT: &str = "origin://platform-event";
pub(crate) fn forward_platform_events(app: &AppHandle, application: Arc<Application>) {
let Ok(mut stream) = application.platform().events.subscribe::<PlatformEvent>() else {
tracing::error!("cannot subscribe to platform events, ui will not receive updates");
return;
};
let app = app.clone();
tauri::async_runtime::spawn(async move {
loop {
match stream.recv().await {
Ok(event) => {
if let Err(error) = app.emit(PLATFORM_EVENT, &event) {
tracing::warn!(%error, "cannot forward platform event to the webview");
}
}
Err(RecvError::Lagged(missed)) => {
tracing::warn!(missed, "ui event bridge fell behind");
}
Err(RecvError::Closed) => break,
}
}
tracing::debug!("platform event bridge stopped");
});
}