Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/blackaslight/requester
- Owner: BlackAsLight
- License: mit
- Created: 2024-03-21T06:15:05.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-03-21T07:04:43.000Z (10 months ago)
- Last Synced: 2024-04-20T08:44:52.002Z (9 months ago)
- Topics: browser, communication, deno, javascript, network, typescript, worker
- Language: TypeScript
- Homepage: https://jsr.io/@doctor/requester
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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)
}
```