2024-03-19 15:06:14 +00:00
|
|
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
2022-03-10 23:30:07 +00:00
|
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
|
2022-03-12 16:08:22 +00:00
|
|
|
import std/[hashes, tables]
|
2022-03-10 23:30:07 +00:00
|
|
|
import preserves
|
|
|
|
import ./actors, ./patterns, ./protocols/dataspace
|
|
|
|
|
|
|
|
type
|
2024-03-08 15:40:07 +00:00
|
|
|
DuringProc* = proc (turn: var Turn; a: Value; h: Handle): TurnAction
|
2022-03-10 23:30:07 +00:00
|
|
|
DuringActionKind = enum null, dead, act
|
|
|
|
DuringAction = object
|
|
|
|
case kind: DuringActionKind
|
|
|
|
of null, dead: discard
|
|
|
|
of act:
|
|
|
|
action: TurnAction
|
|
|
|
DuringEntity {.final.}= ref object of Entity
|
|
|
|
cb: DuringProc
|
|
|
|
assertionMap: Table[Handle, DuringAction]
|
|
|
|
|
2024-03-08 15:40:07 +00:00
|
|
|
method publish(de: DuringEntity; turn: var Turn; a: AssertionRef; h: Handle) =
|
2024-03-19 15:06:14 +00:00
|
|
|
discard inFacet(turn) do (turn: var Turn):
|
|
|
|
let action = de.cb(turn, a.value, h)
|
|
|
|
# assert(not action.isNil "should have put in a no-op action")
|
|
|
|
let g = de.assertionMap.getOrDefault h
|
|
|
|
case g.kind
|
|
|
|
of null:
|
|
|
|
de.assertionMap[h] = DuringAction(kind: act, action: action)
|
|
|
|
of dead:
|
|
|
|
de.assertionMap.del h
|
|
|
|
action(turn)
|
|
|
|
of act:
|
|
|
|
raiseAssert("during: duplicate handle in publish: " & $h)
|
2022-03-10 23:30:07 +00:00
|
|
|
|
2024-03-08 15:40:07 +00:00
|
|
|
method retract(de: DuringEntity; turn: var Turn; h: Handle) =
|
2022-03-10 23:30:07 +00:00
|
|
|
let g = de.assertionMap.getOrDefault h
|
|
|
|
case g.kind
|
|
|
|
of null:
|
|
|
|
de.assertionMap[h] = DuringAction(kind: dead)
|
|
|
|
of dead:
|
|
|
|
raiseAssert("during: duplicate handle in retract: " & $h)
|
|
|
|
of act:
|
|
|
|
de.assertionMap.del h
|
2022-04-23 21:05:48 +00:00
|
|
|
if not g.action.isNil:
|
|
|
|
g.action(turn)
|
2022-03-10 23:30:07 +00:00
|
|
|
|
2022-03-12 16:08:22 +00:00
|
|
|
proc during*(cb: DuringProc): DuringEntity = DuringEntity(cb: cb)
|
2022-03-10 23:30:07 +00:00
|
|
|
|
2024-03-08 15:40:07 +00:00
|
|
|
proc observe*(turn: var Turn; ds: Cap; pat: Pattern; e: Entity): Handle =
|
2024-01-07 22:11:59 +00:00
|
|
|
publish(turn, ds, Observe(pattern: pat, observer: newCap(turn, e)))
|