Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/blackaslight/requester

This module offers a simple function to connect the disjointed sending and receiving of messages between two parties.
https://github.com/blackaslight/requester

browser communication deno javascript network typescript worker

Last synced: 2 months ago
JSON representation

This module offers a simple function to connect the disjointed sending and receiving of messages between two parties.

Awesome Lists containing this project

README

        

# Requester

Requester is a simple lib offering a method to connect disconnected communication between two environments like workers
for example. Basically joining the connections between sending and receiving information.

## Example
The below example connects a Client and a ShareWorker together so the Client can request information from the ShareWorker and
await a response.
```ts
import { createRequester } from '@doctor/requester'

const port = new SharedWorker('path').port

const { post, request, onMessage } = createRequester(
payload => port.postMessage(payload), // Handle Sending Messages
data => console.log(data) // Handle Responding to Messages
)

port.onmessage = ({ data: { data: WorkerMessage } }) => onMessage(data)

console.log(await request('123')) // 123
```
```ts
import createRequester from '@doctor/requester'

const { onMessage } = createRequester(
(payload, port) => port.postMessage(payload) // Handle Sending Messages
data => Number(data) // Handle Responding to Messages
)

self.onconnect = function({ ports: [port] }: MessageEvent) {
port.onmessage = ({ data: { data: WorkerMessage } }) => onMessage(data, port)
}
```