syndicate-nim/src/syndicate/dataspaces.nim

46 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
2022-12-08 08:15:01 +00:00
Assertion = Preserve[Ref]
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; a: AssertionRef; h: Handle) {.gcsafe.} =
if add(ds.index, turn, a.value):
2022-03-10 23:30:07 +00:00
var obs: Observe
if obs.fromPreserve a.value:
2022-12-08 08:15:01 +00:00
ds.index.add(turn, obs.pattern, obs.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
var obs: Observe
if obs.fromPreserve v:
ds.index.remove(turn, obs.pattern, obs.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
2022-10-26 23:58:32 +00:00
proc newDataspace*(turn: var Turn): Ref =
newRef(turn, Dataspace(index: initIndex()))
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
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()
2022-10-26 23:58:32 +00:00
bootProc(newDataspace(turn), turn)