diff --git a/src/generators/ts_c.rs b/src/generators/ts_c.rs index 3cb0276..770f672 100644 --- a/src/generators/ts_c.rs +++ b/src/generators/ts_c.rs @@ -34,10 +34,10 @@ pub fn method_args(method: &crate::data::MethodTy) -> String { } pub fn method_ret(method: &crate::data::MethodTy) -> String { - if method.ret_stream || method.ret.is_none() { + if method.ret_stream { String::new() } else { - format!(": Promise<{}>", ty_to_str(method.ret.as_ref().unwrap())) + format!(": Promise<{}>", method.ret.as_ref().map(ty_to_str).unwrap_or("void".into())) } } diff --git a/templates/cpp_server.rs.cxx b/templates/cpp_server.rs.cxx index 8866f64..fcc789b 100644 --- a/templates/cpp_server.rs.cxx +++ b/templates/cpp_server.rs.cxx @@ -89,8 +89,9 @@ void mrpc::MRPCServer::msg_handler(const std::shared_ptr __c, } @for (name, ty) in m.args.iter().map(|a| (&a.name, ty_to_str(&a.ty))) { @ty @name; @name << json_get(__data, "@name"); } - @if m.ret_stream || m.ret.is_none() {@(s.name)_@(m.name)(@call_args(m));} - else {send_msg(__c, @(s.name)_@(m.name)(@call_args(m)));} + @if m.ret_stream {@(s.name)_@(m.name)(@call_args(m));} + else if m.ret.is_some() {send_msg(__c, @(s.name)_@(m.name)(@call_args(m)));} + else {@(s.name)_@(m.name)(@call_args(m)); send_msg(__c, nullptr);} @}} else @{ throw std::exception@{@}; @} @}} diff --git a/templates/cpp_server_json.rs.cxx b/templates/cpp_server_json.rs.cxx index 5360aac..effd6a9 100644 --- a/templates/cpp_server_json.rs.cxx +++ b/templates/cpp_server_json.rs.cxx @@ -24,6 +24,10 @@ mrpc::MRPCJWriter& operator>>(const mrpc::@e.name &v, mrpc::MRPCJWriter &w) @{ return w; @} } +inline mrpc::MRPCJWriter& operator>>(const std::nullptr_t &, mrpc::MRPCJWriter &w) @{ + w.Null(); + return w; +@} template inline std::vector& operator<<(std::vector &v, const rapidjson::Value &j); diff --git a/templates/typescript_client.rs.ts b/templates/typescript_client.rs.ts index 505918b..45f09bd 100644 --- a/templates/typescript_client.rs.ts +++ b/templates/typescript_client.rs.ts @@ -36,8 +36,10 @@ export class MRPCConnector @{ else if m.ret_stream {fetchEventSource(this.url, @{ method: 'POST', body: JSON.stringify(__msg), - onmessage: __e => __cbk(JSON.parse(__e.data)) - @});} else {fetch(this.url, @{method: 'POST', body: JSON.stringify(__msg)@});} + onmessage: __e => __cbk(JSON.parse(__e.data)), + onerror: __e => @{throw __e;@}, + onclose: () => __cbk(null) + @});} else {return fetch(this.url, @{method: 'POST', body: JSON.stringify(__msg)@}).then(__r => @{@});} @} }} @}