# SPDX-License-Identifier: ISC import asyncdispatch import preserves, preserves/records import syndicate const BoxState = RecordClass(label: symbol"box-state", arity: 1) SetBox = RecordClass(label: symbol"set-box", arity: 1) syndicate testDsl: spawn "box": field(currentValue, int, 0) asserting(BoxState.init currentValue.get) stopIf currentValue.get == 10: echo "box: terminating" onMessage(SetBox % `?*`) do (newValue: int): # The SetBox message is unpacked to `newValue: int` echo "box: taking on new value ", newValue currentValue.set(newValue) spawn "client": #stopIf retracted(observe(SetBox, _)): # echo "client: box has gone" onAsserted(BoxState % `?*`) do (v: int): echo "client: learned that box's value is now ", v send(SetBox % v.succ) onRetracted(BoxState % `?_`) do (_): 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.