Skip to content

Commit

Permalink
fix(patch-updates): replace the entire array instead of mutating it (#1)
Browse files Browse the repository at this point in the history
This should make it easier for things like React to use our data easily.
  • Loading branch information
Alex Van Camp authored Aug 29, 2019
1 parent 8a685ef commit 2f9ffdd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/lib/bingosync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,17 @@ export class Bingosync extends EventEmitter<Events> {

if (json.type === "goal") {
const index = parseInt(json.square.slot.slice(4), 10) - 1;
this.boardState.cells[index] = json.square;

// Update the state in an immutable manner.
// This improves our interop with things like React.
const newBoardState = {
cells: this.boardState.cells.slice(0),
};
newBoardState.cells[index] = {
...newBoardState.cells[index],
...json.square,
};
this.boardState = newBoardState;
this.emit("board-changed", this.boardState);
}
};
Expand Down

0 comments on commit 2f9ffdd

Please # to comment.