core: extend synapse: may return Symbol, handle in forward()
This commit is contained in:
parent
bc12966b70
commit
c78e1ac067
1 changed files with 25 additions and 13 deletions
|
|
@ -14,9 +14,9 @@ from scopes import util
|
||||||
from scopes.util import keywords as k, Symbol, Symbols
|
from scopes.util import keywords as k, Symbol, Symbols
|
||||||
|
|
||||||
# keywords (domains, actions, classes, stages, data keys):
|
# keywords (domains, actions, classes, stages, data keys):
|
||||||
k('csys ctl env',
|
k('csys env _meta',
|
||||||
'connect create created data default query set value',
|
'connect create created data default query remove set value',
|
||||||
'c00 e00 s00 rcvr op',
|
'c00 e00 s00 op rcvr',
|
||||||
'initial active retired',
|
'initial active retired',
|
||||||
'address pred succ')
|
'address pred succ')
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@ type Categ = tuple[Symbol, Symbol]
|
||||||
type Proc = Callable[[Message, Scope], Awaitable[Scope | None]]
|
type Proc = Callable[[Message, Scope], Awaitable[Scope | None]]
|
||||||
type Program = Callable[[Scope], Proc]
|
type Program = Callable[[Scope], Proc]
|
||||||
type ProgramSpec = dict[tuple[Symbol, Symbol], Proc]
|
type ProgramSpec = dict[tuple[Symbol, Symbol], Proc]
|
||||||
type Syn = Callable[[Message], Awaitable[None]]
|
type Syn = Callable[[Message], Awaitable[Symbol | None]]
|
||||||
type SynOp = Callable[[Message], Message | None]
|
type SynOp = Callable[[Message], Message | None]
|
||||||
|
|
||||||
type Handler = Callable[[Message, Scope],
|
type Handler = Callable[[Message, Scope],
|
||||||
|
|
@ -41,12 +41,11 @@ def neuron(scope: Scope) -> Mailbox:
|
||||||
return actor.create(process)
|
return actor.create(process)
|
||||||
|
|
||||||
def synapse(rcvr: Mailbox, op: SynOp = lambda x: x) -> Syn:
|
def synapse(rcvr: Mailbox, op: SynOp = lambda x: x) -> Syn:
|
||||||
async def syn(msg: Message) -> None:
|
async def syn(msg: Message) -> Symbol | None:
|
||||||
# op = update_op(msg) (=> return?)
|
|
||||||
# rv = check_return(msg) => return rv
|
|
||||||
msg_new = op(msg)
|
msg_new = op(msg)
|
||||||
if msg_new is not None:
|
if msg_new is not None:
|
||||||
await rcvr.put(msg_new)
|
await rcvr.put(msg_new) # return actor.send(rcvr, msg_new)
|
||||||
|
return None
|
||||||
return syn
|
return syn
|
||||||
|
|
||||||
env_dummy: Mailbox = asyncio.Queue()
|
env_dummy: Mailbox = asyncio.Queue()
|
||||||
|
|
@ -89,7 +88,7 @@ class Scope:
|
||||||
stage: Symbol = k.initial
|
stage: Symbol = k.initial
|
||||||
proc: Proc = field(init=False)
|
proc: Proc = field(init=False)
|
||||||
program: Program = gen_program({})
|
program: Program = gen_program({})
|
||||||
syns: list[Syn] = field(default_factory=list)
|
syns: list[Syn | None] = field(default_factory=list)
|
||||||
environ: Mailbox = env_dummy
|
environ: Mailbox = env_dummy
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
|
|
@ -117,10 +116,23 @@ async def create_zero(env: Mailbox) -> Mailbox:
|
||||||
|
|
||||||
# predefined action handlers and Action sets
|
# predefined action handlers and Action sets
|
||||||
|
|
||||||
def forward() -> Handler:
|
def noop(no_forward: bool = False) -> Handler:
|
||||||
|
async def handle(msg: Message, scope: Scope) -> Message | None:
|
||||||
|
return None if no_forward else msg
|
||||||
|
return handle
|
||||||
|
|
||||||
|
def forward(no_syns_notify: bool = False) -> Handler:
|
||||||
|
do_notify = notify() if no_syns_notify else None
|
||||||
async def handle(msg: Message, scope: Scope) -> None:
|
async def handle(msg: Message, scope: Scope) -> None:
|
||||||
for syn in scope.syns:
|
count = 0
|
||||||
await syn(msg)
|
for idx, syn in enumerate(scope.syns):
|
||||||
|
res = await syn(msg) if syn is not None else None
|
||||||
|
if res == k.remove:
|
||||||
|
scope.syns[idx] = None
|
||||||
|
else:
|
||||||
|
count += 1
|
||||||
|
if count == 0 and do_notify:
|
||||||
|
do_notify(msg, scope)
|
||||||
return handle
|
return handle
|
||||||
|
|
||||||
def value_add() -> Handler:
|
def value_add() -> Handler:
|
||||||
|
|
@ -161,5 +173,5 @@ def std_proc(actions: Actions | None = None, finalize: Handler = forward(),
|
||||||
|
|
||||||
async def notify_created(msg: Message, new: Mailbox, scope: Scope) -> None:
|
async def notify_created(msg: Message, new: Mailbox, scope: Scope) -> None:
|
||||||
msg = Message(msg.domain, k.created, data=msg.data | {k.new: new})
|
msg = Message(msg.domain, k.created, data=msg.data | {k.new: new})
|
||||||
await scope.environ.put(msg)
|
await notify()(msg, scope)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue