Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/svelte-table/src/createTable.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { constructTable } from '@tanstack/table-core'
import { useSelector } from '@tanstack/svelte-store'
import { untrack } from 'svelte'
import { mergeObjects } from './merge-objects'
import { flatMerge, mergeObjects } from './merge-objects'
import { svelteReactivity } from './reactivity.svelte'
import type {
RowData,
Expand Down Expand Up @@ -75,7 +75,7 @@ export function createTable<
defaultOptions: TableOptions<TFeatures, TData>,
newOptions: Partial<TableOptions<TFeatures, TData>>,
) => {
return mergeObjects(defaultOptions, newOptions)
return flatMerge(defaultOptions, newOptions)
},
},
mergedOptions,
Expand Down Expand Up @@ -107,7 +107,7 @@ export function createTable<

untrack(() => {
table.setOptions((prev) => {
return mergeObjects(prev, mergedOptions)
return flatMerge(prev, mergedOptions)
})
})
})
Expand Down
40 changes: 40 additions & 0 deletions packages/svelte-table/src/merge-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,43 @@ export function mergeObjects(...sources: any): any {
}
return target
}

/**
* Merges objects together by eagerly resolving all values into a flat object.
*
* Unlike `mergeObjects`, this does NOT preserve getters — values are read once
* and stored as plain data properties. This prevents the getter-chain
* accumulation that causes O(N) lookups when the result is repeatedly passed
* back as a source in subsequent merges (e.g., inside `$effect.pre` loops).
*
* Later sources take precedence; `undefined` values do not override.
*
* @see https://github.com/TanStack/table/issues/6235
*/
export function flatMerge<T>(source: T): T
export function flatMerge<T, U>(source: T, source1: U): T & U
export function flatMerge<T, U, V>(
source: T,
source1: U,
source2: V,
): T & U & V
export function flatMerge<T, U, V, W>(
source: T,
source1: U,
source2: V,
source3: W,
): T & U & V & W
export function flatMerge(...sources: any): any {
const result: Record<PropertyKey, unknown> = {}
for (let source of sources) {
if (typeof source === 'function') source = source()
if (!source) continue
for (const key of Reflect.ownKeys(source)) {
const value = (source as Record<PropertyKey, unknown>)[key]
if (value !== undefined) {
result[key as string] = value
}
}
}
return result
}