mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -04:00
allow passing promise<messageport>
This commit is contained in:
parent
a9e23bdb5b
commit
f351f2e9a6
2 changed files with 12 additions and 8 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue