Skip to content

Commit d041cec

Browse files
authored
Merge pull request #22 from Sunshine168/web_worker
Feat: use workerize to manage web worker
2 parents 3a3134a + 07fef7f commit d041cec

File tree

3 files changed

+29
-40
lines changed

3 files changed

+29
-40
lines changed

src/components/ProcessImage.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { noJimpInstance, webWorkerInfo } from '../utils/errorMsg';
99
import { setItem, getItem, removeItem } from '../utils/storage';
1010
import ROOT from '../utils/build';
1111
import MainPropTypes from '../validators/props';
12-
13-
const processImage = require('../utils/options');
12+
import worker from 'workerize-loader?inline!../worker';
13+
import processImage from '../utils/options';
1414

1515
class ProcessImage extends Component {
1616
static propTypes = MainPropTypes;
@@ -37,10 +37,7 @@ class ProcessImage extends Component {
3737
this.checkStorageSupport();
3838

3939
if (typeof Worker !== 'undefined' && !this.props.disableWebWorker) {
40-
this.worker = work(require.resolve('../worker.js'));
41-
// this.worker = new NewWorker();
42-
43-
this.sendPropsToWorker(this.props, this.worker);
40+
this.worker = worker();
4441
}
4542
};
4643

@@ -56,11 +53,11 @@ class ProcessImage extends Component {
5653

5754
componentDidUpdate = () => {
5855
if (this.props.image && !this.props.disableRerender) {
59-
if (typeof Worker !== 'undefined' && !this.props.disableWebWorker) {
60-
this.sendPropsToWorker(this.props, this.worker);
61-
} else {
62-
this.processInMainThread(this.props);
63-
}
56+
this.processInMainThreadOrInWebWorker(
57+
this.worker,
58+
this.props,
59+
this.myStorage
60+
);
6461
}
6562
};
6663

@@ -130,28 +127,20 @@ class ProcessImage extends Component {
130127
});
131128
};
132129

133-
processInWebWorker = (worker, props, storageReference) => {
134-
if (worker !== null) {
135-
worker.onmessage = e => {
136-
// avoid loop
137-
if (e.data.src !== this.state.src || e.data.err !== this.state.err) {
138-
this.setState({ src: e.data.src, err: e.data.err });
139-
setItem('placeholder', e.data.src, storageReference);
140-
this.passPropsToParent(props, e.data.src, e.data.err);
141-
if (typeof props.onProcessFinish === 'function') {
142-
props.onProcessFinish();
143-
}
144-
}
145-
};
146-
}
147-
};
148-
149-
sendPropsToWorker = (props, worker) => {
130+
processInWebWorker = async (worker, props, storageReference) => {
150131
if (worker !== null) {
151-
worker.postMessage({
132+
const result = await worker.process({
152133
props: filterPropsToListen(props),
153134
image: props.image
154135
});
136+
if (result.src !== this.state.src || result.err !== this.state.err) {
137+
this.setState({ src: result.src, err: result.err });
138+
setItem('placeholder', result.src, storageReference);
139+
this.passPropsToParent(props, result.src, result.err);
140+
if (typeof props.onProcessFinish === 'function') {
141+
props.onProcessFinish();
142+
}
143+
}
155144
}
156145
};
157146

src/utils/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,4 @@ function processImage(image, props, ROOT) {
215215
.containImage(image, contain);
216216
}
217217

218-
module.exports = processImage;
218+
export default processImage;

src/worker.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
const processImage = require('./utils/options');
1+
import processImage from './utils/options';
22
const defaultCdn = 'https://unpkg.com/[email protected]/browser/lib/jimp.min.js';
33

4-
module.exports = function worker(self) {
5-
self.onmessage = function(e) {
6-
// how to ensure Jimp can work?
4+
export function process(data) {
5+
// how to ensure Jimp can work?
6+
return new Promise(resolve => {
77
try {
88
if (!Jimp) {
99
}
1010
} catch (error) {
11-
const { customCdn } = e.data;
11+
const { customCdn } = data;
1212
const cdn = customCdn ? customCdn : defaultCdn;
1313
importScripts(cdn);
1414
}
1515

16-
Jimp.read(e.data.image).then(function(image) {
17-
processImage(image, e.data.props, Jimp).getBase64(Jimp.AUTO, function(
16+
Jimp.read(data.image).then(function(image) {
17+
processImage(image, data.props, Jimp).getBase64(Jimp.AUTO, function(
1818
err,
1919
src
2020
) {
21-
self.postMessage({ src, err });
21+
resolve({ src, err });
2222
});
2323
});
24-
};
25-
};
24+
});
25+
}

0 commit comments

Comments
 (0)