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

Add CallMeta for better typing

parent e1217d23
from threading import Thread from threading import Thread
from typing import Callable, Any, TypeVar, Optional from typing import Callable, Any, TypeVar, Optional, GenericMeta
from typing import Iterable from typing import Iterable
__all__ = ['Call'] __all__ = ['Call']
...@@ -14,8 +14,13 @@ Rejectable = Callable[[E], None] ...@@ -14,8 +14,13 @@ Rejectable = Callable[[E], None]
Callback = Callable[[Callable, Callable], Any] Callback = Callable[[Callable, Callable], Any]
class CallMeta(GenericMeta):
pass
class Call: class Call:
"""Asynchronously run code, letting further code subscribe to resolved values or failed exceptions.""" """Asynchronously run code, letting further code subscribe to resolved values or failed exceptions."""
__metaclass__ = CallMeta
PENDING = 'PENDING' PENDING = 'PENDING'
RESOLVED = 'RESOLVED' RESOLVED = 'RESOLVED'
REJECTED = 'REJECTED' REJECTED = 'REJECTED'
...@@ -29,8 +34,8 @@ class Call: ...@@ -29,8 +34,8 @@ class Call:
:param callback: Callback function. Must have (resolve, reject) functions.""" :param callback: Callback function. Must have (resolve, reject) functions."""
self.status = self.PENDING self.status = self.PENDING
self.data = None # type: T self.data = None # type: T
self.error = None # type: E self.error = None # type: E
self.t = Thread(target=callback, args=(self._on_resolve, self._on_rejected)) self.t = Thread(target=callback, args=(self._on_resolve, self._on_rejected))
self.t.start() self.t.start()
...@@ -39,6 +44,7 @@ class Call: ...@@ -39,6 +44,7 @@ class Call:
"""Chain callback, called with the resolved value of the previous 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.""" :param callback: Callback function to be called with the resolved value of the current Call."""
def cb(resolve, reject): def cb(resolve, reject):
# type: (Callable, Callable) -> None # type: (Callable, Callable) -> None
self.t.join() self.t.join()
...@@ -58,6 +64,7 @@ class Call: ...@@ -58,6 +64,7 @@ class Call:
"""Chain callback, called if a failure occurred somewhere in the chain before this. """Chain callback, called if a failure occurred somewhere in the chain before this.
:param callback: Callback function, called on error further up the chain.""" :param callback: Callback function, called on error further up the chain."""
def cb(resolve, reject): def cb(resolve, reject):
self.t.join() self.t.join()
if self.status == self.REJECTED: if self.status == self.REJECTED:
...@@ -131,12 +138,14 @@ class Call: ...@@ -131,12 +138,14 @@ class Call:
:param func: Synchronous function to be called :param func: Synchronous function to be called
:param args: Positional arguments to be passed to the function func :param args: Positional arguments to be passed to the function func
:param kwargs: Dictionary arguments to be passed to the function func""" :param kwargs: Dictionary arguments to be passed to the function func"""
def cb(resolve, reject): def cb(resolve, reject):
# type: (Callable, Callable) -> None # type: (Callable, Callable) -> None
try: try:
resolve(func(*args, **kwargs)) resolve(func(*args, **kwargs))
except Exception as e: except Exception as e:
reject(e) reject(e)
return Call(cb) return Call(cb)
def _on_resolve(self, data): 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