2021-06-29 15:18:09 +00:00
|
|
|
# SPDX-License-Identifier: ISC
|
|
|
|
|
|
|
|
import asyncdispatch
|
2021-07-09 13:26:22 +00:00
|
|
|
import preserves, preserves/records
|
|
|
|
import syndicate
|
2021-06-29 15:18:09 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
BoxState = RecordClass(label: symbol"box-state", arity: 1)
|
|
|
|
SetBox = RecordClass(label: symbol"set-box", arity: 1)
|
|
|
|
|
|
|
|
syndicate "test_dsl":
|
|
|
|
|
|
|
|
spawn "box":
|
|
|
|
field(currentValue, int, 0)
|
2021-06-30 11:30:56 +00:00
|
|
|
assert(BoxState.init currentValue.get)
|
2021-06-29 15:18:09 +00:00
|
|
|
stopIf currentValue.get == 10:
|
|
|
|
echo "box: terminating"
|
2021-06-30 11:30:56 +00:00
|
|
|
onMessage(SetBox % `?*`) do (newValue: int):
|
2021-06-30 10:18:57 +00:00
|
|
|
# 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-06-30 11:30:56 +00:00
|
|
|
onAsserted(BoxState % `?*`) do (v: int):
|
2021-06-29 15:18:09 +00:00
|
|
|
echo "client: learned that box's value is now ", v
|
2021-06-30 11:30:56 +00:00
|
|
|
sendMessage(SetBox % v.succ)
|
|
|
|
onRetracted(BoxState % `?_`) do (_):
|
2021-06-29 15:18:09 +00:00
|
|
|
echo "client: box state disappeared"
|
2021-06-30 10:18:57 +00:00
|
|
|
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.
|