Add onRetract sugar to scope of onPublish
This commit is contained in:
parent
dc420c1a22
commit
515ef22805
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
import std/macros
|
import std/macros
|
||||||
import preserves
|
import preserves
|
||||||
import syndicate/[actors, patterns]
|
import syndicate/[actors, dataspaces, patterns]
|
||||||
export patterns
|
export patterns
|
||||||
|
|
||||||
proc wrapPublishHandler(handler: NimNode): NimNode =
|
proc wrapPublishHandler(handler: NimNode): NimNode =
|
||||||
|
@ -30,15 +30,20 @@ proc wrapPublishHandler(handler: NimNode): NimNode =
|
||||||
var
|
var
|
||||||
varSectionOuter = newNimNode(nnkVarSection, handler).add(
|
varSectionOuter = newNimNode(nnkVarSection, handler).add(
|
||||||
newIdentDefs(valuesSym, valuesTuple))
|
newIdentDefs(valuesSym, valuesTuple))
|
||||||
body = newStmtList(varSectionInner, handler[6])
|
publishBody = newStmtList(varSectionInner, handler[6])
|
||||||
turnSym = ident"turn"
|
turnSym = ident"turn"
|
||||||
handleSym = ident"handle"
|
handleSym = ident"handle"
|
||||||
handlerSym = genSym(nskProc, "publish")
|
handlerSym = genSym(nskProc, "publish")
|
||||||
|
onRetractIdent = ident"onRetract"
|
||||||
quote do:
|
quote do:
|
||||||
proc `handlerSym`(_: Entity; `turnSym`: var Turn; bindings: Assertion; `handleSym`: Handle) =
|
proc `handlerSym`(entity: Entity; `turnSym`: var Turn; bindings: Assertion; `handleSym`: Handle) =
|
||||||
`varSectionOuter`
|
`varSectionOuter`
|
||||||
if fromPreserve(`valuesSym`, bindings):
|
if fromPreserve(`valuesSym`, bindings):
|
||||||
`body`
|
template `onRetractIdent`(retractBody: untyped): untyped =
|
||||||
|
proc callback(e: Entity; turn: var Turn; h: Handle) =
|
||||||
|
retractBody
|
||||||
|
entity.retractImpl = callback # TODO: fine to clobber previous callback?
|
||||||
|
`publishBody`
|
||||||
|
|
||||||
proc wrapMessageHandler(handler: NimNode): NimNode =
|
proc wrapMessageHandler(handler: NimNode): NimNode =
|
||||||
handler.expectKind nnkDo
|
handler.expectKind nnkDo
|
||||||
|
|
|
@ -10,17 +10,9 @@ from syndicate/protocols/protocol import Handle
|
||||||
|
|
||||||
from os import getCurrentProcessId
|
from os import getCurrentProcessId
|
||||||
|
|
||||||
when defined(linux):
|
|
||||||
proc getentropy(buf: pointer; bufLen: csize_t): cint {.
|
|
||||||
importc, header: "sys/random.h".}
|
|
||||||
|
|
||||||
proc mint(): SturdyRef =
|
proc mint(): SturdyRef =
|
||||||
|
var key: array[16, byte]
|
||||||
#var key: array[32, byte]
|
mint(key, "syndicate")
|
||||||
#doAssert getEntropy(addr key[0], csize_t key.len) == 0
|
|
||||||
#mint(key, "syndicate")
|
|
||||||
let pr = parsePreserves("""<ref "syndicate" [] #x"a6480df5306611ddd0d3882b546e1977">""", Ref)
|
|
||||||
doAssert fromPreserve(result, pr)
|
|
||||||
|
|
||||||
waitFor runActor("chat") do (turn: var Turn):
|
waitFor runActor("chat") do (turn: var Turn):
|
||||||
|
|
||||||
|
@ -40,12 +32,12 @@ waitFor runActor("chat") do (turn: var Turn):
|
||||||
|
|
||||||
updateUsername(turn, "user" & $getCurrentProcessId())
|
updateUsername(turn, "user" & $getCurrentProcessId())
|
||||||
|
|
||||||
onPublish(turn, ds, Present ? {0: `?`()}) do (username: string):
|
onPublish(turn, ds, Present ? {0: `?*`()}) do (username: string):
|
||||||
echo username, " arrived"
|
echo username, " arrived"
|
||||||
#onRetract:
|
onRetract:
|
||||||
# echo username, " left"
|
echo username, " left"
|
||||||
|
|
||||||
onMessage(turn, ds, Says ? {0: `?`(), 1: `?`()}) do (who: string; what: string):
|
onMessage(turn, ds, Says ? {0: `?*`(), 1: `?*`()}) do (who: string; what: string):
|
||||||
echo who, ": ", what
|
echo who, ": ", what
|
||||||
|
|
||||||
message(turn, ds, Says(who: username, what: "hello"))
|
message(turn, ds, Says(who: username, what: "hello"))
|
||||||
|
|
Loading…
Reference in New Issue