Skip to content

Commit 1a903b3

Browse files
committed
wip: worker
1 parent 8fd8ac5 commit 1a903b3

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/components/Graph/GravityGraph.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {StageBlockComponent} from './BlockComponents/StageBlockComponent';
1919
import {ConnectionBlockComponent} from './BlockComponents/ConnectionBlockComponent';
2020
import {graphColorsConfig} from './colorsConfig';
2121
import {GraphControls} from './GraphControls';
22-
import {calculateTreeLayout, calculateConnectionPaths} from './treeLayout';
22+
// import {calculateTreeLayout, calculateConnectionPaths} from './treeLayout';
2323
import {NonSelectableConnection} from './NonSelectableConnection';
2424

2525
interface Props<T> {
@@ -68,15 +68,29 @@ export function GravityGraph<T>({data, theme}: Props<T>) {
6868
const {graph, start} = useGraph(config);
6969

7070
React.useEffect(() => {
71-
const blocks = prepareBlocks(data.nodes);
72-
const connections = prepareConnections(data.links);
73-
const layouted = calculateTreeLayout(blocks, connections);
74-
const edges = calculateConnectionPaths(layouted, connections);
75-
76-
graph.setEntities({
77-
blocks: layouted,
78-
connections: edges,
71+
// на всякий случай, хотя маунт больше времени занимает, чем расчёт
72+
const worker = new Worker(new URL('./treeLayout', import.meta.url));
73+
worker.postMessage({
74+
nodes: data.nodes,
75+
links: data.links,
7976
});
77+
78+
worker.onmessage = function (e) {
79+
const {layout, edges} = e.data;
80+
81+
graph.setEntities({
82+
blocks: layout,
83+
connections: edges,
84+
});
85+
};
86+
87+
worker.onerror = (err) => {
88+
console.error(err);
89+
};
90+
91+
return () => {
92+
worker.terminate();
93+
};
8094
}, [data.nodes, data.links, graph]);
8195

8296
React.useEffect(() => {

src/components/Graph/treeLayout.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {prepareBlocks, prepareConnections} from './utils';
2+
13
class TreeLayoutEngine {
24
constructor(blocks, connections, options = {}) {
35
this.blocks = new Map(blocks.map((block) => [block.id, {...block}]));
@@ -221,12 +223,12 @@ class TreeLayoutEngine {
221223
}
222224

223225
// Функция для использования алгоритма
224-
export function calculateTreeLayout(blocks, connections, options = {}) {
226+
function calculateTreeLayout(blocks, connections, options = {}) {
225227
const engine = new TreeLayoutEngine(blocks, connections, options);
226228
return engine.layout();
227229
}
228230

229-
export function calculateConnectionPaths(layoutResult, connections) {
231+
function calculateTreeEdges(layoutResult, connections) {
230232
// Создаем карту позиций для удобства поиска
231233
const positionMap = new Map(layoutResult.map((item) => [item.id, item]));
232234

@@ -314,3 +316,16 @@ export function calculateConnectionPaths(layoutResult, connections) {
314316

315317
return connectionPaths;
316318
}
319+
320+
onmessage = function (e) {
321+
const {nodes, links} = e.data;
322+
const blocks = prepareBlocks(nodes);
323+
const connections = prepareConnections(links);
324+
const layout = calculateTreeLayout(blocks, connections);
325+
const edges = calculateTreeEdges(layout, connections);
326+
327+
postMessage({
328+
layout,
329+
edges,
330+
});
331+
};

0 commit comments

Comments
 (0)