Fix server streaming codegen

This commit is contained in:
NGnius (Graham) 2023-10-09 18:24:41 -04:00
parent 1ad6205067
commit 4aa33971b4

View file

@ -171,19 +171,38 @@ fn generate_service_methods(
gen_methods.push(quote::quote! {
#[wasm_bindgen]
pub async fn #method_name(&self, #(#input_params)*, callback: js_sys::Function) {
pub async fn #method_name(&self, #(#input_params)* callback: js_sys::Function) {
#params_to_fields_transformer
match self.service.#method_name(val.into()).await {
Ok(x) => {
Ok(mut x) => {
while let Some(next_result) = x.next().await {
match next_result {
Err(e) => {
log::error!("service:{}|method:{}|error:{}", self.service.descriptor(), #method_name_str, e);
},
Ok(item) => {
callback.call1(JsValue::undefined(), item.into_wasm_streamable());
#[inline(always)]
fn js_to_string(js: &JsValue) -> String {
if let Some(s) = js.as_string() {
s
} else {
format!("{:?}", js)
}
}
let item: #method_output = item.into();
match callback.call1(&JsValue::undefined(), &item.into_wasm_streamable()) {
Ok(js_val) => {
if !(js_val.is_undefined() && js_val.is_null()) {
let str_val = js_to_string(&js_val);
log::info!("service:{}|method:{}|callback result:{}", self.service.descriptor(), #method_name_str, str_val);
}
},
Err(js_e) => {
let str_e = js_to_string(&js_e);
log::error!("service:{}|method:{}|error:{}", self.service.descriptor(), #method_name_str, str_e);
}
}
}
}
}
@ -206,14 +225,34 @@ fn generate_service_methods(
let stream = Box::new(::usdpl_front::wasm::JsFunctionStream::<#method_input>::from_function(generator));
match self.service.#method_name(stream).await {
Ok(x) => {
Ok(mut x) => {
while let Some(next_result) = x.next().await {
match next_result {
Err(e) => {
log::error!("service:{}|method:{}|error:{}", self.service.descriptor(), #method_name_str, e);
},
Ok(item) => {
callback.call1(JsValue::undefined(), item.into_wasm_streamable());
#[inline(always)]
fn js_to_string(js: &JsValue) -> String {
if let Some(s) = js.as_string() {
s
} else {
format!("{:?}", js)
}
}
let item: #method_output = item.into();
match callback.call1(&JsValue::undefined(), &item.into_wasm_streamable()) {
Ok(js_val) => {
if !(js_val.is_undefined() && js_val.is_null()) {
let str_val = js_to_string(&js_val);
log::info!("service:{}|method:{}|callback result:{}", self.service.descriptor(), #method_name_str, str_val);
}
},
Err(js_e) => {
let str_e = js_to_string(&js_e);
log::error!("service:{}|method:{}|error:{}", self.service.descriptor(), #method_name_str, str_e);
}
}
}
}
}