Originally this cod was written to add each received prompt, but that
does the wrong thing when the game-state object is trying to set a
subset of the prompts we already have, (for example, to drop the
prompt we just finished playing when starting a new game).
events.addEventListener("game-state", event => {
const state = JSON.parse(event.data);
- for (let prompt of state.prompts) {
- window.game.add_or_update_prompt(prompt);
- }
+ window.game.set_prompts(state.prompts);
window.game.set_active_prompt(state.active_prompt);
});
}
+ set_prompts(prompts) {
+ this.setState({
+ prompts: prompts
+ });
+ }
+
add_or_update_prompt(prompt) {
const prompts_copy = [...this.state.prompts];
const idx = prompts_copy.findIndex(p => p.id === prompt.id);