mirror of
https://github.com/beetbox/beets.git
synced 2026-01-03 22:42:44 +01:00
send event method and decorator for listening
This commit is contained in:
parent
0b9265d815
commit
22be9ea2fb
1 changed files with 25 additions and 0 deletions
|
|
@ -29,3 +29,28 @@ def addEventListener(event, function):
|
|||
if event not in handlers:
|
||||
handlers[event] = [ ] #Empty list to store the handlers
|
||||
handlers[event].append(function)
|
||||
|
||||
def send(event, **arguments):
|
||||
"""Sends an event to all assigned event listeners. Event is the name of
|
||||
the event to send, all other named arguments go to the event handler(s).
|
||||
|
||||
Returns the number of handlers called."""
|
||||
|
||||
if event in handlers:
|
||||
for handler in handlers[event]:
|
||||
handler(**arguments)
|
||||
return len(handlers[event])
|
||||
else:
|
||||
return 0
|
||||
|
||||
def listen(event):
|
||||
"""Decorator method for creating an event listener.
|
||||
|
||||
@events.listen("imported")
|
||||
def importListener(**kwargs):
|
||||
pass"""
|
||||
def helper(funct):
|
||||
addEventListener(event, funct)
|
||||
return funct
|
||||
|
||||
return helper
|
||||
|
|
|
|||
Loading…
Reference in a new issue