Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

debugReports.ts

75 lines1.7 KB

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 }
41​
42type FieldErrorDebugReportTarget = Omit<
43 FieldErrorDebugReportRequest,
44 'requestId'
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 }