Open History to see the last commit
1import { EventClient } from '@tanstack/devtools-event-client'
2β
3import type { AnyFormOptions, AnyFormState } from './FormApi'
4β
5type ExtractEventNames<T> = T extends `${string}:${infer EventName}`
6 ? EventName
7 : never
8β
9export type BroadcastFormState = {
10 id: string
11 state: AnyFormState
12}
13β
14export type BroadcastFormApi = {
15 id: string
16 state: AnyFormState
17 options: AnyFormOptions
18}
19β
20export type BroadcastFormSubmissionState =
21 | {
22 id: string
23 submissionAttempt: number
24 successful: false
25 stage: 'validateAllFields' | 'validate'
26 errors: any[]
27 }
28 | {
29 id: string
30 submissionAttempt: number
31 successful: false
32 stage: 'inflight'
33 onError: unknown
34 }
35 | {
36 id: string
37 submissionAttempt: number
38 successful: true
39 }
40β
41export type BroadcastFormId = {
42 id: string
43}
44β
45type EventMap = {
46 'form-state': BroadcastFormState
47 'form-api': BroadcastFormApi
48 'form-submission': BroadcastFormSubmissionState
49β
50 'request-form-state': BroadcastFormId
51 'request-form-reset': BroadcastFormId
52 'request-form-force-submit': BroadcastFormId
53β
54 'form-unmounted': BroadcastFormId
55}
56β
57export type EventClientEventMap = keyof EventMap
58β
59export type EventClientEventNames = ExtractEventNames<EventClientEventMap>
60β
61class FormEventClient extends EventClient<EventMap> {
62 constructor() {
63 super({
64 pluginId: 'form-devtools',
65 reconnectEveryMs: 1000,
66 })
67 }
68}
69β
70export const formEventClient = new FormEventClient()