Skip to content

Commit 9ccb270

Browse files
committed
Add parser and serializer.
1 parent d5b05cd commit 9ccb270

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ any messages or errors it sends, as well as the `postMessage` handler.
1717
- Provides easy access to the last message `data` and last `error`
1818
- Provides `postMessage` to send messages to the Web Worker
1919
- Provides `updatedAt` and `lastPostAt` metadata
20+
- Accepts `parser` and `serializer` for automatic message (de)serialization
2021
- Accepts `onMessage` and `onError` callbacks
2122

2223
> This package was modeled after [`<Async>`](https://github.com/ghengeveld/react-async) which helps you deal with Promises in React.
@@ -80,6 +81,8 @@ const MyComponent = () => (
8081

8182
- `url` {string} (required) Public url to the Web Worker file (or path relative to the root of your domain)
8283
- `options` {Object} Options passed to the Worker constructor
84+
- `parser` {Function} Transforms incoming messages (not errors)
85+
- `serializer` {Function} Transforms `postMessage` payload before sending
8386
- `onMessage` {Function} Callback function invoked when a message is received, passing message data as argument
8487
- `onError` {Function} Callback function invoked when an error is received, passing error object as argument
8588

@@ -138,6 +141,30 @@ const MyComponent = () => (
138141
)
139142
```
140143

144+
### Using `parser` and `serializer` to automatically parse incoming messages and stringify outgoing messages
145+
146+
```js
147+
import WebWorker from "react-webworker"
148+
149+
const MyComponent = () => (
150+
<WebWorker url="/worker.js" parser={JSON.parse} serializer={JSON.stringify}>
151+
{({ data, error, postMessage, updatedAt, lastPostAt }) => (
152+
<div>
153+
{data && (
154+
<div>
155+
<strong>Received some data:</strong>
156+
<pre>{JSON.stringify(data, null, 2)}</pre>
157+
</div>
158+
)}
159+
<button onClick={() => postMessage({ foo: "bar" })}>Send</button>
160+
</div>
161+
)}
162+
</WebWorker>
163+
)
164+
```
165+
166+
> Note: the Worker must still implement JSON (de)serialization on its own end.
167+
141168
## Helper components
142169

143170
`<WebWorker>` provides several helper components that make your JSX even more declarative.

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const { Consumer, Provider } = React.createContext(getInitialState())
1818
class WebWorker extends React.Component {
1919
state = getInitialState()
2020

21-
onMessage = ({ data }) => {
21+
onMessage = message => {
2222
if (!this.mounted) return
23+
const data = this.props.parser ? this.props.parser(message.data) : message.data
2324
const date = new Date()
2425
this.setState(
2526
state => ({ data, error: undefined, messages: state.messages.concat({ data, date }), updatedAt: date }),
@@ -37,8 +38,9 @@ class WebWorker extends React.Component {
3738
}
3839

3940
postMessage = data => {
41+
const { serializer = x => x } = this.props
4042
const { postMessage = uninitialized } = this.worker || {}
41-
this.setState({ lastPostAt: new Date() }, () => postMessage.call(this.worker, data))
43+
this.setState({ lastPostAt: new Date() }, () => postMessage.call(this.worker, serializer(data)))
4244
}
4345

4446
componentDidMount() {

0 commit comments

Comments
 (0)