refactor(samples): use produceAsync in tic-tac-toe
Update the tic-tac-toe sample to use `produceAsync` instead of `produce` to ensure state mutations correctly await interruption promises.
This commit is contained in:
parent
fb3c98b1ef
commit
59391c74d9
|
|
@ -77,7 +77,7 @@ export async function start(game: TicTacToeGame) {
|
||||||
const turnNumber = game.value.turn + 1;
|
const turnNumber = game.value.turn + 1;
|
||||||
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
||||||
|
|
||||||
game.produce((state) => {
|
await game.produceAsync((state) => {
|
||||||
state.winner = turnOutput.winner;
|
state.winner = turnOutput.winner;
|
||||||
if (!state.winner) {
|
if (!state.winner) {
|
||||||
state.currentPlayer = state.currentPlayer === "X" ? "O" : "X";
|
state.currentPlayer = state.currentPlayer === "X" ? "O" : "X";
|
||||||
|
|
@ -111,7 +111,7 @@ async function turn(
|
||||||
game.value.currentPlayer,
|
game.value.currentPlayer,
|
||||||
);
|
);
|
||||||
|
|
||||||
placePiece(game, row, col, turnPlayer);
|
await placePiece(game, row, col, turnPlayer);
|
||||||
|
|
||||||
const winner = checkWinner(game);
|
const winner = checkWinner(game);
|
||||||
if (winner) return { winner };
|
if (winner) return { winner };
|
||||||
|
|
@ -163,7 +163,7 @@ export function checkWinner(host: TicTacToeGame): WinnerType {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function placePiece(
|
export async function placePiece(
|
||||||
host: TicTacToeGame,
|
host: TicTacToeGame,
|
||||||
row: number,
|
row: number,
|
||||||
col: number,
|
col: number,
|
||||||
|
|
@ -177,7 +177,7 @@ export function placePiece(
|
||||||
player,
|
player,
|
||||||
id: `piece-${player}-${moveNumber}`,
|
id: `piece-${player}-${moveNumber}`,
|
||||||
};
|
};
|
||||||
host.produce((state) => {
|
await host.produceAsync((state) => {
|
||||||
state.parts[piece.id] = piece;
|
state.parts[piece.id] = piece;
|
||||||
board.childIds.push(piece.id);
|
board.childIds.push(piece.id);
|
||||||
board.partMap[`${row},${col}`] = piece.id;
|
board.partMap[`${row},${col}`] = piece.id;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue