Skip to content

Commit 766f354

Browse files
committed
Web client: explicit initial message value to avoid null checking
1 parent 5250e11 commit 766f354

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

web/src/App.svelte

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
88
onMount(() => sendMessage({ action: "hello", version: "0.0.0" }));
99
10-
const decorate = createMessageReceiver<Decorate>("decorate");
10+
const decorate = createMessageReceiver<Decorate>({
11+
action: "decorate",
12+
decoration: "",
13+
});
1114
12-
const error = createMessageReceiver<Error>("error");
13-
$: errorMessage = $error?.error ?? "";
15+
const error = createMessageReceiver<Error>({ action: "error", error: "" });
1416
</script>
1517

1618
<style>
@@ -19,10 +21,10 @@
1921
}
2022
</style>
2123

22-
{#if errorMessage}
23-
<Alert>Error from server: {errorMessage}</Alert>
24+
{#if $error.error}
25+
<Alert>Error from server: {$error.error}</Alert>
2426
{/if}
2527

26-
<p>{$decorate?.decoration ?? 'Waiting to be decorated...'}</p>
28+
<p>{$decorate.decoration || 'Waiting to be decorated...'}</p>
2729

2830
<Board />

web/src/Board.svelte

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
88
onMount(() => sendMessage({ action: "hostGame", nickname: "adam" }));
99
10-
const boardUpdate = createMessageReceiver<UpdateBoard>("updateBoard");
10+
const boardUpdate = createMessageReceiver<UpdateBoard>({
11+
action: "updateBoard",
12+
board: [],
13+
player: 1,
14+
p1score: 0,
15+
p2score: 0,
16+
x: 0,
17+
y: 0,
18+
});
1119
</script>
1220

1321
<style>
@@ -28,11 +36,11 @@
2836
</style>
2937

3038
<table>
31-
{#each $boardUpdate?.board ?? [] as row}
39+
{#each $boardUpdate.board as row}
3240
<tr>
33-
{#each row as disk}
41+
{#each row as value}
3442
<td>
35-
<Cell {disk} />
43+
<Cell {value} />
3644
</td>
3745
{/each}
3846
</tr>

web/src/Cell.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
2-
import type { Disk } from "./boardTypes";
3-
export let disk: Disk = 0;
2+
import type { Cell } from "./boardTypes";
3+
export let value: Cell = 0;
44
</script>
55

66
<style>
@@ -18,4 +18,4 @@
1818
}
1919
</style>
2020

21-
<div class="player-{disk}">⬤</div>
21+
<div class="player-{value}">⬤</div>

web/src/boardTypes.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
export type Disk = 0 | 1 | 2;
1+
export type Player = 1 | 2;
22

3-
export type Board = Disk[][];
3+
export type Cell = Player | 0;
4+
5+
export type Board = Cell[][];

web/src/messageTypes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Board, Disk } from "./boardTypes";
1+
import type { Board, Player } from "./boardTypes";
22

33
export type OutboundMessage =
44
| Hello
@@ -75,7 +75,7 @@ export interface PlaceDisk {
7575
export interface UpdateBoard {
7676
action: "updateBoard";
7777
board: Board;
78-
player: Disk;
78+
player: Player;
7979
x: number;
8080
y: number;
8181
p1score: number;

web/src/websocket.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ socket.addEventListener("message", ({ data }) => {
2727

2828
// Function for sending messages over the websocket connection.
2929
export const sendMessage = (message: OutboundMessage) => {
30-
if (socket.readyState == 0) {
30+
if (socket.readyState == socket.CONNECTING) {
3131
outboundQueue.push(message);
32-
} else if (socket.readyState == 1) {
32+
} else if (socket.readyState == socket.OPEN) {
3333
socket.send(JSON.stringify(message));
3434
} else {
3535
throw new Error(`Websocket readystate is ${socket.readyState}`);
@@ -39,12 +39,14 @@ export const sendMessage = (message: OutboundMessage) => {
3939
// Create a readable store that receives a specific message type.
4040
// Svelte components can use the $ shorthand to auto-subscribe to the latest value.
4141
export const createMessageReceiver = <T extends InboundMessage>(
42-
action: string
43-
): Readable<T | null> => ({
44-
subscribe: (run, invalidate) =>
45-
messageStore.subscribe((value) => {
46-
if (!value || value.action === action) {
47-
run(value as T | null);
42+
initialValue: T
43+
): Readable<T> => ({
44+
subscribe: (run, invalidate) => {
45+
run(initialValue);
46+
return messageStore.subscribe((value) => {
47+
if (value && value.action === initialValue.action) {
48+
run(value as T);
4849
}
49-
}, invalidate),
50+
}, invalidate);
51+
},
5052
});

0 commit comments

Comments
 (0)