| 93 | } |
| 94 | |
| 95 | export function useFormKitContextById<T = any>( |
| 96 | id: string, |
| 97 | effect?: (context: FormKitFrameworkContext<T>) => void |
| 98 | ): FormKitFrameworkContext<T> | undefined { |
| 99 | const [effectRef] = useState(() => ({ |
| 100 | current: |
| 101 | (effect as ((context: FormKitFrameworkContext<T>) => void) | undefined) ?? |
| 102 | undefined, |
| 103 | })) |
| 104 | effectRef.current = effect |
| 105 | const [context, setContext] = useState<FormKitFrameworkContext<T> | undefined>( |
| 106 | () => getNode<T>(id)?.context as FormKitFrameworkContext<T> | undefined |
| 107 | ) |
| 108 | |
| 109 | useEffect(() => { |
| 110 | const targetNode = getNode<T>(id) |
| 111 | if (targetNode) { |
| 112 | const next = targetNode.context as FormKitFrameworkContext<T> |
| 113 | setContext(next) |
| 114 | if (effectRef.current) effectRef.current(next) |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | const receipt = watchRegistry(id, ({ payload: node }) => { |
| 119 | if (node) { |
| 120 | const next = node.context as FormKitFrameworkContext<T> |
| 121 | stopWatch(receipt) |
| 122 | queueMicrotask(() => { |
| 123 | setContext(next) |
| 124 | if (effectRef.current) effectRef.current(next) |
| 125 | }) |
| 126 | } |
| 127 | }) |
| 128 | |
| 129 | return () => { |
| 130 | stopWatch(receipt) |
| 131 | } |
| 132 | }, [id]) |
| 133 | |
| 134 | useReactiveStore(context) |
| 135 | |
| 136 | return context |
| 137 | } |
| 138 | |
| 139 | export function useFormKitNodeById<T>( |
| 140 | id: string, |