Open History to see the last commit
1import { uuid } from '@tanstack/form-core/internals'
2import { formDevtoolsEventClient } from './eventClient.lib'
3import type {
4 FieldDebugReport,
5 FieldDebugReportRequest,
6 FieldErrorDebugReport,
7 FieldErrorDebugReportRequest,
8} from './eventClientTypes'
9β
10type FieldDebugReportTarget = Omit<FieldDebugReportRequest, 'requestId'>
11β
12export function requestFieldDebugReport(
13 target: FieldDebugReportTarget,
14 onReport: (report: FieldDebugReport) => void,
15): () => void {
16 const requestId = uuid()
17 let active = true
18β
19 const cleanupReportListener = formDevtoolsEventClient.on(
20 'field-debug-report',
21 (event) => {
22 if (!active || event.payload.requestId !== requestId) return
23β
24 active = false
25 cleanupReportListener()
26 onReport(event.payload)
27 },
28 )
29β
30 formDevtoolsEventClient.emit('field-debug-report-request', {
31 ...target,
32 requestId,
33 })
34β
35 return () => {
36 if (!active) return
37 active = false
38 cleanupReportListener()
39 }
40}
41β
42type FieldErrorDebugReportTarget = Omit<
43 FieldErrorDebugReportRequest,
44 'requestId'
45>
46β
47export function requestFieldErrorDebugReport(
48 target: FieldErrorDebugReportTarget,
49 onReport: (report: FieldErrorDebugReport) => void,
50): () => void {
51 const requestId = uuid()
52 let active = true
53β
54 const cleanupReportListener = formDevtoolsEventClient.on(
55 'field-error-debug-report',
56 (event) => {
57 if (!active || event.payload.requestId !== requestId) return
58β
59 active = false
60 cleanupReportListener()
61 onReport(event.payload)
62 },
63 )
64β
65 formDevtoolsEventClient.emit('field-error-debug-report-request', {
66 ...target,
67 requestId,
68 })
69β
70 return () => {
71 if (!active) return
72 active = false
73 cleanupReportListener()
74 }
75}