Skip to content

Commit

Permalink
Try to fix broken generator sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi committed Nov 13, 2023
1 parent 1f4d90e commit f0b924e
Showing 1 changed file with 44 additions and 41 deletions.
85 changes: 44 additions & 41 deletions src/gameComponents/Generator/Generator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,47 +101,6 @@ const Generator = ({ color = "#ccc", item, id, currentItemId, setState }) => {
}
}, [getItems, id, pushItem, setState]);

const onPlaceItem = React.useCallback(
async (itemIds) => {
/**
* Callback if generated item or generator is placed
*/
const placeSelf = itemIds.includes(id);
if (itemIds.includes(currentItemRef.current) && !placeSelf) {
// We have removed generated item so we create a new one.
const [thisItem] = await getItems([id]);
batchUpdateItems([currentItemRef.current], (item) => {
const result = {
...item,
layer: thisItem.layer,
};
delete result.editable;
return result;
});
await addItem();
}
if (placeSelf) {
if (!currentItemRef.current) {
// Missing item for any reason
await addItem();
}
}
},
[addItem, batchUpdateItems, getItems, id]
);

const onDeleteItem = React.useCallback(
async (itemIds) => {
/**
* Callback if an item is deleted
*/
if (itemIds.includes(currentItemRef.current)) {
await addItem();
}
},
[addItem]
);

/**
* Set generator dimension according to Item content.
*/
Expand Down Expand Up @@ -194,6 +153,50 @@ const Generator = ({ color = "#ccc", item, id, currentItemId, setState }) => {
[]
);

const onPlaceItem = React.useCallback(
async (itemIds) => {
/**
* Callback if generated item or generator is placed
*/
const placeSelf = itemIds.includes(id);
if (itemIds.includes(currentItemRef.current) && !placeSelf) {
// We have removed generated item so we create a new one.
const [thisItem] = await getItems([id]);
batchUpdateItems([currentItemRef.current], (item) => {
const result = {
...item,
layer: thisItem.layer,
};
delete result.editable;
return result;
});

await addItem();
resize(item?.rotation);
}
if (placeSelf) {
if (!currentItemRef.current) {
// Missing item for any reason
await addItem();
}
resize(item?.rotation);
}
},
[addItem, batchUpdateItems, getItems, id, item?.rotation, resize]
);

const onDeleteItem = React.useCallback(
async (itemIds) => {
/**
* Callback if an item is deleted
*/
if (itemIds.includes(currentItemRef.current)) {
await addItem();
}
},
[addItem]
);

React.useEffect(() => {
/**
* update item on modifications only if master
Expand Down

1 comment on commit f0b924e

@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.