From 6561771c435f9d9bed1589b5ed13d17aee0b7873 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sun, 11 Dec 2022 20:46:58 +0100 Subject: feat: Add frontpage --- code/app/src/help/persistent-store.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'code/app/src/help') diff --git a/code/app/src/help/persistent-store.ts b/code/app/src/help/persistent-store.ts index f2c14c9..6a54282 100644 --- a/code/app/src/help/persistent-store.ts +++ b/code/app/src/help/persistent-store.ts @@ -1,3 +1,4 @@ +import {browser} from "$app/environment"; import {writable as _writable, readable as _readable} from "svelte/store"; import type {Writable, Readable, StartStopNotifier} from "svelte/store"; @@ -28,6 +29,7 @@ interface ReadableStore { } function get_store(type: StoreType): Storage { + if (!browser) return undefined; switch (type) { case StoreType.SESSION: return window.sessionStorage; @@ -48,6 +50,7 @@ function prepared_store_value(value: any): string { function get_store_value(options: WritableStore | ReadableStore): any { try { const storage = get_store(options.options.store); + if (!storage) return; const value = storage.getItem(options.name); if (!value) return false; return JSON.parse(value); @@ -64,6 +67,7 @@ function hydrate(store: Writable, options: WritableStore | ReadableStor function subscribe(store: Writable | Readable, options: WritableStore | ReadableStore): void { const storage = get_store(options.options.store); + if (!storage) return; if (!store.subscribe) return; store.subscribe((state: any) => { storage.setItem(options.name, prepared_store_value(state)); @@ -71,6 +75,10 @@ function subscribe(store: Writable | Readable, options: WritableStore(options: WritableStore): Writable { + if (!browser) { + console.warn("Persistent store is only available in the browser"); + return; + } if (options.options === undefined) options.options = default_store_options; console.log("Creating writable store with options: ", options); const store = _writable(options.initialState); @@ -80,6 +88,10 @@ function writable_persistent(options: WritableStore): Writable { } function readable_persistent(options: ReadableStore): Readable { + if (!browser) { + console.warn("Persistent store is only available in the browser"); + return; + } if (options.options === undefined) options.options = default_store_options; console.log("Creating readable store with options: ", options); const store = _readable(options.initialState, options.callback); -- cgit v1.3