Commit 42cb9164 authored by Nathan Graule's avatar Nathan Graule 🤾🏻

Add CallMeta for better typing

parent e1217d23
from threading import Thread
from typing import Callable, Any, TypeVar, Optional
from typing import Callable, Any, TypeVar, Optional, GenericMeta
from typing import Iterable
__all__ = ['Call']
......@@ -14,8 +14,13 @@ Rejectable = Callable[[E], None]
Callback = Callable[[Callable, Callable], Any]
class CallMeta(GenericMeta):
pass
class Call:
"""Asynchronously run code, letting further code subscribe to resolved values or failed exceptions."""
__metaclass__ = CallMeta
PENDING = 'PENDING'
RESOLVED = 'RESOLVED'
REJECTED = 'REJECTED'
......@@ -39,6 +44,7 @@ class Call:
"""Chain callback, called with the resolved value of the previous Call.
:param callback: Callback function to be called with the resolved value of the current Call."""
def cb(resolve, reject):
# type: (Callable, Callable) -> None
self.t.join()
......@@ -58,6 +64,7 @@ class Call:
"""Chain callback, called if a failure occurred somewhere in the chain before this.
:param callback: Callback function, called on error further up the chain."""
def cb(resolve, reject):
self.t.join()
if self.status == self.REJECTED:
......@@ -131,12 +138,14 @@ class Call:
:param func: Synchronous function to be called
:param args: Positional arguments to be passed to the function func
:param kwargs: Dictionary arguments to be passed to the function func"""
def cb(resolve, reject):
# type: (Callable, Callable) -> None
try:
resolve(func(*args, **kwargs))
except Exception as e:
reject(e)
return Call(cb)
def _on_resolve(self, data):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment