From 0abc9a8b457a332d9f4d474713f4fff8f0b4d0fb Mon Sep 17 00:00:00 2001 From: Brandon Vandegrift <798832-bmv437@users.noreply.gitlab.com> Date: Fri, 18 Aug 2023 10:34:40 -0400 Subject: [PATCH] Revert breaking changes to WASM API --- veilid-wasm/src/lib.rs | 124 +++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 61 deletions(-) diff --git a/veilid-wasm/src/lib.rs b/veilid-wasm/src/lib.rs index 76501d54..015fe026 100644 --- a/veilid-wasm/src/lib.rs +++ b/veilid-wasm/src/lib.rs @@ -86,7 +86,6 @@ type APIResult = Result; const APIRESULT_UNDEFINED: APIResult<()> = APIResult::Ok(()); pub fn wrap_api_future_json(future: F) -> Promise -// Result where F: Future> + 'static, T: Serialize + Debug + 'static, @@ -95,7 +94,6 @@ where } pub fn wrap_api_future_plain(future: F) -> Promise -// Result where F: Future> + 'static, JsValue: From, @@ -105,7 +103,6 @@ where } pub fn wrap_api_future_void(future: F) -> Promise -// Result<(), VeilidAPIError> where F: Future> + 'static, { @@ -157,10 +154,13 @@ pub fn initialize_veilid_wasm() { static INITIALIZED: AtomicBool = AtomicBool::new(false); #[wasm_bindgen()] -pub fn initialize_veilid_core(platform_config: VeilidWASMConfig) { +pub fn initialize_veilid_core(platform_config: String) { if INITIALIZED.swap(true, Ordering::Relaxed) { return; } + let platform_config: VeilidWASMConfig = veilid_core::deserialize_json(&platform_config) + .expect("failed to deserialize platform config json"); + // Set up subscriber and layers let subscriber = Registry::default(); let mut layers = Vec::new(); @@ -201,8 +201,9 @@ pub fn initialize_veilid_core(platform_config: VeilidWASMConfig) { } #[wasm_bindgen()] -pub fn change_log_level(layer: String, log_level: VeilidConfigLogLevel) { +pub fn change_log_level(layer: String, log_level: String) { let layer = if layer == "all" { "".to_owned() } else { layer }; + let log_level: veilid_core::VeilidConfigLogLevel = deserialize_json(&log_level).unwrap(); let filters = (*FILTERS).borrow(); if layer.is_empty() { // Change all layers @@ -221,68 +222,65 @@ const IUPDATE_VEILID_FUNCTION: &'static str = r#" type UpdateVeilidFunction = (event: VeilidUpdate) => void; "#; -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(extends = Function, typescript_type = "UpdateVeilidFunction")] - pub type UpdateVeilidFunction; -} - -#[wasm_bindgen] -pub async fn startup_veilid_core( - update_callback_js: UpdateVeilidFunction, - json_config: String, -) -> Result<(), VeilidAPIError> { +#[wasm_bindgen()] +pub fn startup_veilid_core(update_callback_js: Function, json_config: String) -> Promise { let update_callback_js = SendWrapper::new(update_callback_js); - let update_callback = Arc::new(move |update: VeilidUpdate| { - let _ret = match Function::call1( - &update_callback_js, - &JsValue::UNDEFINED, - &to_jsvalue(update), - ) { - Ok(v) => v, - Err(e) => { - console_log(&format!("calling update callback failed: {:?}", e)); - return; - } - }; - }); + wrap_api_future_void(async move { + let update_callback = Arc::new(move |update: VeilidUpdate| { + let _ret = + match Function::call1(&update_callback_js, &JsValue::UNDEFINED, &to_json(update)) { + Ok(v) => v, + Err(e) => { + console_log(&format!("calling update callback failed: {:?}", e)); + return; + } + }; + }); - if VEILID_API.borrow().is_some() { - return Err(veilid_core::VeilidAPIError::AlreadyInitialized); - } + if VEILID_API.borrow().is_some() { + return Err(veilid_core::VeilidAPIError::AlreadyInitialized); + } - let veilid_api = veilid_core::api_startup_json(update_callback, json_config).await?; - // veilid_core::api_startup(update_callback, config_callback) - VEILID_API.replace(Some(veilid_api)); - Ok(()) + let veilid_api = veilid_core::api_startup_json(update_callback, json_config).await?; + VEILID_API.replace(Some(veilid_api)); + APIRESULT_UNDEFINED + }) } #[wasm_bindgen()] -pub async fn get_veilid_state() -> Result { - let veilid_api = get_veilid_api()?; - let core_state = veilid_api.get_state().await?; - Ok(core_state) +pub fn get_veilid_state() -> Promise { + wrap_api_future_json(async move { + let veilid_api = get_veilid_api()?; + let core_state = veilid_api.get_state().await?; + APIResult::Ok(core_state) + }) } #[wasm_bindgen()] -pub async fn attach() -> Result<(), VeilidAPIError> { - let veilid_api = get_veilid_api()?; - veilid_api.attach().await?; - Ok(()) +pub fn attach() -> Promise { + wrap_api_future_void(async move { + let veilid_api = get_veilid_api()?; + veilid_api.attach().await?; + APIRESULT_UNDEFINED + }) } #[wasm_bindgen()] -pub async fn detach() -> Result<(), VeilidAPIError> { - let veilid_api = get_veilid_api()?; - veilid_api.detach().await?; - Ok(()) +pub fn detach() -> Promise { + wrap_api_future_void(async move { + let veilid_api = get_veilid_api()?; + veilid_api.detach().await?; + APIRESULT_UNDEFINED + }) } #[wasm_bindgen()] -pub async fn shutdown_veilid_core() -> Result<(), VeilidAPIError> { - let veilid_api = take_veilid_api()?; - veilid_api.shutdown().await; - Ok(()) +pub fn shutdown_veilid_core() -> Promise { + wrap_api_future_void(async move { + let veilid_api = take_veilid_api()?; + veilid_api.shutdown().await; + APIRESULT_UNDEFINED + }) } fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 { @@ -296,11 +294,13 @@ fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 { } #[wasm_bindgen()] -pub async fn routing_context() -> Result { - let veilid_api = get_veilid_api()?; - let routing_context = veilid_api.routing_context(); - let new_id = add_routing_context(routing_context); - Ok(new_id) +pub fn routing_context() -> Promise { + wrap_api_future_plain(async move { + let veilid_api = get_veilid_api()?; + let routing_context = veilid_api.routing_context(); + let new_id = add_routing_context(routing_context); + APIResult::Ok(new_id) + }) } #[wasm_bindgen()] @@ -1447,10 +1447,12 @@ pub fn now() -> u64 { } #[wasm_bindgen()] -pub async fn debug(command: String) -> Result { - let veilid_api = get_veilid_api()?; - let out = veilid_api.debug(command).await?; - APIResult::Ok(out) +pub fn debug(command: String) -> Promise { + wrap_api_future_plain(async move { + let veilid_api = get_veilid_api()?; + let out = veilid_api.debug(command).await?; + APIResult::Ok(out) + }) } #[wasm_bindgen()]