Skip to content

Use CallbackRegistry for {Artist,Collection}.add_callback. #18912

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
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ def __init__(self):
# only if they override get_cursor_data.
self._mouseover = type(self).get_cursor_data != Artist.get_cursor_data
self.eventson = False # fire events only if eventson
self._oid = 0 # an observer id
self._propobservers = {} # a dict from oids to funcs
self._callbacks = cbook.CallbackRegistry()
try:
self.axes = None
except AttributeError:
Expand Down Expand Up @@ -341,10 +340,9 @@ def func(artist: Artist) -> Any
--------
remove_callback
"""
oid = self._oid
self._propobservers[oid] = func
self._oid += 1
return oid
# Wrapping func in a lambda ensures it can be connected multiple times
# and never gets weakref-gc'ed.
return self._callbacks.connect("pchanged", lambda: func(self))

def remove_callback(self, oid):
"""
Expand All @@ -354,10 +352,7 @@ def remove_callback(self, oid):
--------
add_callback
"""
try:
del self._propobservers[oid]
except KeyError:
pass
self._callbacks.disconnect("pchanged", oid)

def pchanged(self):
"""
Expand All @@ -370,8 +365,7 @@ def pchanged(self):
add_callback
remove_callback
"""
for oid, func in self._propobservers.items():
func(self)
self._callbacks.process("pchanged")

def is_transform_set(self):
"""
Expand Down
4 changes: 1 addition & 3 deletions lib/matplotlib/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def __new__(cls, *args, **kwargs):

def __init__(self, kl, label=None):
self.eventson = False # fire events only if eventson
self._oid = 0 # an observer id
self._propobservers = {} # a dict from oids to funcs
self._callbacks = cbook.CallbackRegistry()
self._remove_method = None
self.set_label(label)

Expand All @@ -29,7 +28,6 @@ def remove(self):
self, scalarp=lambda x: isinstance(x, Artist)):
if c is not None:
c.remove()

if self._remove_method:
self._remove_method(self)

Expand Down