summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-10-18 11:53:02 +0200
committerGitHub <noreply@github.com>2019-10-18 11:53:02 +0200
commitd98029ea89ffc19a80a00299c1f2aff06f7b778d (patch)
tree2156796e5e8bef890dc3c7b6c82c8d61423ada8a /synapse/util
parentAdd missing BOOLEAN_COLUMNs to synapse_port_db (#6216) (diff)
parentPort synapse/rest/client/_base.py to async/await (diff)
downloadsynapse-d98029ea89ffc19a80a00299c1f2aff06f7b778d.tar.xz
Merge pull request #6196 from matrix-org/erikj/await
Move rest/admin to use async/await.
Diffstat (limited to 'synapse/util')
-rw-r--r--synapse/util/async_helpers.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/synapse/util/async_helpers.py b/synapse/util/async_helpers.py
index 0d3bdd88ce..804dbca443 100644
--- a/synapse/util/async_helpers.py
+++ b/synapse/util/async_helpers.py
@@ -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)