feat: make graph work in div
This commit is contained in:
@ -94,3 +94,14 @@ export const createLogger = (() => {
|
||||
}
|
||||
})();
|
||||
|
||||
|
||||
export function withSubComponents<A, B extends Record<string, any>>(
|
||||
component: A,
|
||||
subcomponents: B
|
||||
): A & B {
|
||||
Object.keys(subcomponents).forEach((key) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(component as any)[key] = (subcomponents as any)[key];
|
||||
});
|
||||
return component as A & B;
|
||||
}
|
||||
|
53
app/src/lib/helpers/localStore.ts
Normal file
53
app/src/lib/helpers/localStore.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
function isStore(v: unknown): v is Writable<unknown> {
|
||||
return v !== null && typeof v === "object" && "subscribe" in v && "set" in v;
|
||||
}
|
||||
|
||||
const storeIds: Map<string, ReturnType<typeof createLocalStore>> = new Map();
|
||||
|
||||
const HAS_LOCALSTORAGE = "localStorage" in globalThis;
|
||||
|
||||
function createLocalStore<T>(key: string, initialValue: T | Writable<T>) {
|
||||
|
||||
let store: Writable<T>;
|
||||
|
||||
if (HAS_LOCALSTORAGE) {
|
||||
const localValue = localStorage.getItem(key);
|
||||
const value = localValue ? JSON.parse(localValue) : null;
|
||||
if (value === null) {
|
||||
if (isStore(initialValue)) {
|
||||
store = initialValue;
|
||||
} else {
|
||||
store = writable(initialValue);
|
||||
}
|
||||
} else {
|
||||
store = writable(value);
|
||||
}
|
||||
} else {
|
||||
return isStore(initialValue) ? initialValue : writable(initialValue);
|
||||
}
|
||||
|
||||
store.subscribe((value) => {
|
||||
localStorage.setItem(key, JSON.stringify(value));
|
||||
});
|
||||
|
||||
return {
|
||||
subscribe: store.subscribe,
|
||||
set: store.set,
|
||||
update: store.update
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default function localStore<T>(key: string, initialValue: T | Writable<T>): Writable<T> {
|
||||
|
||||
if (storeIds.has(key)) return storeIds.get(key) as Writable<T>;
|
||||
|
||||
const store = createLocalStore(key, initialValue)
|
||||
|
||||
storeIds.set(key, store);
|
||||
|
||||
return store
|
||||
|
||||
}
|
Reference in New Issue
Block a user