From 986068d95eb1c4101e5c56c92baab961455fddc0 Mon Sep 17 00:00:00 2001 From: ana Date: Sun, 8 Aug 2021 13:56:29 +0200 Subject: feat: implement localstorage store --- src/App.svelte | 14 ++++++++++++-- src/stores.js | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/stores.js (limited to 'src') diff --git a/src/App.svelte b/src/App.svelte index a273fcc..27f7f73 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,7 +1,17 @@ - +
-

Level:

+

+ Level: +

+ + Level is {$level}
diff --git a/src/stores.js b/src/stores.js new file mode 100644 index 0000000..f203bfd --- /dev/null +++ b/src/stores.js @@ -0,0 +1,22 @@ +import { writable } from "svelte/store"; + +const localStorage = window.localStorage; + +export const level = localStorageStore(0); + +function localStorageStore(key) { + const item = localStorage.getItem(key); + const { subscribe, set } = writable(JSON.parse(item) || null); + + return { + subscribe, + set: (value) => { + localStorage.setItem(key, JSON.stringify(value)); + set(value); + }, + clear: () => { + localStorage.removeItem(key); + set(null); + }, + }; +} -- cgit 1.4.1-2-gfad0