Skip to content

Commit

Permalink
Updated all AutoUnload classes to be able to have their callbacks cal…
Browse files Browse the repository at this point in the history
…led directly.
  • Loading branch information
satoon101 committed Dec 6, 2015
1 parent 503b2cc commit 642178b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __call__(self, callback):
self.names, self.callback, *self.args, **self.kwargs)

# Return the original callback
return callback
return self.callback

def _unload_instance(self):
"""Unregister the commands."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def __init__(self, callback):
# Register the filter
self._manager_class.register_filter(self.callback)

def __call__(self, *args):
"""Call the callback."""
self.callback(*args)

def _unload_instance(self):
"""Unregister the filter."""
self._manager_class.unregister_filter(self.callback)
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def __call__(self, callback):
# class name. So, we need to wait until such an entity has been
# created.
_waiting_entity_hooks.append(self)
return self

# Return the callback
return self.callback

@property
def hook_type(self):
Expand Down
12 changes: 6 additions & 6 deletions addons/source-python/packages/source-python/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ def __init__(self, *event_names):
raise ValueError('Event name must be a string.')

self._event_names = event_names
self._callback = None
self.callback = None

def __call__(self, callback):
"""Store the callback and register the events."""
# Store the callback
self._callback = callback
self.callback = callback

# Loop through all event names
for event_name in self._event_names:

# Register the event
event_manager.register_for_event(event_name, self._callback)
event_manager.register_for_event(event_name, self.callback)

# Return the instance so that it unloads properly
return self
# Return the callback
return self.callback

def _unload_instance(self):
"""Unregister the events."""
# Loop through all event names
for event_name in self._event_names:

# Unregister the event
event_manager.unregister_for_event(event_name, self._callback)
event_manager.unregister_for_event(event_name, self.callback)
12 changes: 6 additions & 6 deletions addons/source-python/packages/source-python/events/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,29 @@ class PreEvent(AutoUnload):
def __init__(self, *event_names):
"""Store the event names."""
self._event_names = event_names
self._callback = None
self.callback = None

def __call__(self, callback):
"""Store the callback and register the pre-events."""
# Store the callback
self._callback = callback
self.callback = callback

# Loop through all pre-event names
for event_name in self._event_names:

# Register the pre-event
pre_event_manager.register_for_event(event_name, self._callback)
pre_event_manager.register_for_event(event_name, self.callback)

# Return the instance so that it unloads properly
return self
# Return the callback
return self.callback

def _unload_instance(self):
"""Unregister the pre-events."""
# Loop through all pre-event names
for event_name in self._event_names:

# Unregister the pre-event
pre_event_manager.unregister_for_event(event_name, self._callback)
pre_event_manager.unregister_for_event(event_name, self.callback)


class _PreEventManager(dict):
Expand Down
4 changes: 4 additions & 0 deletions addons/source-python/packages/source-python/hooks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def __init__(self, callback):
self.callback = callback
self._class_instance.append(self.callback)

def __call__(self, *args):
"""Call the callback."""
self.callback(*args)

def _unload_instance(self):
"""Unregister the hook."""
self._class_instance.remove(self.callback)
4 changes: 2 additions & 2 deletions addons/source-python/packages/source-python/memory/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def __call__(self, callback):
# Hook the callback to the Function
self.function.add_hook(self.hook_type, self.callback)

# Return the object
return self
# Return the callback
return self.callback

@property
def hook_type(self):
Expand Down

1 comment on commit 642178b

@Mahi
Copy link
Contributor

@Mahi Mahi commented on 642178b Dec 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! But why return self.callback instead of just return callback? Obviously the speed difference is pretty much insignificant, but regardless, return callback is just a bit faster (no need to do attribute lookup, it's just one LOAD_FAST). At least that's a reason not to prefer return self.callback. Also, using return callback clearly indicates that the returned value is just the original argument and neither __getattr__/__setattr__ nor a callback property could have affected it. These are very minor details, but you explicitly changed return callback to return self.callback, so I was wondering if there's an actual reason to, or just a personal preference/style consistency?

Please sign in to comment.