diff --git a/packages/graph/src/workers/createWorker.ts b/packages/graph/src/workers/createWorker.ts index 7647de9..37f8112 100644 --- a/packages/graph/src/workers/createWorker.ts +++ b/packages/graph/src/workers/createWorker.ts @@ -14,13 +14,13 @@ const createWorker = (type: string) => (...data) => new Promise((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(); diff --git a/packages/graph/src/workers/index.worker.ts b/packages/graph/src/workers/index.worker.ts index ea55c82..733e23f 100644 --- a/packages/graph/src/workers/index.worker.ts +++ b/packages/graph/src/workers/index.worker.ts @@ -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