Open-Source Wikis

/

Svelte

/

Features

/

Stores

sveltejs/svelte

Stores

Active contributors: Rich Harris, Simon H

Purpose

svelte/store provides the legacy writable, readable, and derived store APIs from Svelte 3/4, plus interop helpers (toStore, fromStore) that bridge stores to Svelte 5's reactivity. New code should generally prefer runes ($state, $derived) and createSubscriber from svelte/reactivity, but stores remain fully supported.

Files

packages/svelte/src/store/
├── index-client.js   # browser entry: store ↔ rune interop
├── index-server.js   # server entry: thin re-export
├── shared/index.js   # writable/readable/derived implementations
├── utils.js
├── public.d.ts
└── private.d.ts

Writable / readable / derived

packages/svelte/src/store/shared/index.js is the canonical implementation. A store is { subscribe(callback): unsubscribe } plus, for writable stores, set(value) and update(fn). The implementation is unchanged from Svelte 3: subscribers are stored in a Set, calls fire synchronously on set (with a small queue to avoid cycles).

This module is both server- and browser-safe — it has no DOM dependencies.

Interop with reactivity

packages/svelte/src/store/index-client.js adds two interop helpers:

  • toStore(get, set?) — turns a getter (and optional setter) into a writable/readable store. Internally creates an effect_root with a render_effect that pushes new values into the store whenever the getter's reactive deps change.
  • fromStore(store) — wraps a store as a { current } object; current reads via createSubscriber (packages/svelte/src/reactivity/create-subscriber.js) so reads inside reactions register a dependency, while reads outside reactions fall back to get(store).

packages/svelte/src/internal/client/reactivity/store.js is the runtime piece that auto-subscribes a store when used with the $store_name shorthand in legacy mode. It owns store_get, store_set, and store_unsub helpers and is the bridge between stores and the reactivity engine.

Server

packages/svelte/src/store/index-server.js is a thin re-export — server-side code can call writable(...) etc., but the auto-subscribe shorthand is rewritten by the server transformer to plain .subscribe calls.

Public types

packages/svelte/src/store/public.d.ts exports Readable<T>, Writable<T>, Subscriber<T>, Unsubscriber, Updater<T>, StartStopNotifier<T>.

Tests

packages/svelte/tests/store/ covers the store APIs directly. Auto-subscribe behaviour is tested under runtime-legacy/.

Key source files

File Purpose
packages/svelte/src/store/shared/index.js writable, readable, derived, get, readonly.
packages/svelte/src/store/index-client.js toStore and fromStore interop.
packages/svelte/src/internal/client/reactivity/store.js store_get / store_set for legacy $ shorthand.
packages/svelte/src/reactivity/create-subscriber.js createSubscriber — the modern subscription primitive.

Entry points for modification

If you need a new auto-subscription primitive, prefer extending createSubscriber (packages/svelte/src/reactivity/create-subscriber.js) over modifying the store interop — it's the path Svelte 5 docs recommend. The legacy store implementations in store/shared/index.js are intentionally frozen.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Stores – Svelte wiki | Factory