Remove WOS dependency from wshrpc (#329)

The frontend wshserver.ts had a weird circuitous dependency on wos.ts,
which was unnecessary. This moves the misplaced functions into wshrpc.ts
and updates the generation logic.
This commit is contained in:
Evan Simkowitz 2024-09-05 13:17:35 -07:00 committed by GitHub
parent 779d2d35b7
commit debbed7003
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 96 additions and 95 deletions

View File

@ -89,7 +89,7 @@ func generateWshServerFile(tsTypeMap map[reflect.Type]string) error {
fmt.Fprintf(fd, "// Copyright 2024, Command Line Inc.\n") fmt.Fprintf(fd, "// Copyright 2024, Command Line Inc.\n")
fmt.Fprintf(fd, "// SPDX-License-Identifier: Apache-2.0\n\n") fmt.Fprintf(fd, "// SPDX-License-Identifier: Apache-2.0\n\n")
fmt.Fprintf(fd, "// generated by cmd/generate/main-generate.go\n\n") fmt.Fprintf(fd, "// generated by cmd/generate/main-generate.go\n\n")
fmt.Fprintf(fd, "import * as WOS from \"./wos\";\n\n") fmt.Fprintf(fd, "import { wshServerRpcHelper_call, wshServerRpcHelper_responsestream } from \"./wshrpc\";\n\n")
orderedKeys := utilfn.GetOrderedMapKeys(declMap) orderedKeys := utilfn.GetOrderedMapKeys(declMap)
fmt.Fprintf(fd, "// WshServerCommandToDeclMap\n") fmt.Fprintf(fd, "// WshServerCommandToDeclMap\n")
fmt.Fprintf(fd, "class WshServerType {\n") fmt.Fprintf(fd, "class WshServerType {\n")

View File

@ -3,7 +3,6 @@
// WaveObjectStore // WaveObjectStore
import { sendRpcCommand } from "@/app/store/wshrpc";
import { getWebServerEndpoint } from "@/util/endpoints"; import { getWebServerEndpoint } from "@/util/endpoints";
import { fetch } from "@/util/fetchutil"; import { fetch } from "@/util/fetchutil";
import * as jotai from "jotai"; import * as jotai from "jotai";
@ -11,8 +10,6 @@ import * as React from "react";
import { atoms, globalStore } from "./global"; import { atoms, globalStore } from "./global";
import * as services from "./services"; import * as services from "./services";
const IsElectron = true;
type WaveObjectDataItemType<T extends WaveObj> = { type WaveObjectDataItemType<T extends WaveObj> = {
value: T; value: T;
loading: boolean; loading: boolean;
@ -125,53 +122,6 @@ function callBackendService(service: string, method: string, args: any[], noUICo
return prtn; return prtn;
} }
function wshServerRpcHelper_responsestream(
command: string,
data: any,
opts: RpcOpts
): AsyncGenerator<any, void, boolean> {
if (opts?.noresponse) {
throw new Error("noresponse not supported for responsestream calls");
}
const msg: RpcMessage = {
command: command,
data: data,
reqid: crypto.randomUUID(),
};
if (opts?.timeout) {
msg.timeout = opts.timeout;
}
if (opts?.route) {
msg.route = opts.route;
}
const rpcGen = sendRpcCommand(msg);
return rpcGen;
}
function wshServerRpcHelper_call(command: string, data: any, opts: RpcOpts): Promise<any> {
const msg: RpcMessage = {
command: command,
data: data,
};
if (!opts?.noresponse) {
msg.reqid = crypto.randomUUID();
}
if (opts?.timeout) {
msg.timeout = opts.timeout;
}
if (opts?.route) {
msg.route = opts.route;
}
const rpcGen = sendRpcCommand(msg);
if (rpcGen == null) {
return null;
}
const respMsgPromise = rpcGen.next(true); // pass true to force termination of rpc after 1 response (not streaming)
return respMsgPromise.then((msg: IteratorResult<any, void>) => {
return msg.value;
});
}
const waveObjectValueCache = new Map<string, WaveObjectValue<any>>(); const waveObjectValueCache = new Map<string, WaveObjectValue<any>>();
function clearWaveObjectCache() { function clearWaveObjectCache() {
@ -344,6 +294,4 @@ export {
updateWaveObject, updateWaveObject,
updateWaveObjects, updateWaveObjects,
useWaveObjectValue, useWaveObjectValue,
wshServerRpcHelper_call,
wshServerRpcHelper_responsestream,
}; };

View File

@ -12,6 +12,53 @@ type RpcEntry = {
const openRpcs = new Map<string, RpcEntry>(); const openRpcs = new Map<string, RpcEntry>();
function wshServerRpcHelper_responsestream(
command: string,
data: any,
opts: RpcOpts
): AsyncGenerator<any, void, boolean> {
if (opts?.noresponse) {
throw new Error("noresponse not supported for responsestream calls");
}
const msg: RpcMessage = {
command: command,
data: data,
reqid: crypto.randomUUID(),
};
if (opts?.timeout) {
msg.timeout = opts.timeout;
}
if (opts?.route) {
msg.route = opts.route;
}
const rpcGen = sendRpcCommand(msg);
return rpcGen;
}
function wshServerRpcHelper_call(command: string, data: any, opts: RpcOpts): Promise<any> {
const msg: RpcMessage = {
command: command,
data: data,
};
if (!opts?.noresponse) {
msg.reqid = crypto.randomUUID();
}
if (opts?.timeout) {
msg.timeout = opts.timeout;
}
if (opts?.route) {
msg.route = opts.route;
}
const rpcGen = sendRpcCommand(msg);
if (rpcGen == null) {
return null;
}
const respMsgPromise = rpcGen.next(true); // pass true to force termination of rpc after 1 response (not streaming)
return respMsgPromise.then((msg: IteratorResult<any, void>) => {
return msg.value;
});
}
async function* rpcResponseGenerator( async function* rpcResponseGenerator(
command: string, command: string,
reqid: string, reqid: string,
@ -148,4 +195,10 @@ if (globalThis.window != null) {
globalThis["consumeGenerator"] = consumeGenerator; globalThis["consumeGenerator"] = consumeGenerator;
} }
export { handleIncomingRpcMessage, sendRawRpcMessage, sendRpcCommand }; export {
handleIncomingRpcMessage,
sendRawRpcMessage,
sendRpcCommand,
wshServerRpcHelper_call,
wshServerRpcHelper_responsestream,
};

View File

@ -3,198 +3,198 @@
// generated by cmd/generate/main-generate.go // generated by cmd/generate/main-generate.go
import * as WOS from "./wos"; import { wshServerRpcHelper_call, wshServerRpcHelper_responsestream } from "./wshrpc";
// WshServerCommandToDeclMap // WshServerCommandToDeclMap
class WshServerType { class WshServerType {
// command "announce" [call] // command "announce" [call]
AnnounceCommand(data: string, opts?: RpcOpts): Promise<void> { AnnounceCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("announce", data, opts); return wshServerRpcHelper_call("announce", data, opts);
} }
// command "authenticate" [call] // command "authenticate" [call]
AuthenticateCommand(data: string, opts?: RpcOpts): Promise<CommandAuthenticateRtnData> { AuthenticateCommand(data: string, opts?: RpcOpts): Promise<CommandAuthenticateRtnData> {
return WOS.wshServerRpcHelper_call("authenticate", data, opts); return wshServerRpcHelper_call("authenticate", data, opts);
} }
// command "connconnect" [call] // command "connconnect" [call]
ConnConnectCommand(data: string, opts?: RpcOpts): Promise<void> { ConnConnectCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("connconnect", data, opts); return wshServerRpcHelper_call("connconnect", data, opts);
} }
// command "conndisconnect" [call] // command "conndisconnect" [call]
ConnDisconnectCommand(data: string, opts?: RpcOpts): Promise<void> { ConnDisconnectCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("conndisconnect", data, opts); return wshServerRpcHelper_call("conndisconnect", data, opts);
} }
// command "connensure" [call] // command "connensure" [call]
ConnEnsureCommand(data: string, opts?: RpcOpts): Promise<void> { ConnEnsureCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("connensure", data, opts); return wshServerRpcHelper_call("connensure", data, opts);
} }
// command "connreinstallwsh" [call] // command "connreinstallwsh" [call]
ConnReinstallWshCommand(data: string, opts?: RpcOpts): Promise<void> { ConnReinstallWshCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("connreinstallwsh", data, opts); return wshServerRpcHelper_call("connreinstallwsh", data, opts);
} }
// command "connstatus" [call] // command "connstatus" [call]
ConnStatusCommand(opts?: RpcOpts): Promise<ConnStatus[]> { ConnStatusCommand(opts?: RpcOpts): Promise<ConnStatus[]> {
return WOS.wshServerRpcHelper_call("connstatus", null, opts); return wshServerRpcHelper_call("connstatus", null, opts);
} }
// command "controllerinput" [call] // command "controllerinput" [call]
ControllerInputCommand(data: CommandBlockInputData, opts?: RpcOpts): Promise<void> { ControllerInputCommand(data: CommandBlockInputData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("controllerinput", data, opts); return wshServerRpcHelper_call("controllerinput", data, opts);
} }
// command "controllerresync" [call] // command "controllerresync" [call]
ControllerResyncCommand(data: CommandControllerResyncData, opts?: RpcOpts): Promise<void> { ControllerResyncCommand(data: CommandControllerResyncData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("controllerresync", data, opts); return wshServerRpcHelper_call("controllerresync", data, opts);
} }
// command "controllerstop" [call] // command "controllerstop" [call]
ControllerStopCommand(data: string, opts?: RpcOpts): Promise<void> { ControllerStopCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("controllerstop", data, opts); return wshServerRpcHelper_call("controllerstop", data, opts);
} }
// command "createblock" [call] // command "createblock" [call]
CreateBlockCommand(data: CommandCreateBlockData, opts?: RpcOpts): Promise<ORef> { CreateBlockCommand(data: CommandCreateBlockData, opts?: RpcOpts): Promise<ORef> {
return WOS.wshServerRpcHelper_call("createblock", data, opts); return wshServerRpcHelper_call("createblock", data, opts);
} }
// command "deleteblock" [call] // command "deleteblock" [call]
DeleteBlockCommand(data: CommandDeleteBlockData, opts?: RpcOpts): Promise<void> { DeleteBlockCommand(data: CommandDeleteBlockData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("deleteblock", data, opts); return wshServerRpcHelper_call("deleteblock", data, opts);
} }
// command "eventpublish" [call] // command "eventpublish" [call]
EventPublishCommand(data: WaveEvent, opts?: RpcOpts): Promise<void> { EventPublishCommand(data: WaveEvent, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("eventpublish", data, opts); return wshServerRpcHelper_call("eventpublish", data, opts);
} }
// command "eventreadhistory" [call] // command "eventreadhistory" [call]
EventReadHistoryCommand(data: CommandEventReadHistoryData, opts?: RpcOpts): Promise<WaveEvent[]> { EventReadHistoryCommand(data: CommandEventReadHistoryData, opts?: RpcOpts): Promise<WaveEvent[]> {
return WOS.wshServerRpcHelper_call("eventreadhistory", data, opts); return wshServerRpcHelper_call("eventreadhistory", data, opts);
} }
// command "eventrecv" [call] // command "eventrecv" [call]
EventRecvCommand(data: WaveEvent, opts?: RpcOpts): Promise<void> { EventRecvCommand(data: WaveEvent, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("eventrecv", data, opts); return wshServerRpcHelper_call("eventrecv", data, opts);
} }
// command "eventsub" [call] // command "eventsub" [call]
EventSubCommand(data: SubscriptionRequest, opts?: RpcOpts): Promise<void> { EventSubCommand(data: SubscriptionRequest, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("eventsub", data, opts); return wshServerRpcHelper_call("eventsub", data, opts);
} }
// command "eventunsub" [call] // command "eventunsub" [call]
EventUnsubCommand(data: string, opts?: RpcOpts): Promise<void> { EventUnsubCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("eventunsub", data, opts); return wshServerRpcHelper_call("eventunsub", data, opts);
} }
// command "eventunsuball" [call] // command "eventunsuball" [call]
EventUnsubAllCommand(opts?: RpcOpts): Promise<void> { EventUnsubAllCommand(opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("eventunsuball", null, opts); return wshServerRpcHelper_call("eventunsuball", null, opts);
} }
// command "fileappend" [call] // command "fileappend" [call]
FileAppendCommand(data: CommandFileData, opts?: RpcOpts): Promise<void> { FileAppendCommand(data: CommandFileData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("fileappend", data, opts); return wshServerRpcHelper_call("fileappend", data, opts);
} }
// command "fileappendijson" [call] // command "fileappendijson" [call]
FileAppendIJsonCommand(data: CommandAppendIJsonData, opts?: RpcOpts): Promise<void> { FileAppendIJsonCommand(data: CommandAppendIJsonData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("fileappendijson", data, opts); return wshServerRpcHelper_call("fileappendijson", data, opts);
} }
// command "fileread" [call] // command "fileread" [call]
FileReadCommand(data: CommandFileData, opts?: RpcOpts): Promise<string> { FileReadCommand(data: CommandFileData, opts?: RpcOpts): Promise<string> {
return WOS.wshServerRpcHelper_call("fileread", data, opts); return wshServerRpcHelper_call("fileread", data, opts);
} }
// command "filewrite" [call] // command "filewrite" [call]
FileWriteCommand(data: CommandFileData, opts?: RpcOpts): Promise<void> { FileWriteCommand(data: CommandFileData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("filewrite", data, opts); return wshServerRpcHelper_call("filewrite", data, opts);
} }
// command "getmeta" [call] // command "getmeta" [call]
GetMetaCommand(data: CommandGetMetaData, opts?: RpcOpts): Promise<MetaType> { GetMetaCommand(data: CommandGetMetaData, opts?: RpcOpts): Promise<MetaType> {
return WOS.wshServerRpcHelper_call("getmeta", data, opts); return wshServerRpcHelper_call("getmeta", data, opts);
} }
// command "message" [call] // command "message" [call]
MessageCommand(data: CommandMessageData, opts?: RpcOpts): Promise<void> { MessageCommand(data: CommandMessageData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("message", data, opts); return wshServerRpcHelper_call("message", data, opts);
} }
// command "remotefiledelete" [call] // command "remotefiledelete" [call]
RemoteFileDeleteCommand(data: string, opts?: RpcOpts): Promise<void> { RemoteFileDeleteCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("remotefiledelete", data, opts); return wshServerRpcHelper_call("remotefiledelete", data, opts);
} }
// command "remotefileinfo" [call] // command "remotefileinfo" [call]
RemoteFileInfoCommand(data: string, opts?: RpcOpts): Promise<FileInfo> { RemoteFileInfoCommand(data: string, opts?: RpcOpts): Promise<FileInfo> {
return WOS.wshServerRpcHelper_call("remotefileinfo", data, opts); return wshServerRpcHelper_call("remotefileinfo", data, opts);
} }
// command "remotefilejoin" [call] // command "remotefilejoin" [call]
RemoteFileJoinCommand(data: string[], opts?: RpcOpts): Promise<FileInfo> { RemoteFileJoinCommand(data: string[], opts?: RpcOpts): Promise<FileInfo> {
return WOS.wshServerRpcHelper_call("remotefilejoin", data, opts); return wshServerRpcHelper_call("remotefilejoin", data, opts);
} }
// command "remotestreamcpudata" [responsestream] // command "remotestreamcpudata" [responsestream]
RemoteStreamCpuDataCommand(opts?: RpcOpts): AsyncGenerator<TimeSeriesData, void, boolean> { RemoteStreamCpuDataCommand(opts?: RpcOpts): AsyncGenerator<TimeSeriesData, void, boolean> {
return WOS.wshServerRpcHelper_responsestream("remotestreamcpudata", null, opts); return wshServerRpcHelper_responsestream("remotestreamcpudata", null, opts);
} }
// command "remotestreamfile" [responsestream] // command "remotestreamfile" [responsestream]
RemoteStreamFileCommand(data: CommandRemoteStreamFileData, opts?: RpcOpts): AsyncGenerator<CommandRemoteStreamFileRtnData, void, boolean> { RemoteStreamFileCommand(data: CommandRemoteStreamFileData, opts?: RpcOpts): AsyncGenerator<CommandRemoteStreamFileRtnData, void, boolean> {
return WOS.wshServerRpcHelper_responsestream("remotestreamfile", data, opts); return wshServerRpcHelper_responsestream("remotestreamfile", data, opts);
} }
// command "remotewritefile" [call] // command "remotewritefile" [call]
RemoteWriteFileCommand(data: CommandRemoteWriteFileData, opts?: RpcOpts): Promise<void> { RemoteWriteFileCommand(data: CommandRemoteWriteFileData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("remotewritefile", data, opts); return wshServerRpcHelper_call("remotewritefile", data, opts);
} }
// command "resolveids" [call] // command "resolveids" [call]
ResolveIdsCommand(data: CommandResolveIdsData, opts?: RpcOpts): Promise<CommandResolveIdsRtnData> { ResolveIdsCommand(data: CommandResolveIdsData, opts?: RpcOpts): Promise<CommandResolveIdsRtnData> {
return WOS.wshServerRpcHelper_call("resolveids", data, opts); return wshServerRpcHelper_call("resolveids", data, opts);
} }
// command "setconfig" [call] // command "setconfig" [call]
SetConfigCommand(data: SettingsType, opts?: RpcOpts): Promise<void> { SetConfigCommand(data: SettingsType, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("setconfig", data, opts); return wshServerRpcHelper_call("setconfig", data, opts);
} }
// command "setmeta" [call] // command "setmeta" [call]
SetMetaCommand(data: CommandSetMetaData, opts?: RpcOpts): Promise<void> { SetMetaCommand(data: CommandSetMetaData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("setmeta", data, opts); return wshServerRpcHelper_call("setmeta", data, opts);
} }
// command "setview" [call] // command "setview" [call]
SetViewCommand(data: CommandBlockSetViewData, opts?: RpcOpts): Promise<void> { SetViewCommand(data: CommandBlockSetViewData, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("setview", data, opts); return wshServerRpcHelper_call("setview", data, opts);
} }
// command "streamcpudata" [responsestream] // command "streamcpudata" [responsestream]
StreamCpuDataCommand(data: CpuDataRequest, opts?: RpcOpts): AsyncGenerator<TimeSeriesData, void, boolean> { StreamCpuDataCommand(data: CpuDataRequest, opts?: RpcOpts): AsyncGenerator<TimeSeriesData, void, boolean> {
return WOS.wshServerRpcHelper_responsestream("streamcpudata", data, opts); return wshServerRpcHelper_responsestream("streamcpudata", data, opts);
} }
// command "streamtest" [responsestream] // command "streamtest" [responsestream]
StreamTestCommand(opts?: RpcOpts): AsyncGenerator<number, void, boolean> { StreamTestCommand(opts?: RpcOpts): AsyncGenerator<number, void, boolean> {
return WOS.wshServerRpcHelper_responsestream("streamtest", null, opts); return wshServerRpcHelper_responsestream("streamtest", null, opts);
} }
// command "streamwaveai" [responsestream] // command "streamwaveai" [responsestream]
StreamWaveAiCommand(data: OpenAiStreamRequest, opts?: RpcOpts): AsyncGenerator<OpenAIPacketType, void, boolean> { StreamWaveAiCommand(data: OpenAiStreamRequest, opts?: RpcOpts): AsyncGenerator<OpenAIPacketType, void, boolean> {
return WOS.wshServerRpcHelper_responsestream("streamwaveai", data, opts); return wshServerRpcHelper_responsestream("streamwaveai", data, opts);
} }
// command "test" [call] // command "test" [call]
TestCommand(data: string, opts?: RpcOpts): Promise<void> { TestCommand(data: string, opts?: RpcOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("test", data, opts); return wshServerRpcHelper_call("test", data, opts);
} }
} }

View File

@ -436,7 +436,7 @@ func GenerateWshServerMethod_ResponseStream(methodDecl *wshrpc.WshRpcMethodDecl,
} else { } else {
sb.WriteString(fmt.Sprintf(" %s(opts?: RpcOpts): %s {\n", methodDecl.MethodName, genRespType)) sb.WriteString(fmt.Sprintf(" %s(opts?: RpcOpts): %s {\n", methodDecl.MethodName, genRespType))
} }
sb.WriteString(fmt.Sprintf(" return WOS.wshServerRpcHelper_responsestream(%q, %s, opts);\n", methodDecl.Command, dataName)) sb.WriteString(fmt.Sprintf(" return wshServerRpcHelper_responsestream(%q, %s, opts);\n", methodDecl.Command, dataName))
sb.WriteString(" }\n") sb.WriteString(" }\n")
return sb.String() return sb.String()
} }
@ -459,7 +459,7 @@ func GenerateWshServerMethod_Call(methodDecl *wshrpc.WshRpcMethodDecl, tsTypesMa
} else { } else {
sb.WriteString(fmt.Sprintf(" %s(opts?: RpcOpts): %s {\n", methodDecl.MethodName, rtnType)) sb.WriteString(fmt.Sprintf(" %s(opts?: RpcOpts): %s {\n", methodDecl.MethodName, rtnType))
} }
methodBody := fmt.Sprintf(" return WOS.wshServerRpcHelper_call(%q, %s, opts);\n", methodDecl.Command, dataName) methodBody := fmt.Sprintf(" return wshServerRpcHelper_call(%q, %s, opts);\n", methodDecl.Command, dataName)
sb.WriteString(methodBody) sb.WriteString(methodBody)
sb.WriteString(" }\n") sb.WriteString(" }\n")
return sb.String() return sb.String()