syndicate-nim/src/syndicate/dataspaces.nim

51 lines
1.7 KiB
Nim
Raw Normal View History

2022-03-10 23:30:07 +00:00
# SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway
2021-09-01 11:44:28 +00:00
# SPDX-License-Identifier: Unlicense
2024-01-01 18:18:30 +00:00
import std/[hashes, options, tables]
2021-07-08 09:50:13 +00:00
import preserves
2022-03-10 23:30:07 +00:00
import ./actors, ./protocols/dataspace, ./skeletons
from ./protocols/protocol import Handle
type
2023-12-31 17:15:06 +00:00
Assertion = Value
Observe = dataspace.Observe
2021-09-24 19:25:47 +00:00
Turn = actors.Turn
2022-03-10 23:30:07 +00:00
Dataspace {.final.} = ref object of Entity
index: Index
handleMap: Table[Handle, Assertion]
method publish(ds: Dataspace; turn: var Turn; a: AssertionRef; h: Handle) {.gcsafe.} =
if add(ds.index, turn, a.value):
2024-01-01 18:18:30 +00:00
var obs = a.value.preservesTo(Observe)
2024-01-07 22:11:59 +00:00
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
2022-03-10 23:30:07 +00:00
method retract(ds: Dataspace; turn: var Turn; h: Handle) {.gcsafe.} =
2023-07-20 17:31:02 +00:00
let v = ds.handleMap[h]
if remove(ds.index, turn, v):
ds.handleMap.del h
2024-01-01 18:18:30 +00:00
var obs = v.preservesTo(Observe)
2024-01-07 22:11:59 +00:00
if obs.isSome and obs.get.observer of Cap:
ds.index.remove(turn, obs.get.pattern, Cap(obs.get.observer))
2021-09-24 19:25:47 +00:00
method message(ds: Dataspace; turn: var Turn; a: AssertionRef) {.gcsafe.} =
ds.index.deliverMessage(turn, a.value)
2021-09-24 19:25:47 +00:00
2023-07-24 15:13:36 +00:00
proc newDataspace*(turn: var Turn): Cap =
newCap(turn, Dataspace(index: initIndex()))
2022-10-26 23:58:32 +00:00
type BootProc = proc (turn: var Turn; ds: Cap) {.gcsafe.}
type DeprecatedBootProc = proc (ds: Cap; turn: var Turn) {.gcsafe.}
2021-09-24 19:25:47 +00:00
proc bootDataspace*(name: string; bootProc: BootProc): Actor =
2022-03-10 23:30:07 +00:00
bootActor(name) do (turn: var Turn):
discard turn.facet.preventInertCheck()
bootProc(turn, newDataspace(turn))
proc bootDataspace*(name: string; bootProc: DeprecatedBootProc): Actor {.deprecated.} =
bootDataspace(name) do (turn: var Turn, ds: Cap):
bootProc(ds, turn)