0/24
16. Module-Level State

Lesson 16: Module-Level State

Variables declared at the top of your module persist across RPC calls. They act as shared in-memory state for that module.

let score = 0;

export const addPoint = live((ctx) => {
  score++;  // persists!
  return score;
});

export const getScore = live((ctx) => {
  return score;  // same variable
});

This is how all the previous lessons worked - let count = 0 persisted across increment and decrement calls. Now let's use it intentionally.

Try it

Build a word guessing game using module-level state:

  1. getScore - returns the current score
  2. startGame - picks a random word from the list, resets guessedLetters, returns { wordLength }
  3. guess(letter) - checks if the letter is in the current word. If it hasn't been guessed before, update score (+1 correct, -1 wrong). Return { correct, score, letter }
WebSocket
0
No messages yet
/