question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'onConnectionRemoved' called before 'onConnectionCreated'

See original GitHub issue

For some reason, my code emits the ‘onConnectionRemoved’ before ‘onConnectionCreated’ when I try to delete a connection after it is made.

Steps to reproduce below: (See codepen.io)

let editor = new Drawflow(document.querySelector("#drawflow"));
editor.start();

editor.addNode("Home", 0, 1, 10, 10, "node-test", {}, "Drag from here =>", false);
editor.addNode("Home", 1, 0, 300, 10, "node-test", {}, "<= Connect to here", false);

// Issue: When we delete just after creating a connection, the events
// below get fired incorrently. First "connectionRemoved", then "connectionCreated"
editor.on("connectionCreated", deleteCreatedConnection)

editor.on("connectionCreated", () => console.log("connectionCreated"));
editor.on("connectionRemoved", () => console.log("connectionRemoved"));

// Registering the listener after fixes the problem somehow?
// editor.on("connectionCreated", deleteCreatedConnection)


function deleteCreatedConnection({input_id, output_id, input_class, output_class}) {
  editor.removeSingleConnection(output_id, input_id, output_class, input_class);
}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MyIsaakcommented, Jun 20, 2022

I found a workaround by deleting the connection in the next event loop using a timeout (still not ideal):

function deleteCreatedConnection({input_id, output_id, input_class, output_class}) {
  setTimeout(() => editor.removeSingleConnection(output_id, input_id, output_class, input_class), 0);
}
0reactions
MyIsaakcommented, Jun 20, 2022

Thanks for the thorough explanation, and yes internally events are handled in a synchronous fashion, however logically one could sort the scheduled events to avoid this.

The question is why are you using the same event twice?

I wrote a wrapper class that makes instantiates Drawflow and make internal changes, like modifying the curves and listening to connectionCreated to delete specified connections. After instantiating my wrapper class, one might want to use the same event twice. In this case, I add a listener to connectionCreated, and connectionRemove, to update the state of the connected nodes, but events aren’t played in order.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found