mtp/wasm/src/subscription.rs
Alois 5caa1c9d5f
Some checks failed
CI / rustfmt (push) Successful in 17s
CI / wasm build (push) Successful in 1m16s
CI / clippy (push) Successful in 1m28s
CI / test (push) Successful in 1m48s
CI / example (push) Successful in 1m31s
CI / duplicate code (push) Failing after 33s
CI / web client (push) Failing after 34s
CI / cargo-machete (push) Successful in 1m18s
CI / cargo-deny (push) Failing after 3m2s
(feat): redesign WASM module, add TypeScript SDK, migrate to pnpm
2026-06-27 23:44:27 +02:00

44 lines
1.1 KiB
Rust

use std::collections::HashMap;
use wasm_bindgen::prelude::*;
/// Minimal message router used by higher-level SDK subscription code.
#[wasm_bindgen]
pub struct WasmSubscriptionRouter {
handlers: HashMap<String, js_sys::Function>,
}
#[wasm_bindgen]
impl WasmSubscriptionRouter {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
handlers: HashMap::new(),
}
}
#[wasm_bindgen]
pub fn subscribe(&mut self, message_type: String, callback: js_sys::Function) {
self.handlers.insert(message_type, callback);
}
#[wasm_bindgen]
pub fn unsubscribe(&mut self, message_type: &str) -> bool {
self.handlers.remove(message_type).is_some()
}
#[wasm_bindgen]
pub fn dispatch(&self, message_type: &str, message: JsValue) -> bool {
let Some(callback) = self.handlers.get(message_type) else {
return false;
};
let _ = callback.call1(&JsValue::NULL, &message);
true
}
}
impl Default for WasmSubscriptionRouter {
fn default() -> Self {
Self::new()
}
}