From e4f0153a7de744facc073a07005c7700cfe7660b Mon Sep 17 00:00:00 2001 From: Kia Lam Date: Wed, 9 Mar 2022 06:58:43 -0800 Subject: [PATCH] Remove import statements from web worker file. --- awx/ui/src/util/simulationWorker.js | 34 +++++++++++++++++++++++++++++ awx/ui/src/util/webWorker.js | 7 +----- 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 awx/ui/src/util/simulationWorker.js diff --git a/awx/ui/src/util/simulationWorker.js b/awx/ui/src/util/simulationWorker.js new file mode 100644 index 0000000000..d743e2e76c --- /dev/null +++ b/awx/ui/src/util/simulationWorker.js @@ -0,0 +1,34 @@ +/* eslint-disable no-undef */ +importScripts('https://d3js.org/d3-collection.v1.min.js'); +importScripts('https://d3js.org/d3-dispatch.v1.min.js'); +importScripts('https://d3js.org/d3-quadtree.v1.min.js'); +importScripts('https://d3js.org/d3-timer.v1.min.js'); +importScripts('https://d3js.org/d3-force.v1.min.js'); + +onmessage = function calculateLayout({ data: { nodes, links } }) { + const simulation = d3 + .forceSimulation(nodes) + .force('charge', d3.forceManyBody(15).strength(-50)) + .force( + 'link', + d3.forceLink(links).id((d) => d.hostname) + ) + .force('collide', d3.forceCollide(62)) + .force('forceX', d3.forceX(0)) + .force('forceY', d3.forceY(0)) + .stop(); + + for ( + let i = 0, + n = Math.ceil( + Math.log(simulation.alphaMin()) / Math.log(1 - simulation.alphaDecay()) + ); + i < n; + ++i + ) { + postMessage({ type: 'tick', progress: i / n }); + simulation.tick(); + } + + postMessage({ type: 'end', nodes, links }); +}; diff --git a/awx/ui/src/util/webWorker.js b/awx/ui/src/util/webWorker.js index 64c2eac037..7babb68f38 100644 --- a/awx/ui/src/util/webWorker.js +++ b/awx/ui/src/util/webWorker.js @@ -1,8 +1,3 @@ export default function webWorker() { - return new Worker( - new URL( - 'screens/TopologyView/utils/workers/simulationWorker.js', - import.meta.url - ) - ); + return new Worker(new URL('./simulationWorker.js', import.meta.url)); }