added any_state/event in handler
This commit is contained in:
parent
5d5fbc91e1
commit
8391cec49e
@ -164,23 +164,37 @@ class EventHandler:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.event_handlers = {}
|
self.event_handlers = {}
|
||||||
self.state_handlers = {}
|
self.state_handlers = {}
|
||||||
|
self.any_events = []
|
||||||
|
self.any_state = []
|
||||||
|
|
||||||
async def call_event(self, status: SymbolStatus, event: Event):
|
async def call_event(self, event: Event, status: SymbolStatus):
|
||||||
value = event.kind.value
|
value = event.kind.value
|
||||||
if value in self.event_handlers:
|
if value in self.event_handlers:
|
||||||
for h in self.event_handlers[value]:
|
for h in self.event_handlers[value]:
|
||||||
if inspect.iscoroutinefunction(h):
|
if inspect.iscoroutinefunction(h):
|
||||||
await h(status, event)
|
await h(event, status)
|
||||||
else:
|
else:
|
||||||
h(status, event)
|
h(event, status)
|
||||||
|
|
||||||
async def call_state(self, status: SymbolStatus, pw: PositionWrapper):
|
for h in self.any_events:
|
||||||
if pw.state in self.state_handlers:
|
|
||||||
for h in self.state_handlers[pw.state]:
|
|
||||||
if inspect.iscoroutinefunction(h):
|
if inspect.iscoroutinefunction(h):
|
||||||
await h(status, pw)
|
await h(event, status)
|
||||||
else:
|
else:
|
||||||
h(status, pw)
|
h(event, status)
|
||||||
|
|
||||||
|
async def call_state(self, state: PositionState, status: SymbolStatus):
|
||||||
|
if state in self.state_handlers:
|
||||||
|
for h in self.state_handlers[state]:
|
||||||
|
if inspect.iscoroutinefunction(h):
|
||||||
|
await h(status)
|
||||||
|
else:
|
||||||
|
h(status)
|
||||||
|
|
||||||
|
for h in self.any_state:
|
||||||
|
if inspect.iscoroutinefunction(h):
|
||||||
|
await h(status)
|
||||||
|
else:
|
||||||
|
h(status)
|
||||||
|
|
||||||
def on_event(self, kind: EventKind):
|
def on_event(self, kind: EventKind):
|
||||||
value = kind.value
|
value = kind.value
|
||||||
@ -203,3 +217,15 @@ class EventHandler:
|
|||||||
return handler
|
return handler
|
||||||
|
|
||||||
return registerhandler
|
return registerhandler
|
||||||
|
|
||||||
|
def on_any_event(self):
|
||||||
|
def registerhandle(handler):
|
||||||
|
self.any_events.append(handler)
|
||||||
|
return handler
|
||||||
|
return registerhandle
|
||||||
|
|
||||||
|
def on_any_state(self):
|
||||||
|
def registerhandle(handler):
|
||||||
|
self.any_state.append(handler)
|
||||||
|
return handler
|
||||||
|
return registerhandle
|
Loading…
Reference in New Issue
Block a user