mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-06-13 12:15:09 +03:00
run method calls asynchronously
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import asyncio, time
|
||||
from typing import Awaitable, Callable
|
||||
from typing import Any, Callable, Coroutine
|
||||
import random
|
||||
|
||||
from .localplatform import ON_WINDOWS
|
||||
@@ -7,7 +7,7 @@ from .localplatform import ON_WINDOWS
|
||||
BUFFER_LIMIT = 2 ** 20 # 1 MiB
|
||||
|
||||
class UnixSocket:
|
||||
def __init__(self, on_new_message: Callable[[str], Awaitable[str|None]]):
|
||||
def __init__(self, on_new_message: Callable[[str], Coroutine[Any, Any, Any]]):
|
||||
'''
|
||||
on_new_message takes 1 string argument.
|
||||
It's return value gets used, if not None, to write data to the socket.
|
||||
@@ -93,18 +93,17 @@ class UnixSocket:
|
||||
|
||||
async def _listen_for_method_call(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
|
||||
while True:
|
||||
|
||||
def _(task: asyncio.Task[str|None]):
|
||||
res = task.result()
|
||||
if res is not None:
|
||||
asyncio.create_task(self._write_single_line(writer, res))
|
||||
|
||||
line = await self._read_single_line(reader)
|
||||
|
||||
try:
|
||||
res = await self.on_new_message(line)
|
||||
except Exception:
|
||||
return
|
||||
|
||||
if res != None:
|
||||
await self._write_single_line(writer, res)
|
||||
asyncio.create_task(self.on_new_message(line)).add_done_callback(_)
|
||||
|
||||
class PortSocket (UnixSocket):
|
||||
def __init__(self, on_new_message: Callable[[str], Awaitable[str|None]]):
|
||||
def __init__(self, on_new_message: Callable[[str], Coroutine[Any, Any, Any]]):
|
||||
'''
|
||||
on_new_message takes 1 string argument.
|
||||
It's return value gets used, if not None, to write data to the socket.
|
||||
|
||||
@@ -26,6 +26,7 @@ class PluginWrapper:
|
||||
self.name = json["name"]
|
||||
self.author = json["author"]
|
||||
self.flags = json["flags"]
|
||||
|
||||
self.passive = not path.isfile(self.file)
|
||||
|
||||
self.log = getLogger("plugin")
|
||||
@@ -43,15 +44,18 @@ class PluginWrapper:
|
||||
|
||||
async def _response_listener(self):
|
||||
while True:
|
||||
line = await self._socket.read_single_line()
|
||||
if line != None:
|
||||
res = loads(line)
|
||||
if res["id"] == 0:
|
||||
create_task(self.emitted_message_callback(res["payload"]))
|
||||
return
|
||||
self._method_call_requests.pop(res["id"]).set_result(res)
|
||||
try:
|
||||
line = await self._socket.read_single_line()
|
||||
if line != None:
|
||||
res = loads(line)
|
||||
if res["id"] == "0":
|
||||
create_task(self.emitted_message_callback(res["payload"]))
|
||||
return
|
||||
self._method_call_requests.pop(res["id"]).set_result(res)
|
||||
except:
|
||||
pass
|
||||
|
||||
async def set_emitted_message_callback(self, callback: Callable[[Dict[Any, Any]], Coroutine[Any, Any, Any]]):
|
||||
def set_emitted_message_callback(self, callback: Callable[[Dict[Any, Any]], Coroutine[Any, Any, Any]]):
|
||||
self.emitted_message_callback = callback
|
||||
|
||||
async def execute_method(self, method_name: str, kwargs: Dict[Any, Any]):
|
||||
@@ -69,7 +73,7 @@ class PluginWrapper:
|
||||
if self.passive:
|
||||
return self
|
||||
Process(target=self.sandboxed_plugin.initialize, args=[self._socket]).start()
|
||||
self.listener_task = create_task(self._response_listener())
|
||||
self._listener_task = create_task(self._response_listener())
|
||||
return self
|
||||
|
||||
def stop(self):
|
||||
|
||||
@@ -128,6 +128,6 @@ class SandboxedPlugin:
|
||||
|
||||
async def emit_message(self, message: Dict[Any, Any]):
|
||||
await self._socket.write_single_line(dumps({
|
||||
"id": 0,
|
||||
"id": "0",
|
||||
"payload": message
|
||||
}))
|
||||
Reference in New Issue
Block a user