From b95e138cb416f9bf28a092ba4966b87540a496a4 Mon Sep 17 00:00:00 2001 From: blaumeise20 <62756994+blaumeise20@users.noreply.github.com> Date: Mon, 12 Jul 2021 17:50:58 +0200 Subject: [PATCH] Add `readonly` to stores --- site/content/docs/03-run-time.md | 25 ++++++++++++++++++++++++- src/runtime/store/index.ts | 11 +++++++++++ test/store/index.ts | 19 ++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/site/content/docs/03-run-time.md b/site/content/docs/03-run-time.md index 9a8d6a69b555..19cdcfd25092 100644 --- a/site/content/docs/03-run-time.md +++ b/site/content/docs/03-run-time.md @@ -411,6 +411,29 @@ import { get } from 'svelte/store'; const value = get(store); ``` +#### `readonly` + +```js +readableStore = readonly(writableStore); +``` + +--- + +This simple helper function makes a store readonly. You can still subscribe to the changes from the original one using this new readable store. + + +```js +import { readonly } from 'svelte/store'; + +const writableStore = writable(1); +const readableStore = readonly(writableStore); + +readableStore.subscribe(console.log); + +writableStore.set(2); // console: 2 +readableStore.set(2); // ERROR +``` + ### `svelte/motion` @@ -788,7 +811,7 @@ The `crossfade` function creates a pair of [transitions](docs#transition_fn) cal * `delay` (`number`, default 0) — milliseconds before starting * `duration` (`number` | `function`, default 800) — milliseconds the transition lasts * `easing` (`function`, default `cubicOut`) — an [easing function](docs#svelte_easing) -* `fallback` (`function`) — A fallback [transition](docs#transition_fn) to use for send when there is no matching element being received, and for receive when there is no element being sent. +* `fallback` (`function`) — A fallback [transition](docs#transition_fn) to use for send when there is no matching element being received, and for receive when there is no element being sent. ```sv