Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/illright/worker-request-response
A Promise API for submitting requests to workers and tracking responses.
https://github.com/illright/worker-request-response
promise service-workers web-workers
Last synced: about 2 months ago
JSON representation
A Promise API for submitting requests to workers and tracking responses.
- Host: GitHub
- URL: https://github.com/illright/worker-request-response
- Owner: illright
- License: isc
- Created: 2022-03-31T12:57:26.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-02T17:56:51.000Z (over 1 year ago)
- Last Synced: 2024-11-22T16:06:10.042Z (2 months ago)
- Topics: promise, service-workers, web-workers
- Language: TypeScript
- Homepage:
- Size: 49.8 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `worker-request-response` 👷♀️💬🗨️
[![npm version](https://img.shields.io/npm/v/worker-request-response)](https://www.npmjs.com/package/worker-request-response)
[![minzipped package size](https://img.shields.io/bundlephobia/minzip/worker-request-response.svg)](https://bundlephobia.com/package/worker-request-response)_Ever wish you could request some data from a service/web worker and have them respond to you asynchronously?_
A Promise API for submitting requests to workers and tracking responses.
## Installation
```bash
pnpm add worker-request-response
```Type definitions are built in 😎.
I don't use
pnpm
What do you mean "I don't use [`pnpm`](https://pnpm.io)"? It's so much faster! Alright, here's your `npm` command:
```bash
npm install --save worker-request-response
```## Usage
It has two functions exported:
* `sendRequest` for your main thread code
* `handleRequestsWith` for your worker codeHere's an example of a service worker that converts numbers to strings:
```ts
// In your main thread
import { sendRequest } from 'worker-request-response';export async function sendRequestToServiceWorker() {
const response = await sendRequest(navigator.serviceWorker.controller, 42);
console.log(response, 'is "42"');
}
``````ts
// In your worker
import { handleRequestsWith } from 'worker-request-response';async function processRequest(event: MessageEvent): Promise {
return event.data.toString();
}self.addEventListener('message', handleRequestsWith(processRequest));
```There, that simple. In glorious TypeScript, too.
### Usage with web workers
The `sendRequest` function accepts the worker object as the first argument. For service workers, you would pass `navigator.serviceWorker.controller` (checking for null beforehand). For web workers, you would pass the instance of your worker.
```ts
// In your main thread
import { sendRequest } from 'worker-request-response';
import { yourWorker } from './somewhere';async function() {
const response = await sendRequest(
yourWorker
42,
);
}
```### Asynchronous request handler in the worker
The request handler that you pass into `handleRequestsWith` can be synchronous or asynchronous — in the latter case the resulting promise is awaited before being sent back.
## License
The source code of this project is distributed under the terms of the ISC license. It's like MIT, but better. [Click here](https://choosealicense.com/licenses/isc/) to learn what that means.