Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
fix(frontend): fix duplicate events after re-login
Browse files Browse the repository at this point in the history
  • Loading branch information
mjossdev authored and PascalHonegger committed May 5, 2021
1 parent 9fa688b commit c58702b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 13 additions & 3 deletions frontend/src/components/Game.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@
import type { GameUpdateEvent } from "../gen/game_pb";
import type { ChaosConnectServiceClient } from "../gen/JoestarServiceClientPb";
import { authMetadata } from "../stores/Auth";
import { onMount } from "svelte";
import { onDestroy, onMount } from "svelte";
import Grid from "./Grid.svelte";
import PlayerList from "./PlayerList.svelte";
import FactionSelect from "./FactionSelect.svelte";
export let client: ChaosConnectServiceClient;
function applyGameUpdate(gameUpdateEvent: GameUpdateEvent) {
gameState.apply(gameUpdateEvent);
}
let updateStream: ClientReadableStream<GameUpdateEvent>;
onMount(() => {
const updateStream = client.getGameUpdates(
gameState.reset();
updateStream = client.getGameUpdates(
newEmpty(),
$authMetadata
) as ClientReadableStream<GameUpdateEvent>;
updateStream.on("data", (updateEvent) => gameState.apply(updateEvent));
updateStream.on('data', applyGameUpdate);
});
onDestroy(() => {
updateStream.cancel();
updateStream.removeListener('data', applyGameUpdate);
});
</script>

Expand Down
3 changes: 3 additions & 0 deletions frontend/src/stores/GameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function createGameStateStore() {
subscribe,
apply(updateEvent: GameUpdateEvent) {
update(currentState => applyUpdate(currentState, updateEvent));
},
reset() {
update(initialGameState);
}
}
}
Expand Down

0 comments on commit c58702b

Please sign in to comment.