mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 23:34:56 -04:00
Add maybe_awaitable and fix __init__ bugs
This commit is contained in:
parent
c3b0fbe9c3
commit
3c2d6c708c
2 changed files with 34 additions and 2 deletions
|
@ -21,6 +21,8 @@ from typing import Dict, Sequence, Set, Union
|
|||
|
||||
from six.moves import range
|
||||
|
||||
import attr
|
||||
|
||||
from twisted.internet import defer
|
||||
from twisted.internet.defer import CancelledError
|
||||
from twisted.python import failure
|
||||
|
@ -483,3 +485,30 @@ def timeout_deferred(deferred, timeout, reactor, on_timeout_cancel=None):
|
|||
deferred.addCallbacks(success_cb, failure_cb)
|
||||
|
||||
return new_d
|
||||
|
||||
|
||||
@attr.s(slots=True, frozen=True)
|
||||
class DoneAwaitable(object):
|
||||
"""Simple awaitable that returns the provided value.
|
||||
"""
|
||||
|
||||
value = attr.ib()
|
||||
|
||||
def __await__(self):
|
||||
return self
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
raise StopIteration(self.value)
|
||||
|
||||
|
||||
def maybe_awaitable(value):
|
||||
"""Convert a value to an awaitable if not already an awaitable.
|
||||
"""
|
||||
|
||||
if hasattr(value, "__await__"):
|
||||
return value
|
||||
|
||||
return DoneAwaitable(value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue