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:
landing 13
captures {red2 true}
Define the messages
The game data installs pull/checkers/legal in the events definition folder:
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
list_legal_moves :: proc(game: ^Game, bob, rules: runtime.Owner) -> sd.Error {
worker := events.serve_checkers_legal(rules) or_return
defer runtime.close_worker(worker)
caller := events.open_checkers_legal(bob, {piece = "black1"}) or_return
exchange := runtime.accept(worker, time.Second) or_return
moves := rows(
game,
"checkers/legal",
{"?piece" = exchange.request.piece},
events.Checkers_Legal_Response,
) or_return
for move in moves {
runtime.send_response(exchange, move) or_return
}
runtime.finish_exchange(exchange) or_return
count := 0
for {
move, notice := runtime.read_response(caller, time.Second) or_return
if notice == .Finished {
break
}
assert(notice == .Message && count == 0 && move.landing == 13)
assert(move.captures["red2"].(bool))
count += 1
}
assert(count == 1)
return nil
}