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,24 +222,13 @@ 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);
wrap_api_future_void(async move {
let update_callback = Arc::new(move |update: VeilidUpdate| { let update_callback = Arc::new(move |update: VeilidUpdate| {
let _ret = match Function::call1( let _ret =
&update_callback_js, match Function::call1(&update_callback_js, &JsValue::UNDEFINED, &to_json(update)) {
&JsValue::UNDEFINED,
&to_jsvalue(update),
) {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
console_log(&format!("calling update callback failed: {:?}", e)); console_log(&format!("calling update callback failed: {:?}", e));
@ -252,37 +242,45 @@ pub async fn startup_veilid_core(
} }
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));
Ok(()) APIRESULT_UNDEFINED
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn get_veilid_state() -> Result<VeilidState, VeilidAPIError> { pub fn get_veilid_state() -> Promise {
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let core_state = veilid_api.get_state().await?; let core_state = veilid_api.get_state().await?;
Ok(core_state) APIResult::Ok(core_state)
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn attach() -> Result<(), VeilidAPIError> { pub fn attach() -> Promise {
wrap_api_future_void(async move {
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
veilid_api.attach().await?; veilid_api.attach().await?;
Ok(()) APIRESULT_UNDEFINED
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn detach() -> Result<(), VeilidAPIError> { pub fn detach() -> Promise {
wrap_api_future_void(async move {
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
veilid_api.detach().await?; veilid_api.detach().await?;
Ok(()) APIRESULT_UNDEFINED
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub async fn shutdown_veilid_core() -> Result<(), VeilidAPIError> { pub fn shutdown_veilid_core() -> Promise {
wrap_api_future_void(async move {
let veilid_api = take_veilid_api()?; let veilid_api = take_veilid_api()?;
veilid_api.shutdown().await; veilid_api.shutdown().await;
Ok(()) 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 {
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let routing_context = veilid_api.routing_context(); let routing_context = veilid_api.routing_context();
let new_id = add_routing_context(routing_context); let new_id = add_routing_context(routing_context);
Ok(new_id) 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 {
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let out = veilid_api.debug(command).await?; let out = veilid_api.debug(command).await?;
APIResult::Ok(out) APIResult::Ok(out)
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]