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
|
2021-06-24 15:50:27 +00:00
|
|
|
|
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
|
2021-06-24 15:50:27 +00:00
|
|
|
|
2021-11-02 11:58:51 +00:00
|
|
|
from ./protocols/protocol import Handle
|
2021-06-24 15:50:27 +00:00
|
|
|
|
|
|
|
type
|
2023-07-24 15:13:36 +00:00
|
|
|
Assertion = Preserve[Cap]
|
|
|
|
Observe = dataspace.Observe[Cap]
|
2021-09-24 19:25:47 +00:00
|
|
|
Turn = actors.Turn
|
2021-06-24 15:50:27 +00:00
|
|
|
|
2022-03-10 23:30:07 +00:00
|
|
|
Dataspace {.final.} = ref object of Entity
|
|
|
|
index: Index
|
|
|
|
handleMap: Table[Handle, Assertion]
|
|
|
|
|
2022-12-22 04:59:16 +00:00
|
|
|
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
|
2022-12-22 04:59:16 +00:00
|
|
|
if obs.fromPreserve a.value:
|
2022-12-08 08:15:01 +00:00
|
|
|
ds.index.add(turn, obs.pattern, obs.observer)
|
2022-12-22 04:59:16 +00:00
|
|
|
ds.handleMap[h] = a.value
|
2022-03-10 23:30:07 +00:00
|
|
|
|
2022-12-22 04:59:16 +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
|
|
|
|
2022-12-22 04:59:16 +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
|
|
|
|
2023-07-24 15:13:36 +00:00
|
|
|
type BootProc = proc (ds: Cap; turn: var Turn) {.gcsafe.}
|
2021-09-24 19:25:47 +00:00
|
|
|
|
2023-06-30 07:57:54 +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)
|