syndicate-nim/tests/test_dsl.nim

36 lines
1.1 KiB
Nim
Raw Normal View History

2021-09-01 11:44:28 +00:00
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
# SPDX-License-Identifier: Unlicense
2021-06-29 15:18:09 +00:00
2021-09-01 11:44:28 +00:00
import std/asyncdispatch
import preserves
import syndicate
2021-06-29 15:18:09 +00:00
2021-08-28 08:36:30 +00:00
import ./box_and_client
2021-06-29 15:18:09 +00:00
2021-07-08 09:50:13 +00:00
syndicate testDsl:
2021-06-29 15:18:09 +00:00
spawn "box":
2021-08-28 08:36:30 +00:00
field(currentValue, BiggestInt, 0)
publish prsBoxState(currentValue.get)
2021-06-29 15:18:09 +00:00
stopIf currentValue.get == 10:
echo "box: terminating"
2021-09-01 11:44:28 +00:00
onMessage(prsSetBox(?newValue)) do (newValue: int):
# The SetBox message is unpacked to `newValue: int`
2021-06-29 15:18:09 +00:00
echo "box: taking on new value ", newValue
currentValue.set(newValue)
spawn "client":
#stopIf retracted(observe(SetBox, _)):
# echo "client: box has gone"
2021-09-01 11:44:28 +00:00
onAsserted(prsBoxState(?v)) do (v: BiggestInt):
2021-06-29 15:18:09 +00:00
echo "client: learned that box's value is now ", v
2021-08-28 08:36:30 +00:00
send(prsSetBox(v.succ))
2021-09-01 11:44:28 +00:00
onRetracted(prsBoxState(?_)) do (_):
2021-06-29 15:18:09 +00:00
echo "client: box state disappeared"
onStop:
quit(0) # Quit explicitly rather than let the dispatcher run empty.
runForever()
# The dataspace is driven by the async dispatcher.
# Without `runForever` this module would exit immediately.