Skip to content
This repository was archived by the owner on Feb 11, 2021. It is now read-only.

Commit f92c3c9

Browse files
committed
Move prime calculation to web worker.
This commit moves the calculation to a web worker, so that way it can be handled asynchronously so the web app can move on and not block.
1 parent aa70098 commit f92c3c9

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

examples/simple/middleware/primesMiddleware.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const CHECK_NEXT_PRIME = 'CHECK_NEXT_PRIME';
44

55
function createPrimesMiddleware() {
66
return ( store ) => ( next ) => ( action ) => {
7+
78
switch ( action.type ) {
89
case CHECK_NEXT_PRIME:
910
const { queue } = store.getState().primeState;
@@ -35,18 +36,22 @@ function checkNextPrime( queue, dispatch ) {
3536
}
3637

3738
function checkPrime( number ) {
39+
3840
return new Promise(
3941
( resolve, reject ) => {
42+
// Create a worker to do the actual mathematical operations.
43+
const worker = new Worker( '/static/worker-primes.js' );
4044

41-
for ( let i = 2; i < number; i++ ) {
42-
if ( Number.isInteger( number / i ) ) {
43-
console.log( number + " is divisible by " + i );
44-
resolve( false );
45-
}
45+
worker.onmessage = function( evt ) {
46+
console.log( 'message received from worker.' );
47+
console.log( evt );
48+
resolve( evt.data );
4649
}
4750

48-
// No numbers below this one divided cleanly, it's prime.
49-
resolve( true );
51+
console.log( 'Worker created' );
52+
console.log( worker );
53+
54+
worker.postMessage( Number( number ) );
5055
}
5156
);
5257
}

examples/simple/webpack.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ var webpack = require( 'webpack' );
33

44
module.exports = {
55
devtool: 'cheap-module-source-map',
6-
entry: [
7-
'webpack-hot-middleware/client',
8-
'./index.jsx'
9-
],
6+
entry: {
7+
'hot-middleware': 'webpack-hot-middleware/client',
8+
'bundle': './index.jsx',
9+
'worker-primes': './workers/worker-primes.js'
10+
},
1011
output: {
1112
path: path.join( __dirname, 'dist' ),
12-
filename: 'bundle.js',
13+
filename: '[name].js',
1314
publicPath: '/static/'
1415
},
1516
plugins: [
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
console.log( 'worker script executed.' );
3+
4+
addEventListener( 'message', function( evt ) {
5+
const number = evt.data;
6+
console.log( 'Checking number' + number );
7+
8+
postMessage( isPrime( number ) );
9+
10+
// Our work here is done.
11+
close();
12+
} );
13+
14+
function isPrime( number ) {
15+
for ( let i = 2; i < number; i++ ) {
16+
if ( Number.isInteger( number / i ) ) {
17+
console.log( number + ' is divisible by ' + i );
18+
return false;
19+
}
20+
}
21+
22+
// No numbers below this one divided cleanly, it's prime.
23+
console.log( number + ' is prime.' );
24+
return true;
25+
}
26+

0 commit comments

Comments
 (0)