allow passing promise<messageport>

This commit is contained in:
Toshit Chawda 2024-08-30 17:56:51 -07:00
parent a9e23bdb5b
commit f351f2e9a6
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
2 changed files with 12 additions and 8 deletions

View file

@ -98,8 +98,8 @@ export interface BareResponseFetch extends BareResponse {
export class BareMuxConnection { export class BareMuxConnection {
worker: WorkerConnection; worker: WorkerConnection;
constructor(workerPath: string) { constructor(worker: string | Promise<MessagePort> | MessagePort) {
this.worker = new WorkerConnection(workerPath); this.worker = new WorkerConnection(worker);
} }
async getTransport(): Promise<string> { async getTransport(): Promise<string> {
@ -168,7 +168,7 @@ export class BareClient {
/** /**
* Create a BareClient. Calls to fetch and connect will wait for an implementation to be ready. * Create a BareClient. Calls to fetch and connect will wait for an implementation to be ready.
*/ */
constructor(worker?: string | MessagePort) { constructor(worker?: string | Promise<MessagePort> | MessagePort) {
this.worker = new WorkerConnection(worker); this.worker = new WorkerConnection(worker);
} }

View file

@ -47,10 +47,14 @@ async function searchForPort(): Promise<MessagePort> {
await testPort(port); await testPort(port);
return port; return port;
}); });
const promise: Promise<MessagePort> = Promise.race([Promise.any(promises), new Promise((_, reject) => setTimeout(reject, 1000, new TypeError("timeout")))]) as Promise<MessagePort>; const promise: Promise<MessagePort> = Promise.race([
Promise.any(promises),
new Promise((_, reject) => setTimeout(reject, 1000, new TypeError("timeout")))
]) as Promise<MessagePort>;
try { try {
return await promise; return await promise;
} catch(err) { } catch (err) {
if (err instanceof AggregateError) { if (err instanceof AggregateError) {
console.error("bare-mux: failed to get a bare-mux SharedWorker MessagePort as all clients returned an invalid MessagePort."); console.error("bare-mux: failed to get a bare-mux SharedWorker MessagePort as all clients returned an invalid MessagePort.");
throw new Error("All clients returned an invalid MessagePort."); throw new Error("All clients returned an invalid MessagePort.");
@ -108,7 +112,7 @@ export function browserSupportsTransferringStreams(): boolean {
try { try {
chan.port1.postMessage(stream, [stream]); chan.port1.postMessage(stream, [stream]);
res = true; res = true;
} catch(err) { } catch (err) {
res = false; res = false;
} }
browserSupportsTransferringStreamsCache = res; browserSupportsTransferringStreamsCache = res;
@ -123,9 +127,9 @@ export class WorkerConnection {
port: MessagePort | Promise<MessagePort>; port: MessagePort | Promise<MessagePort>;
workerPath: string; workerPath: string;
constructor(worker?: string | MessagePort) { constructor(worker?: string | Promise<MessagePort> | MessagePort) {
this.channel = new BroadcastChannel("bare-mux"); this.channel = new BroadcastChannel("bare-mux");
if (worker instanceof MessagePort) { if (worker instanceof MessagePort || worker instanceof Promise) {
this.port = worker; this.port = worker;
} else { } else {
this.createChannel(worker, true); this.createChannel(worker, true);