Revert breaking changes to WASM API
This commit is contained in:
parent
1b20037053
commit
0abc9a8b45
@ -86,7 +86,6 @@ type APIResult<T> = Result<T, veilid_core::VeilidAPIError>;
|
||||
const APIRESULT_UNDEFINED: APIResult<()> = APIResult::Ok(());
|
||||
|
||||
pub fn wrap_api_future_json<F, T>(future: F) -> Promise
|
||||
// Result<String, VeilidAPIError>
|
||||
where
|
||||
F: Future<Output = APIResult<T>> + 'static,
|
||||
T: Serialize + Debug + 'static,
|
||||
@ -95,7 +94,6 @@ where
|
||||
}
|
||||
|
||||
pub fn wrap_api_future_plain<F, T>(future: F) -> Promise
|
||||
// Result<T, VeilidAPIError>
|
||||
where
|
||||
F: Future<Output = APIResult<T>> + 'static,
|
||||
JsValue: From<T>,
|
||||
@ -105,7 +103,6 @@ where
|
||||
}
|
||||
|
||||
pub fn wrap_api_future_void<F>(future: F) -> Promise
|
||||
// Result<(), VeilidAPIError>
|
||||
where
|
||||
F: Future<Output = APIResult<()>> + '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,24 +222,13 @@ 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);
|
||||
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_jsvalue(update),
|
||||
) {
|
||||
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));
|
||||
@ -252,37 +242,45 @@ pub async fn startup_veilid_core(
|
||||
}
|
||||
|
||||
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(())
|
||||
APIRESULT_UNDEFINED
|
||||
})
|
||||
}
|
||||
|
||||
#[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 core_state = veilid_api.get_state().await?;
|
||||
Ok(core_state)
|
||||
APIResult::Ok(core_state)
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
pub async fn attach() -> Result<(), VeilidAPIError> {
|
||||
pub fn attach() -> Promise {
|
||||
wrap_api_future_void(async move {
|
||||
let veilid_api = get_veilid_api()?;
|
||||
veilid_api.attach().await?;
|
||||
Ok(())
|
||||
APIRESULT_UNDEFINED
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
pub async fn detach() -> Result<(), VeilidAPIError> {
|
||||
pub fn detach() -> Promise {
|
||||
wrap_api_future_void(async move {
|
||||
let veilid_api = get_veilid_api()?;
|
||||
veilid_api.detach().await?;
|
||||
Ok(())
|
||||
APIRESULT_UNDEFINED
|
||||
})
|
||||
}
|
||||
|
||||
#[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()?;
|
||||
veilid_api.shutdown().await;
|
||||
Ok(())
|
||||
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<u32, VeilidAPIError> {
|
||||
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);
|
||||
Ok(new_id)
|
||||
APIResult::Ok(new_id)
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
@ -1447,10 +1447,12 @@ pub fn now() -> u64 {
|
||||
}
|
||||
|
||||
#[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 out = veilid_api.debug(command).await?;
|
||||
APIResult::Ok(out)
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
|
Loading…
Reference in New Issue
Block a user