-
Notifications
You must be signed in to change notification settings - Fork 0
fix: overriding parent values breaks reactivity #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "svelte-object": patch | ||
| --- | ||
|
|
||
| fix: overriding parent values breaks reactivity |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,12 @@ | ||||||||||||
| <script lang='ts' module> | ||||||||||||
|
|
||||||||||||
| export interface Props<T extends Record<PropertyKey, unknown> | unknown[]> { | ||||||||||||
| children?: Snippet<[{ | ||||||||||||
| children?: Snippet<[props: { | ||||||||||||
| value: T | ||||||||||||
| attributes: Record<PropertyKey, unknown> | ||||||||||||
| }]> | ||||||||||||
| name?: string | number | ||||||||||||
| default?: T | ||||||||||||
| value?: T | ||||||||||||
|
|
||||||||||||
| /** What the unmodified object is */ | ||||||||||||
|
|
@@ -18,9 +19,15 @@ | |||||||||||
| onSubmit?: (value: T) => void | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| type Prescriptor = { name: PropertyKey, get: () => unknown, set: (value: unknown) => void } | ||||||||||||
|
|
||||||||||||
| type SvelteObjectGeneric = Record<PropertyKey, unknown> | unknown[] | ||||||||||||
| type SvelteObjectContext<T extends SvelteObjectGeneric = SvelteObjectGeneric> = { | ||||||||||||
| setValue(key: string | number, newValue: unknown): void | ||||||||||||
| addPrescriptor: ( | ||||||||||||
| name: Prescriptor['name'], | ||||||||||||
| getter: Prescriptor['get'], | ||||||||||||
| setter: Prescriptor['set'] | ||||||||||||
| ) => void | ||||||||||||
| addValidator(fn: (trigger?: ValidationType) => boolean): void | ||||||||||||
| removeValidator(fn: (trigger?: ValidationType) => boolean): void | ||||||||||||
| submit: () => void | ||||||||||||
|
|
@@ -42,12 +49,11 @@ | |||||||||||
|
|
||||||||||||
| type T = $$Generic<SvelteObjectGeneric> | ||||||||||||
|
|
||||||||||||
| let v = $state({}) as T | undefined | ||||||||||||
|
|
||||||||||||
| let { | ||||||||||||
| children: slot, | ||||||||||||
|
|
||||||||||||
| name = '', | ||||||||||||
| default: defaultvalue, | ||||||||||||
| value = $bindable(), | ||||||||||||
|
|
||||||||||||
| origin, | ||||||||||||
|
|
@@ -58,18 +64,16 @@ | |||||||||||
| onSubmit | ||||||||||||
| }: Props<T> = $props() | ||||||||||||
|
|
||||||||||||
| v = value | ||||||||||||
| v ??= {} as T | ||||||||||||
|
|
||||||||||||
| // svelte-ignore state_referenced_locally | ||||||||||||
| value = v | ||||||||||||
| $effect.pre(() => { | ||||||||||||
| value ??= defaultvalue ?? {} as T | ||||||||||||
| }) | ||||||||||||
|
|
||||||||||||
| let object = getContext('svelte-object') as typeof self | ||||||||||||
| let parent = getContext('svelte-object') as typeof self | ||||||||||||
|
|
||||||||||||
| function createAttributeProxy(): { value: Record<PropertyKey, unknown> } { | ||||||||||||
| /* eslint-disable-next-line svelte/prefer-writable-derived */ | ||||||||||||
| let target = $state({ | ||||||||||||
| ...object?.attributes, | ||||||||||||
| ...parent?.attributes, | ||||||||||||
| ...attributes | ||||||||||||
| }) | ||||||||||||
|
|
||||||||||||
|
|
@@ -83,7 +87,7 @@ | |||||||||||
|
|
||||||||||||
| $effect.pre(() => { | ||||||||||||
| target = { | ||||||||||||
| ...object?.attributes, | ||||||||||||
| ...parent?.attributes, | ||||||||||||
| ...attributes | ||||||||||||
| } | ||||||||||||
| }) | ||||||||||||
|
|
@@ -98,15 +102,41 @@ | |||||||||||
|
|
||||||||||||
| let validators = [] as (typeof validate)[] | ||||||||||||
|
|
||||||||||||
| const prescriptors: Prescriptor[] = $state([]) | ||||||||||||
|
|
||||||||||||
| $effect.pre(() => { | ||||||||||||
| for(const prescriptor of prescriptors) { | ||||||||||||
| $effect.pre(() => { | ||||||||||||
| prescriptor.set(value?.[prescriptor.name]) | ||||||||||||
| }) | ||||||||||||
| $effect.pre(() => { | ||||||||||||
| const itemValue = prescriptor.get() | ||||||||||||
| value ??= {} as T | ||||||||||||
| prescriptor.name | ||||||||||||
| untrack(() => { | ||||||||||||
| value ??= {} as T | ||||||||||||
| value[prescriptor.name] = itemValue | ||||||||||||
| }) | ||||||||||||
| }) | ||||||||||||
| } | ||||||||||||
| }) | ||||||||||||
|
|
||||||||||||
| const self = { | ||||||||||||
| setValue(key: string | number, newValue: unknown) { | ||||||||||||
| if(key === undefined || key === null || key === '') | ||||||||||||
| return | ||||||||||||
| value![key as keyof T] = newValue as T[keyof T] | ||||||||||||
| if(object && name !== '') | ||||||||||||
| object.setValue(name, value) | ||||||||||||
| addPrescriptor( | ||||||||||||
| name: PropertyKey, | ||||||||||||
| getter: () => unknown, | ||||||||||||
| setter: (value: unknown) => void, | ||||||||||||
| ) { | ||||||||||||
| const prescriptor = { name, get: getter, set: setter } | ||||||||||||
|
|
||||||||||||
| value ??= {} as T | ||||||||||||
| value[prescriptor.name] = getter() | ||||||||||||
|
|
||||||||||||
| prescriptors.push(prescriptor) | ||||||||||||
| return () => { | ||||||||||||
| prescriptors.splice(prescriptors.indexOf(prescriptor), 1) | ||||||||||||
| } | ||||||||||||
| }, | ||||||||||||
|
|
||||||||||||
| addValidator(fn: typeof validate) { | ||||||||||||
| validators.push(fn) | ||||||||||||
| }, | ||||||||||||
|
|
@@ -131,16 +161,11 @@ | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
| else | ||||||||||||
| object?.submit() | ||||||||||||
| parent?.submit() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if(object && (name !== undefined && name !== null) && name !== '') { | ||||||||||||
| if(object?.value?.[name]) { | ||||||||||||
| value = object?.value[name] as T | ||||||||||||
| } | ||||||||||||
| else { | ||||||||||||
| object.value![name] = value | ||||||||||||
| } | ||||||||||||
| if(parent && (name !== undefined && name !== null) && name !== '') { | ||||||||||||
| parent.addPrescriptor(name, () => value, v => value = v as T) | ||||||||||||
|
Comment on lines
+167
to
+168
|
||||||||||||
| if(parent && (name !== undefined && name !== null) && name !== '') { | |
| parent.addPrescriptor(name, () => value, v => value = v as T) | |
| let unsubscribePrescriptor: (() => void) | undefined; | |
| if(parent && (name !== undefined && name !== null) && name !== '') { | |
| unsubscribePrescriptor = parent.addPrescriptor(name, () => value, v => value = v as T); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <script module> | ||
| import 'tailwindcss' | ||
| import { defineMeta } from '@storybook/addon-svelte-csf' | ||
|
|
||
| import I from '../../../src/index.svelte' | ||
|
|
||
| const { Story } = defineMeta({}) | ||
|
|
||
| let value = $state({}) | ||
|
|
||
| </script> | ||
|
|
||
| <Story name='Reproduction'> | ||
| {#snippet template()} | ||
| <div class='p-4 border border-gray-300 rounded max-w-72 flex flex-col gap-4'> | ||
| <I.Object bind:value> | ||
| {#snippet children(object)} | ||
| <I.Value name='value'> | ||
| {#snippet children(value)} | ||
| <input class='border rounded px-4 py-2' bind:value={value.value} /> | ||
| {/snippet} | ||
| </I.Value> | ||
| <I.Array name='array' value={[ 'value' ]}> | ||
| {#snippet children(array)} | ||
| {#each array.value as item, i (i)} | ||
| <input class='border rounded px-4 py-2' bind:value={array.value[i]} /> | ||
| {/each} | ||
| <button | ||
| class='border px-2 py-2 bg-sky-500 hover:bg-sky-600 rounded text-white cursor-pointer' | ||
| onclick={() => array.value.push('')} | ||
| > | ||
| Add | ||
| </button> | ||
| {/snippet} | ||
| </I.Array> | ||
| <pre class='whitespace-pre-wrap'><code>{JSON.stringify(object.value, null, '\t')}</code></pre> | ||
| {/snippet} | ||
|
|
||
| </I.Object> | ||
| </div> | ||
| <button | ||
| class='border px-2 py-2 bg-gray-500 hover:bg-gray-600 w-72 mt-4 rounded text-white cursor-pointer' | ||
| onclick={() => value = { }} | ||
| > | ||
| Override value | ||
| </button> | ||
| {/snippet} | ||
| </Story> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1 @@ | ||||||
| When overriding the top-object value reactants such as lower-level arrays and objects stop working. | ||||||
|
||||||
| When overriding the top-object value reactants such as lower-level arrays and objects stop working. | |
| When overriding the top-object value reactivity such as lower-level arrays and objects stop working. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| @import 'tailwindcss'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Registering nested
$effect.precallbacks inside a loop on every state change may degrade performance and lead to redundant effect registrations. Consider moving these registrations outside the loop or consolidating the reactive updates.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would break reactivity and the efficiency of such.