| 1 | from gi.repository import Dex, GLib |
| 2 | import asyncio |
| 3 | |
| 4 | _orig_future_init = Dex.Future.__init__ |
| 5 | |
| 6 | def _future__await__(self): |
| 7 | loop = asyncio.get_event_loop() |
| 8 | if loop is None: |
| 9 | raise RuntimeError("No asyncio event loop running") |
| 10 | |
| 11 | async_fut = loop.create_future() |
| 12 | |
| 13 | def on_done(dex_fut, user_data=None): |
| 14 | try: |
| 15 | value = dex_fut.get_value() |
| 16 | except Exception as e: |
| 17 | loop.call_soon_threadsafe(async_fut.set_exception, e) |
| 18 | else: |
| 19 | loop.call_soon_threadsafe(async_fut.set_result, value) |
| 20 | finally: |
| 21 | async_fut.dex_future = None |
| 22 | |
| 23 | async_fut.dex_future = Dex.Future.finally_(self, on_done) |
| 24 | |
| 25 | return (yield from async_fut.__await__()) |
| 26 | |
| 27 | Dex.Future.__await__ = _future__await__ |
| 28 | |
| 29 | def _future_to_asyncio(self): |
| 30 | loop = asyncio.get_event_loop() |
| 31 | if loop is None: |
| 32 | raise RuntimeError("No asyncio event loop") |
| 33 | return self |
| 34 | |
| 35 | Dex.Future.to_asyncio = _future_to_asyncio |