Commit one move with request

Alice chooses red7 and submits landing square 15 with expected revision zero. A request exchange carries this single input to the rules worker. The worker runs the stored checkers/move mutation and returns one result. The event exchange itself does not commit a move.

The mutation checks the whole starting position and changes the piece, turn and revision together. The first attempt returns committed: true at revision one. Running the same parameters again returns committed: false, because that starting position no longer exists. The example checks both results.

Only after a successful commit does the worker publish a turn notification and an animation cue. Both carry revision one so Bob can associate them with the stored turn. The animation path is [11, 15], whose order describes movement. Bob can avoid repeating an animation for the same revision or read the board when a revision is missing.

A database commit and event publication are separate operations. If the process stops between them, the board contains the move without its notification. A missing response also does not prove that the move failed. Read the board before deciding whether to retry.

The stored request/checkers/move definition supplies schemas for request and response messages. Schema validation checks message shape, while the mutation checks whether the turn can commit. The worker unregisters its route when this example exchange ends.

The successful response is:

dust27 B
committed true
revision  1

Define the messages

The game data installs request/checkers/move in the events definition folder:

dust762 B
request  {type                 object
          required             [piece landing expected]
          additionalProperties false
          properties           {piece    {type string}
                                landing  {type    integer
                                          minimum 0
                                          maximum 31}
                                expected {type    integer
                                          minimum 0}}}
response {type                 object
          required             [committed revision]
          additionalProperties false
          properties           {committed {type boolean}
                                revision  {type    integer
                                           minimum 0}}}

The SDK plugin derives the payload types and route helpers from this definition. The Events plugin validates each message against its stored schema. The example below imports the generated SDK.

Code

clojure1.3 kB
(defn move-one-piece [db alice bob rules subscription]
  (let [worker (events/serve-checkers-move db rules)
        turns (events/subscribe-checkers-table-turn db bob)
        animations (events/subscribe-checkers-table-animate db bob)]
    (try
      (let [caller (events/open-checkers-move db alice {:piece "red7" :landing 15 :expected 0})
            exchange (runtime/accept worker wait-time)
            parameters (:request exchange)
            result (commit db "checkers/move" parameters)
            reply {:committed (get result "committed") :revision (get result "revision")}]
        (assert (= {:committed true :revision 1} reply))

        (events/publish-checkers-table-turn db {:revision 1 :turn "black"})
        (events/publish-checkers-table-animate db {:revision 1 :piece "red7" :path [11 15]})
        (runtime/send-response exchange reply)

        (assert (= reply (runtime/read-response caller wait-time)))
        (assert (= 1 (get-in (runtime/receive turns wait-time) [:data :revision])))
        (assert (= [11 15] (get-in (runtime/receive animations wait-time) [:data :path])))
        (assert (false? (get (commit db "checkers/move" parameters) "committed"))))
      (finally
        (runtime/unsubscribe turns)
        (runtime/unsubscribe animations)
        (runtime/close-worker worker)))))