diff --git a/site/content/tutorial/07-stores/01-writable-stores/app-a/App.svelte b/site/content/tutorial/07-stores/01-writable-stores/app-a/App.svelte new file mode 100644 index 0000000000..ee542f789d --- /dev/null +++ b/site/content/tutorial/07-stores/01-writable-stores/app-a/App.svelte @@ -0,0 +1,18 @@ + + +
+ This page has been open for + {$elapsed} {$elapsed === 1 ? 'second' : 'seconds'} +
\ No newline at end of file diff --git a/site/content/tutorial/07-stores/04-derived-stores/app-a/stores.js b/site/content/tutorial/07-stores/04-derived-stores/app-a/stores.js new file mode 100644 index 0000000000..483d3094f0 --- /dev/null +++ b/site/content/tutorial/07-stores/04-derived-stores/app-a/stores.js @@ -0,0 +1,18 @@ +import { readable, derive } from 'svelte/store'; + +export const time = readable(function start(set) { + const interval = setInterval(() => { + set(new Date()); + }, 1000); + + return function stop() { + clearInterval(interval); + }; +}, new Date()); + +const start = new Date(); + +export const elapsed = derive( + time, + $time => {} +); \ No newline at end of file diff --git a/site/content/tutorial/07-stores/04-derived-stores/app-b/App.svelte b/site/content/tutorial/07-stores/04-derived-stores/app-b/App.svelte new file mode 100644 index 0000000000..8182ecd671 --- /dev/null +++ b/site/content/tutorial/07-stores/04-derived-stores/app-b/App.svelte @@ -0,0 +1,17 @@ + + ++ This page has been open for + {$elapsed} {$elapsed === 1 ? 'second' : 'seconds'} +
\ No newline at end of file diff --git a/site/content/tutorial/07-stores/04-derived-stores/app-b/stores.js b/site/content/tutorial/07-stores/04-derived-stores/app-b/stores.js new file mode 100644 index 0000000000..2f3af1247a --- /dev/null +++ b/site/content/tutorial/07-stores/04-derived-stores/app-b/stores.js @@ -0,0 +1,18 @@ +import { readable, derive } from 'svelte/store'; + +export const time = readable(function start(set) { + const interval = setInterval(() => { + set(new Date()); + }, 1000); + + return function stop() { + clearInterval(interval); + }; +}, new Date()); + +const start = new Date(); + +export const elapsed = derive( + time, + $time => Math.round(($time - start) / 1000) +); \ No newline at end of file diff --git a/site/content/tutorial/07-stores/04-derived-stores/text.md b/site/content/tutorial/07-stores/04-derived-stores/text.md new file mode 100644 index 0000000000..9ee25b916c --- /dev/null +++ b/site/content/tutorial/07-stores/04-derived-stores/text.md @@ -0,0 +1,14 @@ +--- +title: Derived stores +--- + +You can create a store whose value is based on the value of one or more *other* stores with `derive`. Building on our previous example, we can create a store that derives the time the page has been open: + +```js +export const elapsed = derive( + time, + $time => Math.round(($time - start) / 1000) +); +``` + +> It's possible to derive a store from multiple inputs, and to explicitly `set` a value instead of returning it (which is useful for deriving values asynchronously). Consult the [API reference](docs/TK) for more information. \ No newline at end of file diff --git a/site/content/tutorial/07-stores/05-custom-stores/app-a/App.svelte b/site/content/tutorial/07-stores/05-custom-stores/app-a/App.svelte new file mode 100644 index 0000000000..a320cc052f --- /dev/null +++ b/site/content/tutorial/07-stores/05-custom-stores/app-a/App.svelte @@ -0,0 +1,9 @@ + + +