forked from Aeolitus/Sephrasto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventBus.py
48 lines (39 loc) · 1.46 KB
/
EventBus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import bisect
class PriotizedCallback():
def __init__(self):
self.priority = 0
self.callback = None
def __lt__(self, other):
return self.priority < other.priority
class EventBus:
actionCallbacks = {}
filterCallbacks = {}
@staticmethod
def addFilter(filterName, callback, priority=0):
if not (filterName in EventBus.filterCallbacks):
EventBus.filterCallbacks[filterName] = []
cb = PriotizedCallback()
cb.priority = priority
cb.callback = callback
bisect.insort(EventBus.filterCallbacks[filterName], cb)
@staticmethod
def applyFilter(filterName, filterValue, paramDict = {}):
if not (filterName in EventBus.filterCallbacks):
return filterValue
for cb in EventBus.filterCallbacks[filterName]:
filterValue = cb.callback(filterValue, paramDict)
return filterValue
@staticmethod
def addAction(actionName, callback, priority=0):
if not (actionName in EventBus.actionCallbacks):
EventBus.actionCallbacks[actionName] = []
cb = PriotizedCallback()
cb.priority = priority
cb.callback = callback
bisect.insort(EventBus.actionCallbacks[actionName], cb)
@staticmethod
def doAction(actionName, paramDict = {}):
if not (actionName in EventBus.actionCallbacks):
return
for cb in EventBus.actionCallbacks[actionName]:
cb.callback(paramDict)