Sveltejs Academy · Lesson

readable Stores and Custom Start Functions

Create read-only stores backed by timers, sensors, or external data sources.

Lesson 4 of 413 steps

readable Stores and Custom Start Functions is a free Sveltejs Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, and your progress syncs across the web and the CoddyKit app. The Sveltejs Academy course includes 4 lessons in total.

Read-Only Stores

A readable store has only subscribe. Consumers cannot mutate it.

Import readable

Comes from svelte/store.

import { readable } from "svelte/store";

Initial Value + Start Function

Pass an initial value and a start function. The start function receives a set and runs once on first subscriber.

export const time = readable(new Date(), (set) => {
  const id = setInterval(() => set(new Date()), 1000);
  return () => clearInterval(id);
});

Stop Function

Return a cleanup function from start. Svelte calls it when the last subscriber unsubscribes.

Lazy Setup

Start function does not run until someone subscribes. Saves resources when not used.

Use Cases

Clocks, sensor readings, web socket feeds, geolocation — anything driven by external timers or events.

Combine with Components

Subscribe with $time in any component. Svelte handles teardown.

<p>The time is {$time.toLocaleTimeString()}</p>

Initial Without Updates

Omit the start function for a constant readable store. Useful for static config.

export const apiBase = readable("https://api.example.com");

Reusing the Pattern

The factory pattern lets you create domain-specific readable stores easily.

Comparison to writable

Readable stores prevent accidental writes from consumers. Use for sources owned by one module.

Convert writable to readable

Expose only the subscribe method of a writable to give consumers a readable view.

export const time = { subscribe: timeStore.subscribe };

Quick Check

When does the start function run?

Recap

readable creates read-only stores with lazy start and cleanup functions, perfect for external sources.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
41
Lessons
163

Frequently Asked Questions

Is the “readable Stores and Custom Start Functions” lesson free?

Yes — the full text of “readable Stores and Custom Start Functions” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO. The Sveltejs Academy course includes 4 lessons in total.

What will I learn in “readable Stores and Custom Start Functions”?

Create read-only stores backed by timers, sensors, or external data sources. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “readable Stores and Custom Start Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating a writable Store
  2. Subscribing with $ Auto-subscription
  3. Updating Stores: set() and update()
  4. readable Stores and Custom Start Functions
← Back to Sveltejs Academy