syndicate-nim/src/syndicate/dataspaces.nim

48 lines
1.4 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
2022-03-10 23:30:07 +00:00
import std/[hashes, 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
2021-09-24 19:25:47 +00:00
Observe = dataspace.Observe[Ref]
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; v: Assertion; h: Handle) =
if add(ds.index, turn, v):
var obs: Observe
if obs.fromPreserve v:
ds.index.add(turn, obs.pattern, unembed obs.observer)
ds.handleMap[h] = v
method retract(ds: Dataspace; turn: var Turn; h: Handle) =
try:
let v = ds.handleMap[h]
if remove(ds.index, turn, v):
ds.handleMap.del h
2021-09-24 19:25:47 +00:00
var obs: Observe
2022-03-10 23:30:07 +00:00
if obs.fromPreserve v:
ds.index.remove(turn, obs.pattern, unembed obs.observer)
except KeyError: discard
2021-09-24 19:25:47 +00:00
2022-03-10 23:30:07 +00:00
method message(ds: Dataspace; turn: var Turn; v: Assertion) =
ds.index.deliverMessage(turn, v)
2021-09-24 19:25:47 +00:00
2022-03-10 23:30:07 +00:00
type BootProc = proc (ds: Ref; turn: var Turn) {.gcsafe.}
2021-09-24 19:25:47 +00:00
2022-03-10 23:30:07 +00:00
proc bootDataspace*(name: string; bootProc: BootProc): Actor {.discardable.} =
bootActor(name) do (turn: var Turn):
discard turn.facet.preventInertCheck()
let ds = newRef(turn, Dataspace(index: initIndex()))
bootProc(ds, turn)
2022-06-12 19:07:29 +00:00
proc newDataspace*(turn: var Turn): Ref =
newRef(turn, Dataspace(index: initIndex()))