mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 06:40:02 -04:00
make it return inner port without awaiting
This commit is contained in:
parent
7bd51538c8
commit
61dfcf6514
2 changed files with 6 additions and 14 deletions
16
README.md
16
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)
|
||||
```
|
||||
```
|
||||
|
|
|
@ -125,8 +125,8 @@ export class BareMuxConnection {
|
|||
});
|
||||
}
|
||||
|
||||
async getInnerPort(): Promise<MessagePort> {
|
||||
return await this.worker.port;
|
||||
getInnerPort(): MessagePort | Promise<MessagePort> {
|
||||
return this.worker.port;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue