51 lines
1.6 KiB
Nim
51 lines
1.6 KiB
Nim
# SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
import std/[hashes, options, tables]
|
|
import preserves
|
|
import ./actors, ./protocols/dataspace, ./skeletons
|
|
|
|
from ./protocols/protocol import Handle
|
|
|
|
type
|
|
Assertion = Value
|
|
Observe = dataspace.Observe
|
|
Turn = actors.Turn
|
|
|
|
Dataspace {.final.} = ref object of Entity
|
|
index: Index
|
|
handleMap: Table[Handle, Assertion]
|
|
|
|
method publish(ds: Dataspace; turn: var Turn; a: AssertionRef; h: Handle) =
|
|
if add(ds.index, turn, a.value):
|
|
var obs = a.value.preservesTo(Observe)
|
|
if obs.isSome and obs.get.observer of Cap:
|
|
ds.index.add(turn, obs.get.pattern, Cap(obs.get.observer))
|
|
ds.handleMap[h] = a.value
|
|
|
|
method retract(ds: Dataspace; turn: var Turn; h: Handle) =
|
|
let v = ds.handleMap[h]
|
|
if remove(ds.index, turn, v):
|
|
ds.handleMap.del h
|
|
var obs = v.preservesTo(Observe)
|
|
if obs.isSome and obs.get.observer of Cap:
|
|
ds.index.remove(turn, obs.get.pattern, Cap(obs.get.observer))
|
|
|
|
method message(ds: Dataspace; turn: var Turn; a: AssertionRef) =
|
|
ds.index.deliverMessage(turn, a.value)
|
|
|
|
proc newDataspace*(turn: var Turn): Cap =
|
|
newCap(turn, Dataspace(index: initIndex()))
|
|
|
|
type BootProc = proc (turn: var Turn; ds: Cap) {.closure.}
|
|
type DeprecatedBootProc = proc (ds: Cap; turn: var Turn) {.closure.}
|
|
|
|
proc bootDataspace*(name: string; bootProc: BootProc): Actor =
|
|
bootActor(name) do (turn: var Turn):
|
|
turn.preventInertCheck()
|
|
bootProc(turn, newDataspace(turn))
|
|
|
|
proc bootDataspace*(name: string; bootProc: DeprecatedBootProc): Actor {.deprecated.} =
|
|
bootDataspace(name) do (turn: var Turn, ds: Cap):
|
|
bootProc(ds, turn)
|