1use crate::host;
2use crate::plugin_runtime::{PluginCtx, on_event};
3use crate::plugin_stdio::PluginError;
4use serde_json::Value;
5
6pub fn subscribe_host(name: &str) -> Result<(), PluginError> {
7 let _ = host::call("events.subscribe", serde_json::json!({ "name": name }))?;
8 Ok(())
9}
10
11pub fn subscribe(
12 name: &'static str,
13 mut handler: impl FnMut(Value) -> Result<(), PluginError> + Send + 'static,
14) -> Result<(), PluginError> {
15 on_event(name, move |_ctx: &mut PluginCtx, payload: Value| {
16 handler(payload)
17 });
18 subscribe_host(name)
19}
20
21pub fn emit(name: &str, payload: Value) -> Result<(), PluginError> {
22 let _ = host::call(
23 "events.emit",
24 serde_json::json!({ "name": name, "payload": payload }),
25 )?;
26 Ok(())
27}