Skip to content

Commit e3fcd4b

Browse files
committed
Add lastPostAt date so we can show a loading indicator.
1 parent a9afb38 commit e3fcd4b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const getInitialState = () => ({
55
errors: [],
66
data: undefined,
77
error: undefined,
8-
updatedAt: undefined
8+
updatedAt: undefined,
9+
lastPostAt: undefined
910
})
1011

1112
const uninitialized = () => {
@@ -35,6 +36,11 @@ class WebWorker extends React.Component {
3536
)
3637
}
3738

39+
postMessage = data => {
40+
const { postMessage = uninitialized } = this.worker || {}
41+
this.setState({ lastPostAt: new Date() }, () => postMessage.call(this.worker, data))
42+
}
43+
3844
componentDidMount() {
3945
this.worker = new window.Worker(this.props.path)
4046
this.worker.onmessage = this.onMessage
@@ -50,10 +56,9 @@ class WebWorker extends React.Component {
5056

5157
render() {
5258
const { children } = this.props
53-
const { postMessage = uninitialized } = this.worker || {}
5459
const renderProps = {
5560
...this.state,
56-
postMessage: (...args) => postMessage.call(this.worker, ...args)
61+
postMessage: this.postMessage
5762
}
5863

5964
if (typeof children === "function") {

0 commit comments

Comments
 (0)