Commit a multi-jump turn with push
Bob's turn continues through landing squares 13 and 4. A push exchange lets him send those landings in order and receive one result for the whole turn. Open the exchange without initial data, then send each landing as a message. Call finish when the input stream is complete.
The worker collects landings without changing the database. Every message identifies the same piece and expected revision. The example checks that the board remains at revision one while the worker reads both messages. Its twelve-landing bound limits the draft held by this worker.
After the worker receives finish, it passes the ordered path to checkers/jump. The mutation validates the complete demonstrated path, moves black1 to square 4, removes both captured pieces, changes the turn and advances the revision. Those changes commit together. An incomplete path cannot match that mutation.
The worker then publishes revision-two turn and animation cues and sends its one response. The animation path is [20, 13, 4]. If an exchange is canceled, discard its collected draft and do not run the mutation. A timeout while reading means no message arrived within that wait, so application code must choose whether to wait again or cancel.
The complete turn returns:
committed true
revision 2
Define the messages
The game data installs push/checkers/jump in the events definition folder:
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
make_two_jumps :: proc(game: ^Game, bob, rules: runtime.Owner) -> sd.Error {
incomplete := commit(
game,
"checkers/jump",
{"?piece" = "black1", "?landings" = []i64{13}, "?expected" = 1},
) or_return
assert(!incomplete.committed && incomplete.revision == 1)
worker := events.serve_checkers_jump(rules) or_return
defer runtime.close_worker(worker)
caller := events.open_checkers_jump(bob) or_return
exchange := runtime.accept(worker, time.Second) or_return
for landing in ([]i64{13, 4}) {
runtime.send_request(caller, {piece = "black1", landing = landing, expected = 1}) or_return
}
runtime.finish_call(caller) or_return
landings := make([dynamic]i64)
for {
jump, notice := runtime.read_request(exchange, time.Second) or_return
if notice == .Finished {
break
}
assert(notice == .Message && len(landings) < 12)
assert(jump.piece == "black1" && jump.expected == 1)
append(&landings, jump.landing)
current := board(game) or_return
assert(current.revision == 1)
}
reply := commit(
game,
"checkers/jump",
{"?piece" = "black1", "?landings" = landings[:], "?expected" = 1},
) or_return
assert(reply.committed && reply.revision == 2)
turn_delivery := events.publish_checkers_table_turn(
game.db,
{revision = 2, turn = "red"},
) or_return
animation_delivery := events.publish_checkers_table_animate(
game.db,
{revision = 2, piece = "black1", path = []i64{20, 13, 4}},
) or_return
assert(turn_delivery.dropped == 0 && animation_delivery.dropped == 0)
runtime.send_response(
exchange,
{committed = reply.committed, revision = reply.revision},
) or_return
response, notice := runtime.read_response(caller, time.Second) or_return
assert(notice == .Message && response.committed && response.revision == 2)
return nil
}