-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding EventDispatcher (mediator pattern) to decouple your system fro…
…m StepperMotors
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import uuid | ||
|
||
from stepper_motors_juanmf1.BlockingQueueWorker import BlockingQueueWorker | ||
from stepper_motors_juanmf1.ThreadOrderedPrint import tprint | ||
|
||
|
||
class EventDispatcher(BlockingQueueWorker): | ||
MAX_EVENTS = 100 | ||
|
||
def __new__(cls, *args, **kwargs): | ||
""" | ||
Singleton | ||
""" | ||
if not hasattr(cls, 'instance'): | ||
cls.instance = super().__new__(cls) | ||
return cls.instance | ||
|
||
def __init__(self): | ||
super().__init__(self.dispatchMainLoop, jobQueueMaxSize=self.MAX_EVENTS, workerName="EventDispatcher_") | ||
self.events = {} | ||
self.markForUnregister = [] | ||
|
||
def dispatchMainLoop(self, eventName, eventInfo): | ||
# Todo: should I add eventId to callee parameters? | ||
tprint("dispatchMainLoop") | ||
tprint("eventName, eventInfo") | ||
tprint(eventName, eventInfo) | ||
tprint("") | ||
for calleeId, callee in self.events.get(eventName, {}).items(): | ||
callee(calleeId=calleeId, eventInfo=eventInfo) | ||
else: | ||
tprint(f"Missed event {eventName}") | ||
while self.markForUnregister: | ||
self.unregister(self.markForUnregister.pop(0)) | ||
|
||
def register(self, eventName, callee, oneTimeHandler=False): | ||
self.events.setdefault(eventName, {}) | ||
cId = uuid.uuid4() | ||
tprint(f"Register eventName {eventName}; calleeId {cId}") | ||
self.events[eventName][cId] = lambda calleeId, eventInfo: self.callThenUnregister(calleeId, eventInfo, callee) \ | ||
if oneTimeHandler else callee | ||
return cId | ||
|
||
def unregister(self, calleeId): | ||
tprint(f"Unregister calleeId {calleeId}") | ||
for event_dict in self.events.values(): | ||
if calleeId in event_dict: | ||
del event_dict[calleeId] | ||
break | ||
|
||
def callThenUnregister(self, calleeId, eventInfo, callee): | ||
callee(calleeId=calleeId, eventInfo=eventInfo) | ||
self.markForUnregister.append(calleeId) | ||
|
||
def publishMainLoop(self, eventName, eventInfo=None): | ||
self.work([eventName, eventInfo], block=False) |