diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index e3b02309f4..14a8279b3e 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -628,14 +628,17 @@ class Cache: # Cache Layer API {{{ @write_api - def add_listener(self, event_callback_function): + def add_listener(self, event_callback_function, check_already_added=False): ''' Register a callback function that will be called after certain actions are taken on this database. The function must take three arguments: (:class:`EventType`, library_id, event_type_specific_data) ''' self.event_dispatcher.library_id = getattr(self, 'server_library_id', self.library_id) + if check_already_added and event_callback_function in self.event_dispatcher: + return False self.event_dispatcher.add_listener(event_callback_function) + return True @write_api def remove_listener(self, event_callback_function): diff --git a/src/calibre/db/listeners.py b/src/calibre/db/listeners.py index 20b0c58a54..f8d2f4a151 100644 --- a/src/calibre/db/listeners.py +++ b/src/calibre/db/listeners.py @@ -70,6 +70,10 @@ class EventDispatcher(Thread): with suppress(ValueError): self.refs.remove(ref) + def __contains__(self, callback): + ref = weakref.ref(callback) + return ref in self.refs + def __call__(self, event_name, *args): if self.activated: self.queue.put((event_name, self.library_id, args))