Skip to content

Commit

Permalink
Fix linkedItem perf and bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi committed Mar 8, 2024
1 parent 579ce91 commit 041ed39
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/gameComponents/AdvancedImage/AdvancedImage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,21 @@ const AdvancedImage = ({
linkedItems: newLinkedItems,
};
}

return {};
}, true);
},
[currentItemId, getItemList, setState]
);

React.useEffect(() => {
const unregisterList = [];
unregisterList.push(register(onPlaceItem));
if (currentItemId) {
unregisterList.push(register(onPlaceItem));
}

return () => {
unregisterList.forEach((callback) => callback());
};
}, [onPlaceItem, register]);
}, [currentItemId, onPlaceItem, register]);

return (
<Wrapper ref={wrapperRef}>
Expand Down
8 changes: 4 additions & 4 deletions src/gameComponents/CheckerBoard/CheckerBoard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ const CheckerBoard = ({
linkedItems: newLinkedItems,
};
}

return {};
}, true);
},
[currentItemId, getItemList, setState]
);

React.useEffect(() => {
const unregisterList = [];
unregisterList.push(register(onPlaceItem));
if (currentItemId) {
unregisterList.push(register(onPlaceItem));
}

return () => {
unregisterList.forEach((callback) => callback());
};
}, [onPlaceItem, register]);
}, [currentItemId, onPlaceItem, register]);

return (
<StyledCheckerBoard
Expand Down
3 changes: 1 addition & 2 deletions src/gameComponents/DieImage/DieImage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ const Dice = ({

React.useEffect(() => {
const unregisterList = [];
if (rollOnMove) {
if (id && rollOnMove) {
const rollOnPlace = (itemIds) => {
if (itemIds.includes(id)) {
roll([id]);
}
};

unregisterList.push(register(rollOnPlace));
}
return () => {
Expand Down
8 changes: 5 additions & 3 deletions src/gameComponents/Generator/Generator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,15 @@ const Generator = ({ color = "#ccc", item, id, currentItemId, setState }) => {
* Register events callback
*/
const unregisterList = [];
unregisterList.push(registerPlace(onPlaceItem));
unregisterList.push(registerDelete(onDeleteItem));
if (currentItemId) {
unregisterList.push(registerPlace(onPlaceItem));
unregisterList.push(registerDelete(onDeleteItem));
}

return () => {
unregisterList.forEach((callback) => callback());
};
}, [registerPlace, registerDelete, onPlaceItem, onDeleteItem]);
}, [registerPlace, registerDelete, onPlaceItem, onDeleteItem, currentItemId]);

React.useEffect(() => {
/**
Expand Down
8 changes: 4 additions & 4 deletions src/gameComponents/Image/Image.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,21 @@ const Image = ({
linkedItems: newLinkedItems,
};
}

return {};
}, true);
},
[currentItemId, getItemList, setState]
);

React.useEffect(() => {
const unregisterList = [];
unregisterList.push(register(onPlaceItem));
if (currentItemId) {
unregisterList.push(register(onPlaceItem));
}

return () => {
unregisterList.forEach((callback) => callback());
};
}, [onPlaceItem, register]);
}, [currentItemId, onPlaceItem, register]);

return (
<Wrapper ref={wrapperRef}>
Expand Down
14 changes: 9 additions & 5 deletions src/gameComponents/Zone/Zone.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ const Zone = ({
linkedItems: newLinkedItems,
};
}

return {};
}, true);

const addedItems = Object.entries(
Expand Down Expand Up @@ -106,18 +104,24 @@ const Zone = ({

React.useEffect(() => {
if (!holdItems && setState) {
setState((item) => ({ ...item, linkedItems: [] }));
setState((item) => {
if (!Array.isArray(item.linkedItems) || item.linkedItems.length > 0) {
return { linkedItems: [] };
}
}, true);
}
}, [holdItems, setState]);

React.useEffect(() => {
const unregisterList = [];
unregisterList.push(register(onInsideItem));
if (currentItemId) {
unregisterList.push(register(onInsideItem));
}

return () => {
unregisterList.forEach((callback) => callback());
};
}, [onInsideItem, onItem, register]);
}, [currentItemId, onInsideItem, onItem, register]);

return (
<ZoneWrapper
Expand Down
8 changes: 6 additions & 2 deletions src/utils/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ export const areItemsInside = (
) => {
return Object.fromEntries(
[...itemIds, ...previousIds].map((itemId) => {
const elementToTest = getItemElement(itemId);
if (!elementToTest) {
return [itemId, { inside: false, added: false, removed: true }];
}
const inside = centerOnly
? isItemCenterInsideElement(getItemElement(itemId), element)
: isItemInsideElement(getItemElement(itemId), element);
? isItemCenterInsideElement(elementToTest, element)
: isItemInsideElement(elementToTest, element);

return [
itemId,
Expand Down

1 comment on commit 041ed39

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.