From c9ec42df6342709805a777f3d6fd1b69b3c2910e Mon Sep 17 00:00:00 2001 From: Gonzalo Casas Date: Wed, 3 Apr 2024 00:50:18 +0200 Subject: [PATCH] Ensure no fail at `unsubscribe_by_id` --- CHANGELOG.md | 2 ++ src/compas_eve/event_emitter.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9b5154..b94c28b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +* Ensure calling `off()` or `unsubscribe()` does not fail if the callback is not present in the registered event callbacks. + ### Removed diff --git a/src/compas_eve/event_emitter.py b/src/compas_eve/event_emitter.py index f2dbd61..103c753 100644 --- a/src/compas_eve/event_emitter.py +++ b/src/compas_eve/event_emitter.py @@ -223,11 +223,19 @@ def g(*args, **kwargs): def off(self, event, f): """Removes the function ``f`` from ``event``.""" - self._events[event].pop(f) + with self._event_lock: + if f in self._events[event]: + return self._events[event].pop(f) + + return None def remove_listener(self, event, f): """Removes the function ``f`` from ``event``.""" - self._events[event].pop(f) + with self._event_lock: + if f in self._events[event]: + return self._events[event].pop(f) + + return None def remove_all_listeners(self, event=None): """Remove all listeners attached to ``event``.