mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -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
|
## 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)
|
||||||
```
|
```
|
||||||
|
|
|
@ -125,8 +125,8 @@ export class BareMuxConnection {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getInnerPort(): Promise<MessagePort> {
|
getInnerPort(): MessagePort | Promise<MessagePort> {
|
||||||
return await this.worker.port;
|
return this.worker.port;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue