about summary refs log tree commit diff stats
path: root/src/stores.js
blob: 2dbaca9b228b423e20b02ab8a3e3a28dace06ee8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { writable } from "svelte/store";

const localStorage = window.localStorage;

export const level = localStorageStore(1);
export const aWeather = writable();
export const paWeather = writable();
export const pyWeather = writable();
export const hWeather = writable();

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