Skip to content

Commit

Permalink
fix: #25 postMessage 导致循环
Browse files Browse the repository at this point in the history
  • Loading branch information
飝猫 committed Mar 31, 2021
1 parent 758f511 commit 858ef1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/graph/src/workers/createWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const createWorker = <R>(type: string) => (...data) =>
new Promise<R>((resolve, reject) => {
const worker = new Worker();
worker.postMessage({
type,
_algorithmType:type,
data,
});

worker.onmessage = (event: Event) => {
const { data, type } = event.data;
if (MESSAGE.SUCCESS === type) {
const { data, _algorithmType } = event.data;
if (MESSAGE.SUCCESS === _algorithmType) {
resolve(data);
} else {
reject();
Expand Down
16 changes: 10 additions & 6 deletions packages/graph/src/workers/index.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ interface Event {
}

ctx.onmessage = (event: Event) => {
const { type, data } = event.data;
if (typeof algorithm[type] === 'function') {
const result = algorithm[type](...data);
ctx.postMessage({ type: MESSAGE.SUCCESS, data: result });
const { _algorithmType, data } = event.data;
// 如果发送内容没有私有类型。说明不是自己发的。不管
// fix: https://github.com/antvis/algorithm/issues/25
if(!_algorithmType){
return;
}

ctx.postMessage({ type: MESSAGE.FAILURE });
if (typeof algorithm[_algorithmType] === 'function') {
const result = algorithm[_algorithmType](...data);
ctx.postMessage({ _algorithmType: MESSAGE.SUCCESS, data: result });
return;
}
ctx.postMessage({ _algorithmType: MESSAGE.FAILURE });
};

// https://stackoverflow.com/questions/50210416/webpack-worker-loader-fails-to-compile-typescript-worker
Expand Down

0 comments on commit 858ef1a

Please sign in to comment.