0
0
Svelteframework~10 mins

Readable stores in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Readable stores
Create readable store with initial value
Subscribe function called
Subscriber receives current value
Store updates internally (optional)
Subscribers notified with new value
Unsubscribe when no longer needed
End
Readable stores provide values that can be subscribed to but not directly changed by subscribers. Updates come from inside the store.
Execution Sample
Svelte
import { readable } from 'svelte/store';

const time = readable(new Date(), set => {
  const interval = setInterval(() => set(new Date()), 1000);
  return () => clearInterval(interval);
});
Creates a readable store that updates the current time every second.
Execution Table
StepActionStore ValueSubscriber CalledNotes
1Create readable store with initial valueCurrent Date at creationNoStore initialized with current time
2Subscribe to storeCurrent Date at creationYesSubscriber receives initial value immediately
31 second passes, interval triggers set()New Date +1sYesSubscriber notified with updated time
4Another second passes, interval triggers set()New Date +2sYesSubscriber notified again
5Unsubscribe calledLast Date valueNoInterval cleared, no more updates
6No more updatesLast Date valueNoStore stops updating after unsubscribe
💡 Unsubscribe clears interval, stopping updates and subscriber calls
Variable Tracker
VariableStartAfter 1After 2After Unsubscribe
store valueDate at creationDate +1sDate +2sDate +2s
subscriber calledNoYesYesNo
interval activeYesYesYesNo
Key Moments - 2 Insights
Why can't subscribers directly change the value of a readable store?
Readable stores only allow internal updates via the set function inside the store. Subscribers get notified but cannot set values themselves, as shown in steps 2 and 3 of the execution_table.
What happens when you unsubscribe from a readable store?
Unsubscribing calls the cleanup function that stops internal updates (like clearing intervals). This is shown in step 5 where the interval is cleared and no further subscriber calls happen.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the store value at step 3?
AInitial Date at creation
BNew Date +1s
CNew Date +2s
DNo value
💡 Hint
Check the 'Store Value' column at step 3 in the execution_table
At which step does the subscriber stop receiving updates?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Look at the 'Subscriber Called' column and the notes about unsubscribe in the execution_table
If the cleanup function did not clear the interval, what would happen?
ASubscriber would never get updates
BStore value would never change
CSubscriber would keep getting updates even after unsubscribe
DStore would throw an error
💡 Hint
Refer to the 'interval active' variable in variable_tracker and step 5 in execution_table
Concept Snapshot
Readable stores in Svelte:
- Created with readable(initialValue, startFunction)
- Subscribers get current value immediately
- Only internal code can update value via set()
- Cleanup function stops updates on unsubscribe
- Subscribers cannot directly change store value
Full Transcript
Readable stores in Svelte provide a way to share data that can be read and subscribed to but not directly changed by subscribers. When you create a readable store, you give it an initial value and a function that can update the value internally, like a timer. Subscribers get the current value right away and get notified whenever the store updates. When they unsubscribe, the store runs a cleanup function to stop updates, such as clearing an interval. This keeps the data flow controlled and predictable.