1
1
import React from "react"
2
2
3
- const getInitialState = ( ) => ( {
4
- messages : [ ] ,
5
- errors : [ ] ,
6
- data : undefined ,
7
- error : undefined ,
8
- updatedAt : undefined ,
9
- lastPostAt : undefined
10
- } )
11
-
12
- const uninitialized = ( ) => {
13
- throw new Error ( "Not initialized" )
14
- }
15
-
16
- const { Consumer, Provider } = React . createContext ( getInitialState ( ) )
3
+ const { Consumer, Provider } = React . createContext ( )
17
4
18
5
class WebWorker extends React . Component {
19
- state = getInitialState ( )
6
+ constructor ( props ) {
7
+ super ( props )
8
+ this . state = {
9
+ messages : [ ] ,
10
+ errors : [ ] ,
11
+ data : undefined ,
12
+ error : undefined ,
13
+ updatedAt : undefined ,
14
+ lastPostAt : undefined ,
15
+ postMessage : this . postMessage
16
+ }
17
+ }
18
+
19
+ uninitialized = ( ) => {
20
+ throw new Error ( "Worker not initialized" )
21
+ }
20
22
21
23
onMessage = message => {
22
24
if ( ! this . mounted ) return
@@ -39,7 +41,7 @@ class WebWorker extends React.Component {
39
41
40
42
postMessage = data => {
41
43
const { serializer = x => x } = this . props
42
- const { postMessage = uninitialized } = this . worker || { }
44
+ const { postMessage = this . uninitialized } = this . worker || { }
43
45
this . setState ( { lastPostAt : new Date ( ) } , ( ) => postMessage . call ( this . worker , serializer ( data ) ) )
44
46
}
45
47
@@ -49,7 +51,6 @@ class WebWorker extends React.Component {
49
51
this . worker . onmessage = this . onMessage
50
52
this . worker . onerror = this . onError
51
53
this . mounted = true
52
- this . setState ( getInitialState ( ) )
53
54
}
54
55
55
56
componentWillUnmount ( ) {
@@ -59,19 +60,12 @@ class WebWorker extends React.Component {
59
60
60
61
render ( ) {
61
62
const { children } = this . props
62
- const renderProps = {
63
- ...this . state ,
64
- postMessage : this . postMessage
65
- }
66
-
67
63
if ( typeof children === "function" ) {
68
- return < Provider value = { renderProps } > { children ( renderProps ) } </ Provider >
64
+ return < Provider value = { this . state } > { children ( this . state ) } </ Provider >
69
65
}
70
-
71
66
if ( children !== undefined && children !== null ) {
72
- return < Provider value = { renderProps } > { children } </ Provider >
67
+ return < Provider value = { this . state } > { children } </ Provider >
73
68
}
74
-
75
69
return null
76
70
}
77
71
}
@@ -80,42 +74,42 @@ class WebWorker extends React.Component {
80
74
* Renders only when no message or error has been received yet
81
75
*
82
76
* @prop {boolean } persist Show until we have data, even when an error occurred
83
- * @prop {Function|Node } children Function (passing props ) or React node
77
+ * @prop {Function|Node } children Function (passing state ) or React node
84
78
*/
85
79
WebWorker . Pending = ( { children, persist } ) => (
86
80
< Consumer >
87
- { props => {
88
- if ( props . data !== undefined ) return null
89
- if ( ! persist && props . error !== undefined ) return null
90
- return typeof children === "function" ? children ( props ) : children || null
81
+ { state => {
82
+ if ( state . data !== undefined ) return null
83
+ if ( ! persist && state . error !== undefined ) return null
84
+ return typeof children === "function" ? children ( state ) : children || null
91
85
} }
92
86
</ Consumer >
93
87
)
94
88
95
89
/**
96
90
* Renders only when worker has sent a message with data
97
91
*
98
- * @prop {Function|Node } children Function (passing data and props ) or React node
92
+ * @prop {Function|Node } children Function (passing data and state ) or React node
99
93
*/
100
94
WebWorker . Data = ( { children } ) => (
101
95
< Consumer >
102
- { props => {
103
- if ( props . data === undefined ) return null
104
- return typeof children === "function" ? children ( props . data , props ) : children || null
96
+ { state => {
97
+ if ( state . data === undefined ) return null
98
+ return typeof children === "function" ? children ( state . data , state ) : children || null
105
99
} }
106
100
</ Consumer >
107
101
)
108
102
109
103
/**
110
104
* Renders only when worker has sent an error
111
105
*
112
- * @prop {Function|Node } children Function (passing error and props ) or React node
106
+ * @prop {Function|Node } children Function (passing error and state ) or React node
113
107
*/
114
108
WebWorker . Error = ( { children } ) => (
115
109
< Consumer >
116
- { props => {
117
- if ( props . error === undefined ) return null
118
- return typeof children === "function" ? children ( props . error , props ) : children || null
110
+ { state => {
111
+ if ( state . error === undefined ) return null
112
+ return typeof children === "function" ? children ( state . error , state ) : children || null
119
113
} }
120
114
</ Consumer >
121
115
)
0 commit comments