added event pause/resume functionality

This commit is contained in:
itdominator 2023-10-19 20:57:04 -05:00
parent bfd526ad7c
commit f27f738351
1 changed files with 19 additions and 0 deletions

View File

@ -13,7 +13,20 @@ class EventSystem(Singleton):
def __init__(self):
self.subscribers = defaultdict(list)
self._is_paused = False
self._subscribe_to_events()
def _subscribe_to_events(self):
self.subscribe("pause_event_processing", self._pause_processing_events)
self.subscribe("resume_event_processing", self._resume_processing_events)
def _pause_processing_events(self):
self._is_paused = True
def _resume_processing_events(self):
self._is_paused = False
def subscribe(self, event_type, fn):
self.subscribers[event_type].append(fn)
@ -25,6 +38,9 @@ class EventSystem(Singleton):
self.subscribers.pop(event_type, None)
def emit(self, event_type, data = None):
if self._is_paused and event_type != "resume_event_processing":
return
if event_type in self.subscribers:
for fn in self.subscribers[event_type]:
if data:
@ -36,6 +52,9 @@ class EventSystem(Singleton):
fn()
def emit_and_await(self, event_type, data = None):
if self._is_paused and event_type != "resume_event_processing":
return
""" NOTE: Should be used when signal has only one listener and vis-a-vis """
if event_type in self.subscribers:
response = None