Skip to content

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

License

Notifications You must be signed in to change notification settings

BlackAsLight/requester

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

import { createRequester } from '@doctor/requester'

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

const { post, request, onMessage } = createRequester<string>(
	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
import createRequester from '@doctor/requester'

const { onMessage } = createRequester<number, MessagePort>(
	(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)
}

About

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

Topics

Resources

License

Stars

Watchers

Forks