Skip to content

Commit b62749f

Browse files
authored
Merge pull request #13 from drizuid/dev
optimize by using usememo for device centers
2 parents d6534ca + 05583d1 commit b62749f

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

static/js/app.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { useState, useEffect } = React;
22

3-
// Simple SVG icon components
43
const ServerIcon = (props) => React.createElement('svg', {
54
xmlns: 'http://www.w3.org/2000/svg',
65
fill: 'none',
@@ -571,10 +570,20 @@ const NetworkMonitor = () => {
571570
}
572571
}, [draggedDevice, draggedBox, resizingBox, offset, zoom, panOffset]);
573572

573+
// stackoverflow says this should make things more efficient... we shall see
574+
const deviceCenters = React.useMemo(() => {
575+
const centers = {};
576+
devices.forEach(device => {
577+
centers[device.id] = {
578+
x: device.x + 60,
579+
y: device.y + 48
580+
};
581+
});
582+
return centers;
583+
}, [devices]);
584+
574585
const getDeviceCenter = (deviceId) => {
575-
const device = devices.find(d => d.id === deviceId);
576-
if (!device) return { x: 0, y: 0 };
577-
return { x: device.x + 60, y: device.y + 48 };
586+
return deviceCenters[deviceId] || { x: 0, y: 0 };
578587
};
579588

580589
const boxColors = {

0 commit comments

Comments
 (0)