Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event hooks, enable multiple event handlers
- Loading branch information
Showing
5 changed files
with
61 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import traceback | ||
|
||
from . import _internal | ||
|
||
|
||
class EventEmitter(object): | ||
def __init__(self, name): | ||
self.name = name | ||
self.callbacks = set() | ||
self._executor = _internal.call_as_function_or_method | ||
|
||
def register(self, func): | ||
self.callbacks.add(func) | ||
return func | ||
|
||
def unregister(self, func): | ||
try: | ||
self.callbacks.remove(func) | ||
return True | ||
except KeyError: | ||
return False | ||
|
||
def emit(self, *args, **kwargs): | ||
#print('EventEmitter("%s").emit called' % self.name) | ||
for callback in self.callbacks: | ||
try: | ||
self._executor(callback, *args, **kwargs) | ||
except Exception: | ||
traceback.print_exc() | ||
|
||
__call__ = register | ||
|
||
|
||
before_start = EventEmitter('before_start') | ||
on_notification = EventEmitter('on_notification') | ||
on_sleep = EventEmitter('on_sleep') | ||
on_wake = EventEmitter('on_wake') | ||
before_quit = EventEmitter('before_quit') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters