diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-10-05 14:45:21 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-10-05 14:45:21 +0200 |
| commit | b7e39b59fd0fc7b5610ebff29035bf622079e0d8 (patch) | |
| tree | 64be84ebbdac9f7ceced983390c53b10d575af5c /old-apps/web-shared/src/lib/persistent-store.ts | |
| parent | 2001c035fbb417ab0a3d42cfb04d17420bde4086 (diff) | |
| download | greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.tar.xz greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.zip | |
refactor: Change file structure
Diffstat (limited to 'old-apps/web-shared/src/lib/persistent-store.ts')
| -rw-r--r-- | old-apps/web-shared/src/lib/persistent-store.ts | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/old-apps/web-shared/src/lib/persistent-store.ts b/old-apps/web-shared/src/lib/persistent-store.ts deleted file mode 100644 index 922f3ab..0000000 --- a/old-apps/web-shared/src/lib/persistent-store.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { writable as _writable, readable as _readable, } from "svelte/store"; -import type { Writable, Readable, StartStopNotifier } from "svelte/store"; - -enum StoreType { - SESSION = 0, - LOCAL = 1 -} - -interface StoreOptions { - store?: StoreType; -} - -const default_store_options = { - store: StoreType.SESSION -} as StoreOptions; - -interface WritableStore<T> { - name: string, - initialState: T, - options?: StoreOptions -} - -interface ReadableStore<T> { - name: string, - initialState: T, - callback: StartStopNotifier<any>, - options?: StoreOptions -} - -function get_store(type: StoreType): Storage { - switch (type) { - case StoreType.SESSION: - return window.sessionStorage; - case StoreType.LOCAL: - return window.localStorage; - } -} - -function prepared_store_value(value: any): string { - try { - return JSON.stringify(value); - } catch (e) { - console.error(e); - return "__INVALID__"; - } -} - -function get_store_value<T>(options: WritableStore<T> | ReadableStore<T>): any { - try { - const storage = get_store(options.options.store); - const value = storage.getItem(options.name); - if (!value) return false; - return JSON.parse(value); - } catch (e) { - console.error(e); - return { __INVALID__: true }; - } -} - -function hydrate<T>(store: Writable<T>, options: WritableStore<T> | ReadableStore<T>): void { - const value = get_store_value<T>(options); - if (value && store.set) store.set(value); -} - -function subscribe<T>(store: Writable<T> | Readable<T>, options: WritableStore<T> | ReadableStore<T>): void { - const storage = get_store(options.options.store); - if (!store.subscribe) return; - store.subscribe((state: any) => { - storage.setItem(options.name, prepared_store_value(state)); - }); -} - -function writable_persistent<T>(options: WritableStore<T>): Writable<T> { - if (options.options === undefined) options.options = default_store_options; - console.log("Creating writable store with options: ", options); - const store = _writable<T>(options.initialState); - hydrate(store, options); - subscribe(store, options); - return store; -} - -function readable_persistent<T>(options: ReadableStore<T>): Readable<T> { - 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); - // hydrate(store, options); - subscribe(store, options); - return store; -} - -export { - writable_persistent, - readable_persistent, - StoreType -}; - -export type { - WritableStore, - ReadableStore, - StoreOptions -}; - |
