Added a new argument once to the error handler registration that decides whether the following callback notifications should be made after a successful callback notification before in loop

This commit is contained in:
andrew000 2022-09-10 01:30:38 +03:00
parent 4dbfb52a39
commit 09448f1b71
No known key found for this signature in database
GPG key ID: D332A306AAA27181
2 changed files with 23 additions and 6 deletions

View file

@ -1200,32 +1200,45 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
return decorator
def register_errors_handler(self, callback, *custom_filters, exception=None, run_task=None, **kwargs):
def register_errors_handler(self,
callback,
*custom_filters,
exception=None,
run_task=None,
once: typing.Optional[bool] = None,
**kwargs):
"""
Register handler for errors
:param callback:
:param exception: you can make handler for specific errors type
:param run_task: run callback in task (no wait results)
:param once: run handler only once
"""
filters_set = self.filters_factory.resolve(self.errors_handlers,
*custom_filters,
exception=exception,
**kwargs)
self.errors_handlers.register(self._wrap_async_task(callback, run_task), filters_set)
self.errors_handlers.register(self._wrap_async_task(callback, run_task), filters_set, once)
def errors_handler(self, *custom_filters, exception=None, run_task=None, **kwargs):
def errors_handler(self,
*custom_filters,
exception=None,
run_task=None,
once: typing.Optional[bool] = None,
**kwargs):
"""
Decorator for errors handler
:param exception: you can make handler for specific errors type
:param run_task: run callback in task (no wait results)
:param once: run handler only once
:return:
"""
def decorator(callback):
self.register_errors_handler(self._wrap_async_task(callback, run_task),
*custom_filters, exception=exception, **kwargs)
*custom_filters, exception=exception, once=once, **kwargs)
return callback
return decorator

View file

@ -43,7 +43,7 @@ class Handler:
self.handlers: typing.List[Handler.HandlerObj] = []
self.middleware_key = middleware_key
def register(self, handler, filters=None, index=None):
def register(self, handler, filters=None, index=None, once: typing.Optional[bool] = None):
"""
Register callback
@ -52,6 +52,7 @@ class Handler:
:param handler: coroutine
:param filters: list of filters
:param index: you can reorder handlers
:param once: if True, handler will be called only once
"""
from .filters import get_filters_spec
@ -67,6 +68,9 @@ class Handler:
else:
self.handlers.insert(index, record)
if once is not None:
self.once = once
def unregister(self, handler):
"""
Remove handler
@ -116,7 +120,7 @@ class Handler:
response = await handler_obj.handler(*args, **partial_data)
if response is not None:
results.append(response)
if self.once:
if self.once is True:
break
except SkipHandler:
continue