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