Revert breaking changes to WASM API

This commit is contained in:
Brandon Vandegrift 2023-08-18 10:34:40 -04:00
parent 1b20037053
commit 0abc9a8b45

View File

@ -86,7 +86,6 @@ type APIResult<T> = Result<T, veilid_core::VeilidAPIError>;
const APIRESULT_UNDEFINED: APIResult<()> = APIResult::Ok(()); const APIRESULT_UNDEFINED: APIResult<()> = APIResult::Ok(());
pub fn wrap_api_future_json<F, T>(future: F) -> Promise pub fn wrap_api_future_json<F, T>(future: F) -> Promise
// Result<String, VeilidAPIError>
where where
F: Future<Output = APIResult<T>> + 'static, F: Future<Output = APIResult<T>> + 'static,
T: Serialize + Debug + 'static, T: Serialize + Debug + 'static,
@ -95,7 +94,6 @@ where
} }
pub fn wrap_api_future_plain<F, T>(future: F) -> Promise pub fn wrap_api_future_plain<F, T>(future: F) -> Promise
// Result<T, VeilidAPIError>
where where
F: Future<Output = APIResult<T>> + 'static, F: Future<Output = APIResult<T>> + 'static,
JsValue: From<T>, JsValue: From<T>,
@ -105,7 +103,6 @@ where
} }
pub fn wrap_api_future_void<F>(future: F) -> Promise pub fn wrap_api_future_void<F>(future: F) -> Promise
// Result<(), VeilidAPIError>
where where
F: Future<Output = APIResult<()>> + 'static, F: Future<Output = APIResult<()>> + 'static,
{ {
@ -157,10 +154,13 @@ pub fn initialize_veilid_wasm() {
static INITIALIZED: AtomicBool = AtomicBool::new(false); static INITIALIZED: AtomicBool = AtomicBool::new(false);
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn initialize_veilid_core(platform_config: VeilidWASMConfig) { pub fn initialize_veilid_core(platform_config: String) {
if INITIALIZED.swap(true, Ordering::Relaxed) { if INITIALIZED.swap(true, Ordering::Relaxed) {
return; return;
} }
let platform_config: VeilidWASMConfig = veilid_core::deserialize_json(&platform_config)
.expect("failed to deserialize platform config json");
// Set up subscriber and layers // Set up subscriber and layers
let subscriber = Registry::default(); let subscriber = Registry::default();
let mut layers = Vec::new(); let mut layers = Vec::new();
@ -201,8 +201,9 @@ pub fn initialize_veilid_core(platform_config: VeilidWASMConfig) {
} }
#[wasm_bindgen()] #[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 layer = if layer == "all" { "".to_owned() } else { layer };
let log_level: veilid_core::VeilidConfigLogLevel = deserialize_json(&log_level).unwrap();
let filters = (*FILTERS).borrow(); let filters = (*FILTERS).borrow();
if layer.is_empty() { if layer.is_empty() {
// Change all layers // Change all layers
@ -221,68 +222,65 @@ const IUPDATE_VEILID_FUNCTION: &'static str = r#"
type UpdateVeilidFunction = (event: VeilidUpdate) => void; type UpdateVeilidFunction = (event: VeilidUpdate) => void;
"#; "#;
#[wasm_bindgen] #[wasm_bindgen()]
extern "C" { pub fn startup_veilid_core(update_callback_js: Function, json_config: String) -> Promise {
#[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> {
let update_callback_js = SendWrapper::new(update_callback_js); let update_callback_js = SendWrapper::new(update_callback_js);
let update_callback = Arc::new(move |update: VeilidUpdate| { wrap_api_future_void(async move {
let _ret = match Function::call1( let update_callback = Arc::new(move |update: VeilidUpdate| {
&update_callback_js, let _ret =
&JsValue::UNDEFINED, match Function::call1(&update_callback_js, &JsValue::UNDEFINED, &to_json(update)) {
&to_jsvalue(update), Ok(v) => v,
) { Err(e) => {
Ok(v) => v, console_log(&format!("calling update callback failed: {:?}", e));
Err(e) => { return;
console_log(&format!("calling update callback failed: {:?}", e)); }
return; };
} });
};
});
if VEILID_API.borrow().is_some() { if VEILID_API.borrow().is_some() {
return Err(veilid_core::VeilidAPIError::AlreadyInitialized); return Err(veilid_core::VeilidAPIError::AlreadyInitialized);
} }
let veilid_api = veilid_core::api_startup_json(update_callback, json_config).await?; 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));
VEILID_API.replace(Some(veilid_api)); APIRESULT_UNDEFINED
Ok(()) })
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn get_veilid_state() -> Result<VeilidState, VeilidAPIError> { pub fn get_veilid_state() -> Promise {
let veilid_api = get_veilid_api()?; wrap_api_future_json(async move {
let core_state = veilid_api.get_state().await?; let veilid_api = get_veilid_api()?;
Ok(core_state) let core_state = veilid_api.get_state().await?;
APIResult::Ok(core_state)
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn attach() -> Result<(), VeilidAPIError> { pub fn attach() -> Promise {
let veilid_api = get_veilid_api()?; wrap_api_future_void(async move {
veilid_api.attach().await?; let veilid_api = get_veilid_api()?;
Ok(()) veilid_api.attach().await?;
APIRESULT_UNDEFINED
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn detach() -> Result<(), VeilidAPIError> { pub fn detach() -> Promise {
let veilid_api = get_veilid_api()?; wrap_api_future_void(async move {
veilid_api.detach().await?; let veilid_api = get_veilid_api()?;
Ok(()) veilid_api.detach().await?;
APIRESULT_UNDEFINED
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn shutdown_veilid_core() -> Result<(), VeilidAPIError> { pub fn shutdown_veilid_core() -> Promise {
let veilid_api = take_veilid_api()?; wrap_api_future_void(async move {
veilid_api.shutdown().await; let veilid_api = take_veilid_api()?;
Ok(()) veilid_api.shutdown().await;
APIRESULT_UNDEFINED
})
} }
fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 { 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()] #[wasm_bindgen()]
pub async fn routing_context() -> Result<u32, VeilidAPIError> { pub fn routing_context() -> Promise {
let veilid_api = get_veilid_api()?; wrap_api_future_plain(async move {
let routing_context = veilid_api.routing_context(); let veilid_api = get_veilid_api()?;
let new_id = add_routing_context(routing_context); let routing_context = veilid_api.routing_context();
Ok(new_id) let new_id = add_routing_context(routing_context);
APIResult::Ok(new_id)
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
@ -1447,10 +1447,12 @@ pub fn now() -> u64 {
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn debug(command: String) -> Result<String, VeilidAPIError> { pub fn debug(command: String) -> Promise {
let veilid_api = get_veilid_api()?; wrap_api_future_plain(async move {
let out = veilid_api.debug(command).await?; let veilid_api = get_veilid_api()?;
APIResult::Ok(out) let out = veilid_api.debug(command).await?;
APIResult::Ok(out)
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]