Skip to content

Commit cfedad3

Browse files
committed
Main Functionality Done
1 parent 860f1f6 commit cfedad3

File tree

5 files changed

+58
-23
lines changed

5 files changed

+58
-23
lines changed

example/src/App.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import 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

58
export 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
}

example/src/workers/count.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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;

example/src/workers/message.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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;

src/index.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
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
}

src/utils/CreateWorker.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

0 commit comments

Comments
 (0)