File tree Expand file tree Collapse file tree 5 files changed +58
-23
lines changed Expand file tree Collapse file tree 5 files changed +58
-23
lines changed Original file line number Diff line number Diff line change 11import React , { Component } from 'react'
22
3- import ExampleComponent from 'react-web-worker'
3+ import WebWorker from 'react-web-worker'
4+
5+ import message from './workers/message'
6+ import count from './workers/count'
47
58export default class App extends Component {
69 render ( ) {
10+ const [ messageWorker , countWorker ] = WebWorker ( [ message , count ] )
11+ messageWorker . postMessage ( 'Hi!' )
712 return (
813 < div >
9- < ExampleComponent text = 'Working with Web Workers in React' / >
14+ < p > Web Worker </ p >
1015 </ div >
1116 )
1217 }
Original file line number Diff line number Diff line change 1+ const count = ( ) => {
2+ let count = 0 ;
3+ const countFunc = ( ) => {
4+ while ( count < 1000000000 ) {
5+ count ++ ;
6+ }
7+ }
8+ const sentData = ( ) => {
9+ // eslint-disable-next-line no-restricted-globals
10+ self . postMessage ( { count } )
11+ }
12+
13+ // eslint-disable-next-line no-restricted-globals
14+ self . onmessage = ( ) => {
15+ countFunc ( ) ;
16+ sentData ( ) ;
17+ }
18+ }
19+
20+ export default count ;
Original file line number Diff line number Diff line change 1+ const message = ( ) => {
2+ let text = 'Hello I am Your Web Worker'
3+
4+ const sentData = ( ) => {
5+ // eslint-disable-next-line no-restricted-globals
6+ self . postMessage ( { text } )
7+ }
8+
9+ // eslint-disable-next-line no-restricted-globals
10+ self . onmessage = ( { data } ) => {
11+ console . log ( data )
12+ sentData ( )
13+ }
14+ }
15+
16+ export default message ;
Original file line number Diff line number Diff line change 11/**
2- * @class ExampleComponent
2+ * @function WebWorker
33 */
44
5- import React , { Component } from 'react'
6- import PropTypes from 'prop-types'
5+ import CreateWorker from './utils/CreateWorker'
76
8- import styles from './styles.css'
9-
10- export default class ExampleComponent extends Component {
11- static propTypes = {
12- text : PropTypes . string
13- }
14-
15- render ( ) {
16- const {
17- text
18- } = this . props
19-
20- return (
21- < div className = { styles . test } >
22- Example Component Hello: { text }
23- </ div >
24- )
25- }
7+ export default ( files ) => {
8+ return files . map ( file => new CreateWorker ( file ) )
269}
Original file line number Diff line number Diff line change 1+ export default class CreateWorker {
2+ constructor ( worker ) {
3+ let code = worker . toString ( )
4+ code = code . substring ( code . indexOf ( '{' ) + 1 , code . lastIndexOf ( '}' ) )
5+ // eslint-disable-next-line no-undef
6+ code = new Blob ( [ code ] , { type : 'application/javascript' } )
7+ // eslint-disable-next-line no-undef
8+ code = new Worker ( URL . createObjectURL ( code ) )
9+ return code
10+ }
11+ }
You can’t perform that action at this time.
0 commit comments