about summary refs log blame commit diff stats
path: root/src/stores.js
blob: f203bfda529f820697fcb1aa345de9f307988b68 (plain) (tree)





















                                                                
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);
    },
  };
}