diff --git a/src/lib/helpers/stores.ts b/src/lib/helpers/stores.ts new file mode 100644 index 000000000..509693a9e --- /dev/null +++ b/src/lib/helpers/stores.ts @@ -0,0 +1,38 @@ +import { writable, type Writable } from 'svelte/store'; +import { deepClone, objectEntries } from './object'; + +/** + * Given an object `obj`, returns an object of writable stores for each key in that object. + * + * Also returns a listen method that accepts an object of the same shape as `obj` and updates + * the stores with the values from that object if they are different. + * + * @export + */ +export function createConservative>(obj: Obj) { + const stores = Object.fromEntries( + objectEntries(obj).map(([key, value]) => [key, writable(value)]) + ) as { + [K in keyof Obj]: Writable; + }; + + const history = deepClone(obj); + + function listen(input: Obj) { + objectEntries(input).forEach(([key, value]) => { + if (!(key in stores) || !(key in history)) { + return; + } + const curr = history[key]; + if (curr !== value) { + stores[key].set(value); + history[key] = value; + } + }); + } + + return { + stores, + listen + }; +} diff --git a/src/lib/helpers/types.ts b/src/lib/helpers/types.ts index 7f054f210..e0e822c27 100644 --- a/src/lib/helpers/types.ts +++ b/src/lib/helpers/types.ts @@ -9,3 +9,6 @@ export function isHTMLElement(el: unknown): el is HTMLElement { export function isHTMLInputElement(el: unknown): el is HTMLInputElement { return el instanceof HTMLInputElement; } + +// eslint-disable-next-line @typescript-eslint/ban-types +export type Prettify = T & {}; diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/string.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/string.svelte index 92c3feaa5..a1bb2f953 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/string.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/string.svelte @@ -35,6 +35,7 @@