diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-12-11 20:46:58 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-12-11 20:47:06 +0100 |
| commit | 6561771c435f9d9bed1589b5ed13d17aee0b7873 (patch) | |
| tree | 2c47e60fa4aaaa5bf1e151838ac197a61f4377cc /code/app/src/help/persistent-store.ts | |
| parent | 8da37c77cae0c7f712a775e3996afd9d84b0f9af (diff) | |
| download | greatoffice-6561771c435f9d9bed1589b5ed13d17aee0b7873.tar.xz greatoffice-6561771c435f9d9bed1589b5ed13d17aee0b7873.zip | |
feat: Add frontpage
Diffstat (limited to 'code/app/src/help/persistent-store.ts')
| -rw-r--r-- | code/app/src/help/persistent-store.ts | 12 |
1 files changed, 12 insertions, 0 deletions
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<T> { } 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<T>(options: WritableStore<T> | ReadableStore<T>): 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<T>(store: Writable<T>, options: WritableStore<T> | ReadableStor function subscribe<T>(store: Writable<T> | Readable<T>, options: WritableStore<T> | ReadableStore<T>): 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<T>(store: Writable<T> | Readable<T>, options: WritableStore<T } function writable_persistent<T>(options: WritableStore<T>): Writable<T> { + 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<T>(options.initialState); @@ -80,6 +88,10 @@ function writable_persistent<T>(options: WritableStore<T>): Writable<T> { } function readable_persistent<T>(options: ReadableStore<T>): Readable<T> { + 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<T>(options.initialState, options.callback); |
