Make CollabVMClient#send use typescript magic to allow passing any type

This allows passing any type (though practically, this is only ever going to include numbers) into it. This removes the need to constantly .toString() (though booleans are still kind of awful, but I guess we could factor that out somehow)
This commit is contained in:
modeco80 2024-03-11 23:40:43 -04:00
parent 72f321f689
commit 4dd5c42d55

View File

@ -9,6 +9,15 @@ import GetKeysym from '../keyboard.js';
import VoteStatus from "./VoteStatus.js"; import VoteStatus from "./VoteStatus.js";
import MuteState from "./MuteState.js"; import MuteState from "./MuteState.js";
// TODO: `Object` has a toString(), but we should probably gate that off
/// Interface for things that can be turned into strings
interface ToStringable {
toString() : string
}
/// A type for strings, or things that can (in a valid manner) be turned into strings
type StringLike = string | ToStringable;
export default class CollabVMClient { export default class CollabVMClient {
// Fields // Fields
private socket : WebSocket; private socket : WebSocket;
@ -318,8 +327,15 @@ export default class CollabVMClient {
} }
// Sends a message to the server // Sends a message to the server
send(...args : string[]) { send(...args : StringLike[]) {
this.socket.send(Guacutils.encode(...args)); let guacElements = [...args].map((el) => {
// This catches cases where the thing already is a string
if(el instanceof String)
return (el as string);
return el.toString();
});
this.socket.send(Guacutils.encode(...guacElements));
} }
// Get a list of all VMs // Get a list of all VMs
@ -388,12 +404,12 @@ export default class CollabVMClient {
// Send mouse instruction // Send mouse instruction
sendmouse(x : number, y : number, mask : number) { sendmouse(x : number, y : number, mask : number) {
this.send("mouse", x.toString(), y.toString(), mask.toString()); this.send("mouse", x, y, mask);
} }
// Send key // Send key
key(keysym : number, down : boolean) { key(keysym : number, down : boolean) {
this.send("key", keysym.toString(), down ? "1" : "0"); this.send("key", keysym, down ? "1" : "0");
} }
// Get vote status // Get vote status
@ -458,7 +474,7 @@ export default class CollabVMClient {
// Mute user // Mute user
mute(user : string, state : MuteState) { mute(user : string, state : MuteState) {
this.send("admin", AdminOpcode.MuteUser, user, state.toString()); this.send("admin", AdminOpcode.MuteUser, user, state);
} }
// Grab IP // Grab IP