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
|
||||
|
||||
# keywords (domains, actions, classes, stages, data keys):
|
||||
k('csys ctl env',
|
||||
'connect create created data default query set value',
|
||||
'c00 e00 s00 rcvr op',
|
||||
k('csys env _meta',
|
||||
'connect create created data default query remove set value',
|
||||
'c00 e00 s00 op rcvr',
|
||||
'initial active retired',
|
||||
'address pred succ')
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ type Categ = tuple[Symbol, Symbol]
|
|||
type Proc = Callable[[Message, Scope], Awaitable[Scope | None]]
|
||||
type Program = Callable[[Scope], 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 Handler = Callable[[Message, Scope],
|
||||
|
|
@ -41,12 +41,11 @@ def neuron(scope: Scope) -> Mailbox:
|
|||
return actor.create(process)
|
||||
|
||||
def synapse(rcvr: Mailbox, op: SynOp = lambda x: x) -> Syn:
|
||||
async def syn(msg: Message) -> None:
|
||||
# op = update_op(msg) (=> return?)
|
||||
# rv = check_return(msg) => return rv
|
||||
async def syn(msg: Message) -> Symbol | None:
|
||||
msg_new = op(msg)
|
||||
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
|
||||
|
||||
env_dummy: Mailbox = asyncio.Queue()
|
||||
|
|
@ -89,7 +88,7 @@ class Scope:
|
|||
stage: Symbol = k.initial
|
||||
proc: Proc = field(init=False)
|
||||
program: Program = gen_program({})
|
||||
syns: list[Syn] = field(default_factory=list)
|
||||
syns: list[Syn | None] = field(default_factory=list)
|
||||
environ: Mailbox = env_dummy
|
||||
|
||||
def __post_init__(self):
|
||||
|
|
@ -117,10 +116,23 @@ async def create_zero(env: Mailbox) -> Mailbox:
|
|||
|
||||
# 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:
|
||||
for syn in scope.syns:
|
||||
await syn(msg)
|
||||
count = 0
|
||||
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
|
||||
|
||||
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:
|
||||
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