mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -04:00
allow transferables while setting client
This commit is contained in:
parent
646a521e49
commit
d750186782
3 changed files with 15 additions and 9 deletions
|
@ -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> {
|
||||||
|
|
|
@ -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 = {
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue