import type { Table_Internal } from './types/Table' import type { NoInfer, RowData, Updater } from './types/type-utils' import type { TableFeatures } from './types/TableFeatures' import type { TableState, TableState_All } from './types/TableState' /** * Applies a TanStack updater to a value. * * If the updater is a function it is called with the previous value; otherwise the updater value is returned directly. */ export function functionalUpdate(updater: Updater, input: T): T { return typeof updater === 'function' ? (updater as (i: T) => T)(input) : updater } /** * Clones table state values while preserving non-plain objects. * * Plain objects and arrays are copied recursively so state updates can avoid mutating existing references. */ export function cloneState(value: T): T { if (Array.isArray(value)) { return value.map(cloneState) as T } if (value && typeof value === 'object') { const proto = Object.getPrototypeOf(value) if (proto !== Object.prototype && proto !== null) { return value } const copy: Record = proto === null ? makeObjectMap() : {} const keys = Object.keys(value) for (let i = 0; i < keys.length; i++) { const key = keys[i]! Object.defineProperty(copy, key, { configurable: true, enumerable: true, value: cloneState((value as Record)[key]), writable: true, }) } return copy as T } return value } /** * Copies prototype-instance own properties without carrying over lazy memo * closures or the per-row cell cache, both of which are bound to the source * instance (cached cells reference the source row). */ export function copyInstancePropertiesWithoutMemos< TTarget extends Record, TSource extends Record, >(target: TTarget, source: TSource): TTarget & TSource { const keys = Object.keys(source) const targetRecord = target as Record for (let i = 0; i < keys.length; i++) { const key = keys[i]! if (!key.startsWith('_memo_') && key !== '_cellsCache') { targetRecord[key] = source[key] } } return target as TTarget & TSource } /** * Creates an object intended only for string-keyed dictionary lookups. * * The null prototype keeps user-controlled ids such as `__proto__` and * `hasOwnProperty` as plain data keys. */ export function makeObjectMap(): Record { return Object.create(null) as Record } /** * Checks whether an object owns a key, including null-prototype dictionaries. */ export function hasOwn(obj: object, key: PropertyKey): boolean { return Object.prototype.hasOwnProperty.call(obj, key) } /** * Creates a table state updater for a single state slice. * * The updater writes through the table base atom for the slice and supports both value and functional updater forms. */ export function makeStateUpdater< TFeatures extends TableFeatures, K extends (string & {}) | keyof TableState_All | keyof TableState, >( key: K, // Minimal structural shape so any table view (public `Table`, // `Table_Internal`, or a custom plugin table) can be passed without forcing // the compiler to relate the full table types. instance: { readonly options: { readonly atoms?: object | undefined } readonly baseAtoms: object }, ) { return (updater: Updater[K & keyof TableState]>) => { const externalAtom = (instance.options as any).atoms?.[key] const targetAtom = externalAtom ?? (instance.baseAtoms as any)[key] targetAtom.set((old: any) => functionalUpdate(updater, old)) } } /** * Checks whether a value is an array or a plain (or null-prototype) object. * Class instances, dates, and other exotic values compare by reference only, * mirroring the `cloneState` plain-object policy. */ function isPlainContainer(value: unknown): value is object { if (typeof value !== 'object' || value === null) { return false } if (Array.isArray(value)) { return true } const proto = Object.getPrototypeOf(value) return proto === Object.prototype || proto === null } /** * Returns every enumerable own key, including symbols and non-index array * properties. Keeping key presence explicit distinguishes sparse array holes * from entries whose value is `undefined`. */ function getEnumerableOwnKeys(value: object): Array { return Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key), ) } const MAX_STATE_COMPARE_DEPTH = 3 /** * Structurally compares two state slice values as deeply as stock feature * state can nest and no deeper. * * Three container levels cover flat maps and arrays, arrays of state objects, * array-valued filter values, and `columnResizing.columnSizingStart` tuples. * Deeper containers and non-plain values compare by reference. A `false` * result is always safe: the state update simply proceeds. */ export function stateSlicesEqual(a: unknown, b: unknown): boolean { return stateSlicesEqualAtDepth(a, b, MAX_STATE_COMPARE_DEPTH) } function stateSlicesEqualAtDepth( a: unknown, b: unknown, depth: number, ): boolean { if (Object.is(a, b)) { return true } if (depth <= 0 || !isPlainContainer(a) || !isPlainContainer(b)) { return false } if (Array.isArray(a) || Array.isArray(b)) { if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) { return false } } const keysA = getEnumerableOwnKeys(a) const keysB = getEnumerableOwnKeys(b) if (keysA.length !== keysB.length) { return false } const recordA = a as Record const recordB = b as Record for (let i = 0; i < keysA.length; i++) { const key = keysA[i]! if (!Object.prototype.propertyIsEnumerable.call(b, key)) { return false } if (!stateSlicesEqualAtDepth(recordA[key], recordB[key], depth - 1)) { return false } } return true } type StateSliceForKey = K extends keyof TableState ? TableState[K] : unknown export type StateSliceEqualityFn = (current: T, next: T) => boolean /** * Routes a state slice update through the slice's `onChange` handler, * preserving the owner's current reference for structural no-ops. * * Equality is evaluated inside the updater received by the state owner, never * against the table's potentially stale controlled snapshot. This keeps * same-tick updates composable in queued host containers such as React state, * evaluates the original updater only when the owner applies it, and lets atom * owners suppress notifications by returning their existing reference. * * A user-provided change handler is still invoked for a no-op because only that * handler's state container can know its latest queued value. The guarded * updater returns that container's previous reference, preventing a state write * or render in state containers with identity bailout semantics. * * Hot-path slices that skip guarding entirely (selection maps that scale with * row count, pointer-frequency resize state) call their change handler * directly instead of routing through this util. Custom feature slices with a * cheaper or semantic-aware comparison can pass `isEqual` to override the * structural default. */ export function setStateSlice( // Minimal structural shape so any table view (public `Table`, // `Table_Internal`, or a custom plugin table) can be passed without forcing // the compiler to relate the full table types. instance: { readonly options: object }, key: K, // Unknown keys (custom feature slices) accept any updater shape instead of // collapsing to `never`. updater: Updater>, isEqual: StateSliceEqualityFn> = stateSlicesEqual, ): void { const onChangeKey = `on${key.charAt(0).toUpperCase()}${key.slice(1)}Change` const onChange = (instance.options as Record)[ onChangeKey ] as ((updater: Updater) => void) | undefined if (!onChange) { return } onChange((current: StateSliceForKey) => { const next = functionalUpdate(updater, current) return isEqual(current, next) ? current : next }) } type AnyFunction = (...args: any) => any /** * Returns whether a value is a function. */ export function isFunction(d: any): d is T { return d instanceof Function } /** * Flattens a tree of nodes by recursively reading child nodes. * * The original nodes are preserved in depth-first order. */ export function flattenBy( arr: Array, getChildren: (item: TNode) => Array, ) { const flat: Array = [] const recurse = (subArr: Array) => { subArr.forEach((item) => { flat.push(item) const children = getChildren(item) if (children.length) { recurse(children) } }) } recurse(arr) return flat } interface MemoOptions, TDepArgs, TResult> { fn: (...args: NoInfer) => TResult memoDeps?: (depArgs?: TDepArgs) => [...TDeps] | undefined onAfterCompare?: (depsChanged: boolean) => void onAfterUpdate?: (result: TResult) => void onBeforeCompare?: () => void onBeforeUpdate?: () => void } /** * Creates a dependency-tracked memoized function for table internals. * * The memo recomputes only when its dependency tuple changes and can emit debug timing information. */ export const memo = , TDepArgs, TResult>({ fn, memoDeps, onAfterCompare, onAfterUpdate, onBeforeCompare, onBeforeUpdate, }: MemoOptions): (( depArgs?: TDepArgs, ) => TResult) => { let deps: Array | undefined = [] let result: TResult | undefined const memoizedFn = (depArgs?: TDepArgs): TResult => { onBeforeCompare?.() const newDeps = memoDeps?.(depArgs) let depsChanged = !newDeps || newDeps.length !== deps?.length if (!depsChanged && newDeps) { for (let i = 0; i < newDeps.length; i++) { if (newDeps[i] !== deps![i]) { depsChanged = true break } } } onAfterCompare?.(depsChanged) if (!depsChanged) { return result! } deps = newDeps onBeforeUpdate?.() result = fn(...(newDeps ?? ([] as any))) onAfterUpdate?.(result) return result } return memoizedFn } /** * Wraps a callback so that its first invocation is skipped. * * Row-model `onAfterUpdate` hooks schedule auto-resets when their inputs * change. The initial computation of a row model is not a change, so state * resets must not fire for it — otherwise merely reading a row model on mount * would wipe initial or controlled state. */ export function skipFirstRun(fn: () => void): () => void { let hasRun = false return () => { if (!hasRun) { hasRun = true return } fn() } } interface TableMemoOptions< TFeatures extends TableFeatures, TDeps extends ReadonlyArray, TDepArgs, TResult, > extends MemoOptions { feature?: keyof TFeatures & string fnName: string objectId?: string onAfterUpdate?: () => void table: Table_Internal } const pad = (str: number | string, num: number) => { str = String(str) while (str.length < num) { str = ' ' + str } return str } /** * Creates a table-aware memoized function. * * This wraps `memo` with table debug options and feature metadata so row models and derived APIs can share consistent diagnostics. */ export function tableMemo< TFeatures extends TableFeatures, TDeps extends ReadonlyArray, TDepArgs, TResult, >({ feature, fnName, objectId, onAfterUpdate, table, ...memoOptions }: TableMemoOptions) { let beforeCompareTime: number let afterCompareTime: number let startCalcTime: number let endCalcTime: number let runCount = 0 let debug: boolean | undefined let debugCache: boolean | undefined if (process.env.NODE_ENV === 'development') { const { debugAll } = table.options const { parentName } = getFunctionNameInfo(fnName, '.') const debugByParent = // @ts-expect-error table.options[ `debug${(parentName != 'table' ? parentName + 's' : parentName).replace( parentName, parentName.charAt(0).toUpperCase() + parentName.slice(1), )}` ] const debugByFeature = feature ? // @ts-expect-error table.options[ `debug${feature.charAt(0).toUpperCase() + feature.slice(1)}` ] : false debug = debugAll || debugByParent || debugByFeature } function logTime(time: number, depsChanged: boolean) { const runType = runCount === 0 ? '(1st run)' : depsChanged ? '(rerun #' + runCount + ')' : '(cache)' runCount++ console.groupCollapsed( `%c⏱ ${pad(`${time.toFixed(1)} ms`, 12)} %c${runType}%c ${fnName}%c ${objectId ? `(${fnName.split('.')[0]}Id: ${objectId})` : ''}`, `font-size: .6rem; font-weight: bold; ${ depsChanged ? `color: hsl( ${Math.max(0, Math.min(120 - Math.log10(time) * 60, 120))}deg 100% 31%);` : '' } `, `color: ${runCount < 2 ? '#FF00FF' : '#FF1493'}`, 'color: #666', 'color: #87CEEB', ) console.info({ feature, state: table.store.state, deps: memoOptions.memoDeps?.toString(), }) console.trace() console.groupEnd() } const onAfterUpdateHandler = onAfterUpdate ? () => { const { schedule, untrack } = table._reactivity schedule(() => untrack(() => onAfterUpdate())) } : undefined const debugOptions = process.env.NODE_ENV === 'development' ? { onBeforeCompare: () => { if (debugCache) { beforeCompareTime = performance.now() } }, onAfterCompare: (depsChanged: boolean) => { if (debugCache) { afterCompareTime = performance.now() const compareTime = Math.round((afterCompareTime - beforeCompareTime) * 100) / 100 if (!depsChanged) { logTime(compareTime, depsChanged) } } }, onBeforeUpdate: () => { if (debug) { startCalcTime = performance.now() } }, onAfterUpdate: () => { if (debug) { endCalcTime = performance.now() const executionTime = Math.round((endCalcTime - startCalcTime) * 100) / 100 logTime(executionTime, true) } onAfterUpdateHandler?.() }, } : { onAfterUpdate: onAfterUpdateHandler } return memo({ ...memoOptions, ...debugOptions, }) } export interface API<_TDeps extends ReadonlyArray, _TDepArgs> { fn: (...args: any) => any memoDeps?: (depArgs?: any) => [...any] | undefined } export type APIObject, TDepArgs> = Record< string, API > /** * Assumes that a function name is in the format of `parentName_fnKey` and returns the `fnKey` and `fnName` in the format of `parentName.fnKey`. * * Splits on the first separator only, so an internal API name such as * `table__expandRows` has the `fnKey` `_expandRows`. */ export function getFunctionNameInfo( staticFnName: string, splitBy: '_' | '.' = '_', ) { // `replace` with a string pattern only replaces the first separator, and // function names never contain spaces const [parentName, fnKey] = staticFnName.replace(splitBy, ' ').split(' ') as [ string, string, ] const fnName = `${parentName}.${fnKey}` return { fnKey, fnName, parentName } } /** * Assigns Table API methods directly to the table instance. * Unlike row/cell/column/header, the table is a singleton so methods are assigned directly. */ export function assignTableAPIs< TFeatures extends TableFeatures, TData extends RowData, TDeps extends ReadonlyArray, TDepArgs, >( feature: keyof TFeatures & string, table: Table_Internal, apis: APIObject>, ): void { for (const [staticFnName, { fn, memoDeps }] of Object.entries(apis)) { const { fnKey, fnName } = getFunctionNameInfo(staticFnName) ;(table as Record)[fnKey] = memoDeps ? tableMemo({ memoDeps, fn, fnName, table, feature, }) : fn } } export interface PrototypeAPI<_TDeps extends ReadonlyArray, _TDepArgs> { fn: (self: any, ...args: any) => any memoDeps?: (self: any, depArgs?: any) => [...any] | undefined } export type PrototypeAPIObject< TDeps extends ReadonlyArray, TDepArgs, > = Record> /** * Assigns API methods to a prototype object for memory-efficient method sharing. * All instances created with this prototype will share the same method references. * * For memoized methods, the memo state is lazily created and stored on each instance. * This provides the best of both worlds: shared method code + per-instance caching. */ export function assignPrototypeAPIs< TFeatures extends TableFeatures, TData extends RowData, TDeps extends ReadonlyArray, TDepArgs, >( feature: keyof TFeatures & string, prototype: Record, table: Table_Internal, apis: PrototypeAPIObject>, ): void { for (const [staticFnName, { fn, memoDeps }] of Object.entries(apis)) { const { fnKey, fnName } = getFunctionNameInfo(staticFnName) if (memoDeps) { // For memoized methods, create a function that lazily initializes // the memo on first access and stores it on the instance const memoKey = `_memo_${fnKey}` prototype[fnKey] = function (this: any, ...args: Array) { // Lazily create memo on first access for this instance if (!this[memoKey]) { const self = this this[memoKey] = tableMemo({ memoDeps: (depArgs) => memoDeps(self, depArgs), fn: (...deps) => fn(self, ...deps), fnName, objectId: self.id, table, feature, }) } return this[memoKey](...args) } } else { // Non-memoized methods just call the static function with `this` prototype[fnKey] = function (this: any, ...args: Array) { return fn(this, ...args) } } } } /** * Looks to run the memoized function with the builder pattern on the object if it exists, otherwise fall back to the static method passed in. */ export function callMemoOrStaticFn< TObject extends Record, TArgs extends Array, TReturn, >( obj: TObject, fnKey: string, staticFn: (obj: TObject, ...args: TArgs) => TReturn, ...args: TArgs ): TReturn { return ( (obj[fnKey] as Function | undefined)?.(...args) ?? staticFn(obj, ...args) ) }