25 lines
654 B
Python
25 lines
654 B
Python
# scopes.core.actor
|
|
|
|
import asyncio
|
|
from collections.abc import Awaitable, Callable
|
|
from typing import Any, Optional
|
|
|
|
from scopes.util import keywords as k
|
|
|
|
type Behavior = Callable[[Any], Awaitable[Behavior | None]]
|
|
|
|
async def loop(q: asyncio.Queue, bhv: Behavior) -> None:
|
|
while True:
|
|
msg = await q.get()
|
|
if msg is k.quit:
|
|
break
|
|
bhv = await bhv(msg) or bhv
|
|
|
|
def create(bhv: Behavior, group: Optional[asyncio.TaskGroup] = None) -> asyncio.Queue:
|
|
q: asyncio.Queue = asyncio.Queue()
|
|
if group is None:
|
|
asyncio.create_task(loop(q, bhv))
|
|
else:
|
|
group.create_task(loop(q, bhv))
|
|
return q
|
|
|