Skip to content

Commit e333f37

Browse files
committed
Initial commit
0 parents  commit e333f37

File tree

6 files changed

+7353
-0
lines changed

6 files changed

+7353
-0
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": [["env", { "loose": true }], "stage-2", "react"],
3+
"ignore": ["node_modules", "stories.js"]
4+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib/
2+
node_modules/

README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# `<WebWorker>`
2+
3+
[![npm version](https://img.shields.io/npm/v/react-webworker.svg)](https://www.npmjs.com/package/react-webworker)
4+
[![npm downloads](https://img.shields.io/npm/dm/react-webworker.svg)](https://www.npmjs.com/package/react-webworker)
5+
[![ISC license](https://img.shields.io/npm/l/react-webworker.svg)](https://opensource.org/licenses/ISC)
6+
[![minified size](https://img.shields.io/bundlephobia/min/react-webworker.svg)](https://bundlephobia.com/result?p=react-webworker)
7+
[![GitHub issues](https://img.shields.io/github/issues/ghengeveld/react-webworker.svg)](https://github.com/ghengeveld/react-webworker/issues)
8+
[![GitHub PRs](https://img.shields.io/github/issues-pr/ghengeveld/react-webworker.svg)](https://github.com/ghengeveld/react-webworker/pulls)
9+
10+
React component for easy communication with a Web Worker. Leverages the Render Props pattern for ultimate flexibility as
11+
well as the new Context API for ease of use. Just specify the public path to your Web Worker and you'll get access to
12+
any messages or errors it sends, as well as the `postMessage` handler.
13+
14+
- Zero dependencies
15+
- Choose between Render Props and Context-based helper components
16+
- Provides timestamped `messages` and `errors`
17+
- Provides easy access to the last message `data` and last `error`
18+
- Provides `postMessage` to send messages to the Web Worker
19+
- Accepts `onMessage` and `onError` callbacks
20+
21+
## Install
22+
23+
```
24+
npm install --save react-webworker
25+
```
26+
27+
## Usage
28+
29+
Using render props for ultimate flexibility:
30+
31+
```js
32+
import WebWorker from "react-webworker"
33+
34+
const MyComponent = () => (
35+
<WebWorker path="/worker.js">
36+
{({ data, error, postMessage }) => {
37+
if (error) return `Something went wrong: ${error.message}`
38+
if (data)
39+
return (
40+
<div>
41+
<strong>Received some data:</strong>
42+
<pre>{JSON.stringify(data, null, 2)}</pre>
43+
</div>
44+
)
45+
return <button onClick={() => postMessage("hello")}>Hello</button>
46+
}}
47+
</WebWorker>
48+
)
49+
```
50+
51+
Using helper components (don't have to be direct children) for ease of use:
52+
53+
```js
54+
import WebWorker from "react-webworker"
55+
56+
const MyComponent = () => (
57+
<WebWorker path="/worker.js">
58+
<WebWorker.Pending>
59+
{({ postMessage }) => <button onClick={() => postMessage("hello")}>Hello</button>}
60+
</WebWorker.Pending>
61+
<WebWorker.Data>
62+
{data => (
63+
<div>
64+
<strong>Received some data:</strong>
65+
<pre>{JSON.stringify(data, null, 2)}</pre>
66+
</div>
67+
)}
68+
</WebWorker.Data>
69+
<WebWorker.Error>{error => `Something went wrong: ${error.message}`}</WebWorker.Error>
70+
</WebWorker>
71+
)
72+
```
73+
74+
### Props
75+
76+
`<WebWorker>` takes the following properties:
77+
78+
- `path` {string} Public path to the Web Worker file (from the root of your domain)
79+
- `onMessage` {Function} Callback function invoked when a message is received, passing message data as argument
80+
- `onError` {Function} Callback function invoked when an error is received, passing error object as argument
81+
82+
### Render props
83+
84+
`<WebWorker>` provides the following render props:
85+
86+
- `messages` {Array} list of received messages, in chronological order
87+
- `errors` {Array} list of received errors, in chronological order
88+
- `data` {any} last received message data, maintained when an error is received
89+
- `error` {Error} last received error, cleared when new message arrives
90+
- `updatedAt` {Date} when the last message or error was received
91+
- `postMessage` {Function} sends a message to the Web Worker
92+
93+
## Helper components
94+
95+
`<WebWorker>` provides several helper components that make your JSX even more declarative.
96+
They don't have to be direct children of `<WebWorker>` and you can use the same component several times.
97+
98+
### `<WebWorker.Data>`
99+
100+
Renders only when a message has been received.
101+
102+
#### Props
103+
104+
- `children` {Function|Node} Render function which receives last message data and props object or just a plain React node.
105+
106+
#### Examples
107+
108+
```js
109+
<WebWorker.Data>{data => <pre>{JSON.stringify(data)}</pre>}</WebWorker.Data>
110+
```
111+
112+
### `<WebWorker.Error>`
113+
114+
Renders only when an error has been received.
115+
116+
#### Props
117+
118+
- `children` {Function|Node} Render function which receives error and props object or just a plain React node.
119+
120+
#### Examples
121+
122+
```js
123+
<WebWorker.Error>{error => `Unexpected error: ${error.message}`}</WebWorker.Error>
124+
```
125+
126+
### `<WebWorker.Pending>`
127+
128+
Renders only when no message or error has been received yet. Enable `persist` to ignore errors.
129+
130+
#### Props
131+
132+
- `persist` {boolean} Show until we receive message data, even when an error is received.
133+
- `children` {Function|Node} Function which receives props object or React node.

0 commit comments

Comments
 (0)