2022-03-10 23:30:07 +00:00
|
|
|
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
|
|
|
# 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
|
|
|
|
|
|
|
|
from ./protocols/protocol import Handle
|
|
|
|
|
|
|
|
type
|
2023-12-31 17:15:06 +00:00
|
|
|
Observe = dataspace.Observe
|
2022-03-10 23:30:07 +00:00
|
|
|
Turn = actors.Turn
|
|
|
|
|
|
|
|
type
|
2022-04-23 21:05:48 +00:00
|
|
|
DuringProc* = proc (turn: var Turn; a: Assertion; h: Handle): TurnAction {.gcsafe.}
|
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]
|
|
|
|
|
2022-12-22 04:59:16 +00:00
|
|
|
method publish(de: DuringEntity; turn: var Turn; a: AssertionRef; h: Handle) =
|
|
|
|
let action = de.cb(turn, a.value, h)
|
2022-03-10 23:30:07 +00:00
|
|
|
# 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
|
|
|
|
freshen(turn, action)
|
|
|
|
of act:
|
|
|
|
raiseAssert("during: duplicate handle in publish: " & $h)
|
|
|
|
|
2022-03-12 16:08:22 +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
|
|
|
|
2023-07-24 15:13:36 +00:00
|
|
|
proc observe*(turn: var Turn; ds: Cap; pat: Pattern; e: Entity): Handle =
|
|
|
|
publish(turn, ds, Observe(pattern: pat, observer: newCap(turn, e)))
|