Stream legal landings with pull

It is now Black's turn. Bob selects black1, and a pull exchange sends that one selection to the rules worker. The worker runs checkers/legal against the current revision and sends each candidate as a response message. It calls finish after the last candidate.

Bob reads until he receives kind: finish. The candidate for this position lands on 13 and captures red2. Bob collects candidates in a map keyed by landing square. The collection has no order, even though the transport delivers messages in sequence.

This fixture has one candidate for Black's first jump. A larger result can use the same send-and-finish pattern without changing the request. Treat the result as a preview of the queried revision. The commit mutation must still check the board because another turn could change it before Bob acts.

The streamed candidate is:

dust33 B
landing  13
captures {red2 true}

Define the messages

The game data installs pull/checkers/legal in the events definition folder:

dust608 B
request  {type                 object
          required             [piece]
          additionalProperties false
          properties           {piece {type string}}}
response {type                 object
          required             [landing captures]
          additionalProperties false
          properties           {landing  {type    integer
                                          minimum 0
                                          maximum 31}
                                captures {type                 object
                                          additionalProperties {type boolean}}}}

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

clojure890 B
(defn list-legal-moves [db bob rules]
  (let [worker (events/serve-checkers-legal db rules)]
    (try
      (let [caller (events/open-checkers-legal db bob {:piece "black1"})
            exchange (runtime/accept worker wait-time)]

        (doseq [move (rows db "checkers/legal" {"?piece" (get-in exchange [:request :piece])})]
          (runtime/send-response exchange
            {:landing (get move "landing")
             :captures (get move "captures")}))
        (runtime/finish-exchange exchange)

        (let [moves (loop [collected []]
                      (let [move (runtime/read-response caller wait-time)]
                        (if (= :finished move)
                          collected
                          (recur (conj collected move)))))]
          (assert (= [{:landing 13 :captures {:red2 true}}] moves))))
      (finally
        (runtime/close-worker worker)))))