From 61dfcf65146e2fdbbc7f930dd5a69cccccdf9b6a Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Tue, 9 Jul 2024 11:53:07 -0700 Subject: [PATCH] make it return inner port without awaiting --- README.md | 16 ++++------------ src/client.ts | 4 ++-- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 8a58054..7b3f03a 100644 --- a/README.md +++ b/README.md @@ -59,18 +59,10 @@ const ws = client.createWebSocket("wss://echo.websocket.events"); ## WebWorker support Due to limitations in browsers, there is no way for bare-mux to get a connection to the bare-mux SharedWorker while inside a WebWorker. Proxies that use bare-mux must manually pass in a MessagePort to the SharedWorker to be able to use BareClient in a WebWorker. ```js -const connection = new BareMuxConnection(); -const port = await connection.getInnerPort(); -// ... transfer it to worker ... -const client = new BareClient(port); -``` -```js -// doing this synchronously const connection = new Ultraviolet.BareMuxConnection(); -let port; -connection.getInnerPort().then((MessagePort) => { - port = MessagePort; -}); +let port = connection.getInnerPort(); +// this could be a promise, but right now it's only a promise when called inside a service worker +if (port instanceof Promise) port = await port; // ... transfer it to worker ... this.bareClient = new BareClient(port) -``` \ No newline at end of file +``` diff --git a/src/client.ts b/src/client.ts index eb221f2..e4006eb 100644 --- a/src/client.ts +++ b/src/client.ts @@ -125,8 +125,8 @@ export class BareMuxConnection { }); } - async getInnerPort(): Promise { - return await this.worker.port; + getInnerPort(): MessagePort | Promise { + return this.worker.port; } }