allow transferables while setting client

This commit is contained in:
Toshit Chawda 2024-07-16 18:23:33 -07:00
parent 646a521e49
commit d750186782
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
3 changed files with 15 additions and 9 deletions

View file

@ -105,18 +105,21 @@ export class BareMuxConnection {
return (await this.worker.sendMessage({ type: "get" })).name; return (await this.worker.sendMessage({ type: "get" })).name;
} }
async setTransport(path: string, options: any[]) { async setTransport(path: string, options: any[], transferables?: Transferable[]) {
await this.setManualTransport(` await this.setManualTransport(`
const { default: BareTransport } = await import("${path}"); const { default: BareTransport } = await import("${path}");
return [new BareTransport(${options.map(x => JSON.stringify(x)).join(", ")}), "${path}"]; return [BareTransport, "${path}"];
`); `, options, transferables);
} }
async setManualTransport(functionBody: string) { async setManualTransport(functionBody: string, options: any[], transferables?: Transferable[]) {
await this.worker.sendMessage({ await this.worker.sendMessage({
type: "set", type: "set",
client: functionBody, client: {
}); function: functionBody,
args: options,
},
}, transferables);
} }
getInnerPort(): MessagePort | Promise<MessagePort> { getInnerPort(): MessagePort | Promise<MessagePort> {

View file

@ -17,7 +17,10 @@ export type WorkerMessage = {
requestHeaders: BareHeaders, requestHeaders: BareHeaders,
channel: MessagePort, channel: MessagePort,
}, },
client?: string, client?: {
function: string,
args: any[],
},
}; };
export type WorkerRequest = { export type WorkerRequest = {

View file

@ -25,9 +25,9 @@ function handleConnection(port: MessagePort) {
const AsyncFunction = (async function() { }).constructor; const AsyncFunction = (async function() { }).constructor;
// @ts-expect-error // @ts-expect-error
const func = new AsyncFunction(message.client); const func = new AsyncFunction(message.client.function);
const [newTransport, name] = await func(); const [newTransport, name] = await func();
currentTransport = newTransport; currentTransport = new newTransport(...message.client.args);
currentTransportName = name; currentTransportName = name;
console.log("set transport to ", currentTransport, name); console.log("set transport to ", currentTransport, name);