Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/chatty-readers-ask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/form-core': patch
---

fixes partial prerendering in NextJS using cache components
1 change: 0 additions & 1 deletion packages/form-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
],
"dependencies": {
"@tanstack/devtools-event-client": "^0.3.5",
"@tanstack/pacer": "^0.15.3",
"@tanstack/store": "^0.7.7"
},
"devDependencies": {
Expand Down
6 changes: 2 additions & 4 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Derived, Store, batch } from '@tanstack/store'
import { throttle } from '@tanstack/pacer'
import {
deleteBy,
determineFormLevelErrorSourceAndValue,
Expand All @@ -12,6 +11,7 @@ import {
isNonEmptyArray,
mergeOpts,
setBy,
throttle,
uuid,
} from './utils'
import { defaultValidationLogic } from './ValidationLogic'
Expand Down Expand Up @@ -1316,9 +1316,7 @@ export class FormApi<
id: this._formId,
state: state,
}),
{
wait: 300,
},
300,
)

// devtool broadcasts
Expand Down
9 changes: 9 additions & 0 deletions packages/form-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,12 @@ export function uuid(): string {
IDX++
return out
}

export function throttle<TFn extends (...args: Array<any>) => any>(fn: TFn, delay: number) {
let timer: ReturnType<typeof setTimeout> | undefined;

return function (...args: Parameters<TFn>) {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
16 changes: 15 additions & 1 deletion packages/form-core/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, expectTypeOf, it } from 'vitest'
import { describe, expect, expectTypeOf, it, vi } from 'vitest'
import {
concatenatePaths,
createFieldMap,
Expand All @@ -10,6 +10,7 @@ import {
makePathArray,
mergeOpts,
setBy,
throttle,
uuid,
} from '../src/index'

Expand Down Expand Up @@ -825,3 +826,16 @@ describe('uuid', () => {
expect(['8', '9', 'a', 'b']).toContain(parts[3]?.[0])
})
})

describe('throttle', () => {
it('should throttle a function', async () => {
const fn = vi.fn()
vi.useFakeTimers()
const throttledFn = throttle(fn, 100)
throttledFn()
throttledFn()
await vi.runAllTimersAsync()
vi.useRealTimers()
expect(fn).toHaveBeenCalledTimes(1)
})
})
12 changes: 0 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.