Skip to content

Commit 85ba867

Browse files
authored
fix: workers should be inline to work in other apps (#3003)
1 parent afb6e95 commit 85ba867

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

package-lock.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@
169169
"source-map-explorer": "^2.5.3",
170170
"stylelint": "^16.20.0",
171171
"ts-jest": "^29.2.5",
172-
"typescript": "^5.8.3"
172+
"typescript": "^5.8.3",
173+
"workerize-loader": "^2.0.2"
173174
},
174175
"peerDependencies": {
175176
"monaco-yql-languages": ">=1.3.0",

src/components/Graph/BlockComponents/StageBlockComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const StageBlockComponent = ({className, block}: Props) => {
1616
? block.operators.map((item) => <div key={item}>{item}</div>)
1717
: block.name}
1818
{block.tables ? (
19-
<div>
19+
<div style={{overflow: 'hidden', textOverflow: 'ellipsis'}}>
2020
<Text color="secondary">{i18n('label_tables')}: </Text>
2121
{block.tables.join(', ')}
2222
</div>

src/components/Graph/GravityGraph.tsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import React from 'react';
33
import {GraphState} from '@gravity-ui/graph';
44
import type {HookGraphParams} from '@gravity-ui/graph/react';
55
import {GraphBlock, GraphCanvas, useGraph, useGraphEvent} from '@gravity-ui/graph/react';
6+
// @ts-ignore - workerize-loader syntax
7+
import createWorker from 'workerize-loader!./treeLayout.worker';
68

79
import {cn} from '../../utils/cn';
810

@@ -66,25 +68,21 @@ export function GravityGraph<T>({data, theme}: Props<T>) {
6668
const {graph, start} = useGraph(config);
6769

6870
React.useEffect(() => {
69-
// Just in case, although mounting takes more time than calculation
70-
const worker = new Worker(new URL('./treeLayout', import.meta.url));
71-
worker.postMessage({
72-
nodes: data.nodes,
73-
links: data.links,
74-
});
75-
76-
worker.onmessage = function (e) {
77-
const {layout, edges} = e.data;
78-
79-
graph.setEntities({
80-
blocks: layout,
81-
connections: edges,
71+
// Using workerize-loader to inline the worker and avoid CORS issues
72+
const worker = createWorker();
73+
74+
// Call the exported function from the worker
75+
worker
76+
.calculateLayout(data.nodes, data.links)
77+
.then((result: {layout: any; edges: any}) => {
78+
graph.setEntities({
79+
blocks: result.layout,
80+
connections: result.edges,
81+
});
82+
})
83+
.catch((err: Error) => {
84+
console.error('Worker error:', err);
8285
});
83-
};
84-
85-
worker.onerror = (err) => {
86-
console.error(err);
87-
};
8886

8987
return () => {
9088
worker.terminate();

src/components/Graph/treeLayout.ts renamed to src/components/Graph/treeLayout.worker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,15 @@ function calculateTreeEdges(layoutResult: ExtendedTBlock[], connections: TConnec
359359
return connectionPaths;
360360
}
361361

362-
onmessage = function (e) {
363-
const {nodes, links} = e.data;
362+
// Export function for workerize-loader
363+
export function calculateLayout(nodes: any[], links: any[]) {
364364
const blocks = prepareBlocks(nodes);
365365
const connections = prepareConnections(links);
366366
const layout = calculateTreeLayout(blocks, connections);
367367
const edges = calculateTreeEdges(layout, connections);
368368

369-
postMessage({
369+
return {
370370
layout,
371371
edges,
372-
});
373-
};
372+
};
373+
}

0 commit comments

Comments
 (0)