syndicate-rkt/syndicate-examples/box-and-client.rkt

25 lines
957 B
Racket
Raw Normal View History

2021-06-04 13:56:03 +00:00
;;; SPDX-License-Identifier: LGPL-3.0-or-later
;;; SPDX-FileCopyrightText: Copyright © 2010-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
2020-04-27 18:27:48 +00:00
#lang syndicate
2018-04-29 11:22:12 +00:00
;; Simple mutable box and count-to-infinity box client.
(message-struct set-box (new-value))
(assertion-struct box-state (value))
(spawn (field [current-value 0])
(assert (box-state (current-value)))
(stop-when-true (= (current-value) 10)
(log-info "box: terminating"))
(on (message (set-box $new-value))
(log-info "box: taking on new-value ~v" new-value)
(current-value new-value)))
(spawn (stop-when (retracted (observe (set-box _)))
(log-info "client: box has gone"))
(on (asserted (box-state $v))
(log-info "client: learned that box's value is now ~v" v)
(send! (set-box (+ v 1))))
(on (retracted (box-state _))
(log-info "client: box state disappeared")))