2023-10-01 11:15:12 +00:00
|
|
|
@use itertools::Itertools;
|
|
|
|
@use crate::data::RPC;
|
|
|
|
@use crate::generators::ts_c::*;
|
|
|
|
|
|
|
|
@(rpc: &RPC)
|
2023-10-02 13:41:00 +00:00
|
|
|
import @{ fetchEventSource @} from '@@microsoft/fetch-event-source';
|
2023-10-01 11:15:12 +00:00
|
|
|
@for e in &rpc.enums {
|
|
|
|
export enum @e.name @{
|
|
|
|
@for (k,v) in &e.values { @k = @v,
|
2023-10-02 13:41:00 +00:00
|
|
|
}@}
|
2023-10-01 11:15:12 +00:00
|
|
|
}
|
|
|
|
@for s in &rpc.structs {
|
|
|
|
export interface @s.name @{
|
|
|
|
@for f in &s.fields { @f.name: @ty_to_str(&f.ty);
|
2023-10-02 13:41:00 +00:00
|
|
|
}@}
|
2023-10-01 11:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class MRPCConnector @{
|
|
|
|
url: string;
|
|
|
|
|
2023-10-11 15:10:20 +00:00
|
|
|
private __create_msg(service: string, method: string, data: any) @{
|
|
|
|
return @{service, method, data@};
|
|
|
|
@}
|
|
|
|
|
2023-10-01 11:15:12 +00:00
|
|
|
public constructor(url: string) @{
|
|
|
|
this.url = url;
|
|
|
|
@}
|
|
|
|
|
|
|
|
@for s in &rpc.services { @for m in &s.methods {
|
|
|
|
public @(s.name)_@(m.name)(@method_args(m))@method_ret(m) @{
|
2023-10-11 15:10:20 +00:00
|
|
|
const __msg = this.__create_msg('@s.name', '@m.name', @{@m.args.iter().map(|a| &a.name).join(",")@});
|
2023-10-02 13:41:00 +00:00
|
|
|
@if m.ret.is_some() && !m.ret_stream {return fetch(this.url, @{
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(__msg)
|
|
|
|
@}).then((__r) => __r.json());}
|
|
|
|
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)@});}
|
2023-10-01 11:15:12 +00:00
|
|
|
@}
|
|
|
|
}}
|
|
|
|
@}
|