CSC-105

LibraryData and State

Data and State

Tuesday, November 17

📆 Today

  • On their own is due now
  • Data and state: what your app remembers, and where
  • Work time: Make it real, due next Tuesday at 2:30
  • Thursday: quiz (15 minutes, in the Quiz Space), then work time

Data and state

Two words

WordMeansIn a daily word puzzle
StateWhat your app stores right nowThe letters you've typed, guesses left
DataWhat your app persistsYour streak, yesterday's score

Client and server

  • The client is the browser on your user's phone. It runs your page.
  • A server is a computer that answers requests. It can keep data for everyone.
  • Many good apps only have a client.

Where data lives

WhereWho sees itHow long it lastsFor example
The page (memory)This tabUntil a reloadThe timer on a practice session
This browser (localStorage)This person, hereUntil they clear itA guitar player's practice log
Your server (a database)Whoever you allowUntil you delete itA roommate chore board
Someone else's server (an API, the AI)Whoever they allowTheir choiceTonight's pickup soccer standings

How data moves

  • A request and a response: the page asks (fetch), a server answers. One trip.
  • JSON is the shape the answer takes: names and values, like a filled-in form
  • A cookie rides along with each request, so your server knows who's asking
  • Staying connected (a WebSocket) only when people need to see each other right now

Your nouns are your data

A roommate chore board:

NounWhat it keeps
Householdname, invite code
Personname, which household
Chorewhat, how often
Donewhich chore, who, when

What to store vs compute

StoreCompute
Each day you playedYour streak
Each book you finishedPages read this month
Each chore someone didWhose turn it is next

Handling many users

Each user has their own state and data

  • This browser gives it for free: every phone has its own localStorage
  • On a server, every row says whose it is, or which group it belongs to
  • Shared on purpose is different: the chore board should show everyone's chores

Check it yourself

  1. Open your app on your phone and in a private window on your laptop (e.g. command + shift + N)
  2. Use it as two different people
  3. Reload both. Is everything still there?
  4. Does either one see, or change, the other's work? Should it?

Data storage

AskIf yes
Does someone else need to see it?A server
Does it follow them to another device?Accounts
Would it matter if someone faked it?The server decides, not the page
None of these?localStorage, and you're done

NOT on the client (browser)

  • API keys: the course adds them for you
  • Anything a cheater would change: scores, prices, who won
  • Someone else's private data: the page shows only what this person may see

Your Data section

Add a data section to your project.md

What it remembersWhere it livesWho sees itHow long it lasts
Practice sessionsThis browserThe playerUntil cleared
Tonight's standingsA sports APIEveryoneUpdated each game

Then: where your data comes from (people, an API, the course AI), and why that's enough for your users.

Ask your agent to walk you through it

Read my app's code and docs/project.md. List everything my app remembers, and for each one tell me where it lives (the page, this browser, a server, or an API), who can see it, and how long it lasts. Then ask me whether that's what my users need. Don't write my Data section for me: add the heading and the table's columns, and I'll fill them in.

Work time

"Make it real", due next Tuesday at 2:30

Make it real

  1. Write your Data section in docs/project.md
  2. Decide if anything has to move: to the browser, a server, or accounts
  3. Build the smallest version that works, if it needs one
  4. Check two people and a reload: nothing lost, nothing crossed

Thursday

  • 2:30: Quiz, 15 minutes, in the Quiz Space
  • Then work time on Make it real

A server for your app · Accounts in your app · The Studio