Skip to content

Fix: sync store.pods[id].parent/position immediately on updates from anywhere #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

li-xin-yi
Copy link
Collaborator

@li-xin-yi li-xin-yi commented Dec 13, 2022

I don't know what triggered such a horrible code reformation in canvas, actually this is just a very tiny modification. Eventually, I adopted another simpler optimization strategy other than what I proposed here. Please test it carefully before merging, it works on my side, but I'm not sure if any issue exists.

A brief summary:

  1. Make all tooltips not draggable.
  2. Rewrite store.setPodPosition and store.setPodParent, add a parameter dirty: bool, set pod.dirty = pod.dirty | dirty, that is, only triggers remote update request when the argument dirty=true. Note that we use | (or) operation instead of = (assign) to avoid a dirty pod cleaned by another local setPodPosition or setPodParent with dirty=false before the update is synced to the DB.
  3. For codeNode and scopeNode, useEffect listens on the change of node position or change, call setPodPosition or setPodParent with dirty=false whenever the corresponding position or parent node changes. We don't need to write the change to DB for now, but we must sync store.pods with the nodes in canvas.
  4. dirty=true is only used when stopping dragging nodes (in onNodeDragStop, that is, we only writes the update to DB when it is caused by operations by our current user. We don't need to write update pulled from the remote yjs server to the DB.

Possible issue:

  • During dragging, the position of a node changes all the time, so we keep rendering the node and calling setPodPosition continually, even though it doesn't change store.pods[id].dirty and no data is written to the DB. One solution to mitigate the update to store: take a look at dragging as well, only update store.pods[id] after the dragging stops, like:
function codeNode({..., xPos, yPos, dragging}) {
// ....
useEffect( () => {
 if(!dragging) {
 let [x, y] = relativePostion(xPos, yPos); // transform XY coordinate
 setPodPosition({id, x, y, dirty: false})
}
}, [...,xPos, yPos, dragging])
// ....
}

But I think the performance of dragging is acceptable now, if you feel uncomfortable with the performance in the future, the solution is just for reference.

close #138

Copy link
Collaborator

@lihebi lihebi left a comment

Choose a reason for hiding this comment

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

Thanks!

@lihebi lihebi merged commit 2ccaa87 into codepod-io:main Dec 13, 2022
@li-xin-yi li-xin-yi deleted the fix/immediate-update-position-parent branch December 13, 2022 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1. sync important fields of store.pods[id]; 2. sync between yjs and database
2 participants