Provide connection properties directly to callback

This avoids stale data in the callback when the connection
is reconnected. Reconnect may use a different port.
This commit is contained in:
Arjan Molenaar 2022-09-29 20:44:30 +02:00
parent 6d3003e4d4
commit 53ac9aec37
4 changed files with 32 additions and 9 deletions

View File

@ -28,7 +28,7 @@ class Connection(NamedTuple):
connected: Item
port: Port
constraint: Constraint
callback: Callable[[], None]
callback: Callable[[Item, Handle, Item, Port], None]
class ConnectionError(Exception):
@ -91,7 +91,7 @@ class Connections:
connected: Item,
port: Port | None,
constraint: Constraint | None = None,
callback: Callable[[], None] | None = None,
callback: Callable[[Item, Handle, Item, Port], None] | None = None,
) -> None:
"""Create a connection between two items. The connection is registered
and the constraint is added to the constraint solver.
@ -137,7 +137,7 @@ class Connections:
connected: Item,
port: Port,
constraint: Constraint,
callback: Callable[[], None],
callback: Callable[[Item, Handle, Item, Port], None],
) -> None:
"""Perform the real disconnect."""
# Same arguments as connect_item, makes reverser easy
@ -145,7 +145,7 @@ class Connections:
self._solver.remove_constraint(constraint)
if callback:
callback()
callback(item, handle, connected, port)
self._connections.delete(item, handle, connected, port, constraint, callback)

View File

@ -80,7 +80,9 @@ class ItemConnector:
self.connect_handle(sink)
def connect_handle(
self, sink: ConnectionSinkType, callback: Optional[Callable[[], None]] = None
self,
sink: ConnectionSinkType,
callback: Optional[Callable[[Item, Handle, Item, Port], None]] = None,
) -> None:
"""Create constraint between handle of a line and port of connectable
item.

View File

@ -70,7 +70,7 @@ def test_disconnect_item_with_callback():
events = []
def callback():
def callback(*args):
events.append("called")
c.connections.connect_item(
@ -118,7 +118,7 @@ def test_disconnect_item_by_deleting_element():
events = []
def callback():
def callback(*args):
events.append("called")
c.connections.connect_item(

View File

@ -1,9 +1,10 @@
"""Test segment aspects for items."""
import pytest
from gaphas.connector import ItemConnectionSink, ItemConnector
from gaphas.handlemove import HandleMove
from gaphas.item import Element
from gaphas.segment import Line, Segment
from gaphas.item import Element, Line
from gaphas.segment import Segment
def test_segment_fails_for_element(canvas, connections):
@ -327,3 +328,23 @@ def test_merge_orthogonal_line(canvas, connections):
with pytest.raises(ValueError):
Segment(line, canvas).merge_segment(1)
def callback(_item, _handle, connected, port):
connected.ports().index(port)
def test_update_callback_on_reconnect(canvas, connections):
line = Line(connections)
attached = Line(connections)
segment = Segment(line, canvas)
sink = ItemConnectionSink(line)
connector = ItemConnector(attached, attached.handles()[0], connections)
segment.split_segment(0)
connector.glue(sink)
connector.connect_handle(sink, callback)
segment.merge_segment(0)
connector.disconnect()