Skip to content
Open
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
20 changes: 11 additions & 9 deletions udacidrone/drone.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ def register_callback(self, name, fn):

These can be added anywhere in the code and are identical to initializing a callback with the decorator
"""
if name not in self._callbacks:
self._callbacks[name] = []
if fn not in self._callbacks[name]:
self._callbacks[name].append(fn)

callbacks = self._callbacks.setdefault(name, [])
if fn not in callbacks:
callbacks.append(fn)

def remove_callback(self, name, fn):
"""Remove the function, `fn`, as a callback for the message type, `name`
Expand All @@ -364,11 +364,13 @@ def remove_callback(self, name, fn):
self.remove_message_listener(MsgID.GLOBAL_POSITION, global_msg_listener)

"""
if name in self._callbacks:
if fn in self._callbacks[name]:
self._callbacks[name].remove(fn)
if len(self._callbacks[name]) == 0:
del self._callbacks[name]

callbacks = self._callbacks.get(name, [])
if fn in callbacks:
callbacks.remove(fn)

if not len(self._callbacks[name]):
del self._callbacks[name]

def notify_callbacks(self, name, msg):
"""Passes the message to the appropriate listeners"""
Expand Down