make it return inner port without awaiting

This commit is contained in:
Toshit Chawda 2024-07-09 11:53:07 -07:00
parent 7bd51538c8
commit 61dfcf6514
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
2 changed files with 6 additions and 14 deletions

View file

@ -59,18 +59,10 @@ const ws = client.createWebSocket("wss://echo.websocket.events");
## WebWorker support ## 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. 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 ```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(); const connection = new Ultraviolet.BareMuxConnection();
let port; let port = connection.getInnerPort();
connection.getInnerPort().then((MessagePort) => { // this could be a promise, but right now it's only a promise when called inside a service worker
port = MessagePort; if (port instanceof Promise) port = await port;
});
// ... transfer it to worker ... // ... transfer it to worker ...
this.bareClient = new BareClient(port) this.bareClient = new BareClient(port)
``` ```

View file

@ -125,8 +125,8 @@ export class BareMuxConnection {
}); });
} }
async getInnerPort(): Promise<MessagePort> { getInnerPort(): MessagePort | Promise<MessagePort> {
return await this.worker.port; return this.worker.port;
} }
} }