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
|
2021-07-09 13:26:22 +00:00
|
|
|
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)
|
2021-09-22 12:39:30 +00:00
|
|
|
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):
|
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-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"
|
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.
|