From 4aa33971b440f38efdb3757389d30caa2ee4d9e8 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Mon, 9 Oct 2023 18:24:41 -0400 Subject: [PATCH] Fix server streaming codegen --- usdpl-build/src/front/service_generator.rs | 51 +++++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/usdpl-build/src/front/service_generator.rs b/usdpl-build/src/front/service_generator.rs index d3dd5f9..a4201f1 100644 --- a/usdpl-build/src/front/service_generator.rs +++ b/usdpl-build/src/front/service_generator.rs @@ -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); + } + } } } }