|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Real-time Event Subscription Hooks |
| 5 | + * |
| 6 | + * Provides React hooks for subscribing to metadata and data events. |
| 7 | + * Events are automatically cleaned up when components unmount. |
| 8 | + */ |
| 9 | + |
| 10 | +import { useEffect, useState, useCallback } from 'react'; |
| 11 | +import type { MetadataEvent, DataEvent } from '@objectstack/spec/api'; |
| 12 | +import { useClient } from './context'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Hook to subscribe to metadata events |
| 16 | + * |
| 17 | + * @param type - Metadata type to subscribe to (e.g., 'object', 'view', 'agent') |
| 18 | + * @param options - Optional filters (packageId) |
| 19 | + * @returns Latest metadata event or null |
| 20 | + * |
| 21 | + * @example |
| 22 | + * ```tsx |
| 23 | + * function ObjectList() { |
| 24 | + * const event = useMetadataSubscription('object'); |
| 25 | + * |
| 26 | + * useEffect(() => { |
| 27 | + * if (event?.type === 'metadata.object.created') { |
| 28 | + * console.log('New object:', event.name); |
| 29 | + * // Refresh list |
| 30 | + * } |
| 31 | + * }, [event]); |
| 32 | + * |
| 33 | + * return <div>...</div>; |
| 34 | + * } |
| 35 | + * ``` |
| 36 | + */ |
| 37 | +export function useMetadataSubscription( |
| 38 | + type: string, |
| 39 | + options?: { packageId?: string } |
| 40 | +): MetadataEvent | null { |
| 41 | + const client = useClient(); |
| 42 | + const [event, setEvent] = useState<MetadataEvent | null>(null); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (!client) return; |
| 46 | + |
| 47 | + const unsubscribe = client.events.subscribeMetadata( |
| 48 | + type, |
| 49 | + (e) => setEvent(e), |
| 50 | + options |
| 51 | + ); |
| 52 | + |
| 53 | + return () => { |
| 54 | + unsubscribe(); |
| 55 | + }; |
| 56 | + }, [client, type, options?.packageId]); |
| 57 | + |
| 58 | + return event; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Hook to subscribe to data record events |
| 63 | + * |
| 64 | + * @param object - Object name to subscribe to |
| 65 | + * @param options - Optional filters (recordId for specific record) |
| 66 | + * @returns Latest data event or null |
| 67 | + * |
| 68 | + * @example |
| 69 | + * ```tsx |
| 70 | + * function TaskDetail({ taskId }: { taskId: string }) { |
| 71 | + * const event = useDataSubscription('project_task', { recordId: taskId }); |
| 72 | + * |
| 73 | + * useEffect(() => { |
| 74 | + * if (event?.type === 'data.record.updated') { |
| 75 | + * console.log('Task updated:', event.changes); |
| 76 | + * // Refresh task data |
| 77 | + * } |
| 78 | + * }, [event]); |
| 79 | + * |
| 80 | + * return <div>...</div>; |
| 81 | + * } |
| 82 | + * ``` |
| 83 | + */ |
| 84 | +export function useDataSubscription( |
| 85 | + object: string, |
| 86 | + options?: { recordId?: string } |
| 87 | +): DataEvent | null { |
| 88 | + const client = useClient(); |
| 89 | + const [event, setEvent] = useState<DataEvent | null>(null); |
| 90 | + |
| 91 | + useEffect(() => { |
| 92 | + if (!client) return; |
| 93 | + |
| 94 | + const unsubscribe = client.events.subscribeData( |
| 95 | + object, |
| 96 | + (e) => setEvent(e), |
| 97 | + options |
| 98 | + ); |
| 99 | + |
| 100 | + return () => { |
| 101 | + unsubscribe(); |
| 102 | + }; |
| 103 | + }, [client, object, options?.recordId]); |
| 104 | + |
| 105 | + return event; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Hook to subscribe to metadata events with a callback |
| 110 | + * |
| 111 | + * This variant doesn't store events in state, it just triggers a callback. |
| 112 | + * Useful for triggering refetches or side effects without re-renders. |
| 113 | + * |
| 114 | + * @param type - Metadata type to subscribe to |
| 115 | + * @param callback - Callback to invoke on events |
| 116 | + * @param options - Optional filters |
| 117 | + * |
| 118 | + * @example |
| 119 | + * ```tsx |
| 120 | + * function ObjectList() { |
| 121 | + * const { refetch } = useQuery(...); |
| 122 | + * |
| 123 | + * useMetadataSubscriptionCallback('object', () => { |
| 124 | + * refetch(); // Refetch list when objects change |
| 125 | + * }); |
| 126 | + * |
| 127 | + * return <div>...</div>; |
| 128 | + * } |
| 129 | + * ``` |
| 130 | + */ |
| 131 | +export function useMetadataSubscriptionCallback( |
| 132 | + type: string, |
| 133 | + callback: (event: MetadataEvent) => void, |
| 134 | + options?: { packageId?: string } |
| 135 | +): void { |
| 136 | + const client = useClient(); |
| 137 | + |
| 138 | + useEffect(() => { |
| 139 | + if (!client) return; |
| 140 | + |
| 141 | + const unsubscribe = client.events.subscribeMetadata( |
| 142 | + type, |
| 143 | + callback, |
| 144 | + options |
| 145 | + ); |
| 146 | + |
| 147 | + return () => { |
| 148 | + unsubscribe(); |
| 149 | + }; |
| 150 | + }, [client, type, callback, options?.packageId]); |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Hook to subscribe to data events with a callback |
| 155 | + * |
| 156 | + * @param object - Object name to subscribe to |
| 157 | + * @param callback - Callback to invoke on events |
| 158 | + * @param options - Optional filters |
| 159 | + * |
| 160 | + * @example |
| 161 | + * ```tsx |
| 162 | + * function TaskList() { |
| 163 | + * const { refetch } = useQuery(...); |
| 164 | + * |
| 165 | + * useDataSubscriptionCallback('project_task', () => { |
| 166 | + * refetch(); // Refetch list when tasks change |
| 167 | + * }); |
| 168 | + * |
| 169 | + * return <div>...</div>; |
| 170 | + * } |
| 171 | + * ``` |
| 172 | + */ |
| 173 | +export function useDataSubscriptionCallback( |
| 174 | + object: string, |
| 175 | + callback: (event: DataEvent) => void, |
| 176 | + options?: { recordId?: string } |
| 177 | +): void { |
| 178 | + const client = useClient(); |
| 179 | + |
| 180 | + useEffect(() => { |
| 181 | + if (!client) return; |
| 182 | + |
| 183 | + const unsubscribe = client.events.subscribeData( |
| 184 | + object, |
| 185 | + callback, |
| 186 | + options |
| 187 | + ); |
| 188 | + |
| 189 | + return () => { |
| 190 | + unsubscribe(); |
| 191 | + }; |
| 192 | + }, [client, object, callback, options?.recordId]); |
| 193 | +} |
| 194 | + |
| 195 | +/** |
| 196 | + * Hook to get connection status of realtime events |
| 197 | + * |
| 198 | + * @returns Whether realtime is connected |
| 199 | + * |
| 200 | + * @example |
| 201 | + * ```tsx |
| 202 | + * function ConnectionIndicator() { |
| 203 | + * const connected = useRealtimeConnection(); |
| 204 | + * |
| 205 | + * return ( |
| 206 | + * <div> |
| 207 | + * {connected ? '🟢 Connected' : '🔴 Disconnected'} |
| 208 | + * </div> |
| 209 | + * ); |
| 210 | + * } |
| 211 | + * ``` |
| 212 | + */ |
| 213 | +export function useRealtimeConnection(): boolean { |
| 214 | + const client = useClient(); |
| 215 | + const [connected, setConnected] = useState(true); |
| 216 | + |
| 217 | + useEffect(() => { |
| 218 | + if (!client) { |
| 219 | + setConnected(false); |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + // For now, assume always connected with in-memory adapter |
| 224 | + // In production, this would listen to WebSocket connection events |
| 225 | + setConnected(true); |
| 226 | + }, [client]); |
| 227 | + |
| 228 | + return connected; |
| 229 | +} |
| 230 | + |
| 231 | +/** |
| 232 | + * Hook for auto-refreshing queries when data changes |
| 233 | + * |
| 234 | + * Combines data subscription with query refetch. |
| 235 | + * |
| 236 | + * @param object - Object name to watch |
| 237 | + * @param refetch - Refetch function from useQuery |
| 238 | + * @param options - Optional filters |
| 239 | + * |
| 240 | + * @example |
| 241 | + * ```tsx |
| 242 | + * function TaskList() { |
| 243 | + * const { data, refetch } = useQuery('project_task', {}); |
| 244 | + * |
| 245 | + * useAutoRefresh('project_task', refetch); |
| 246 | + * |
| 247 | + * return <div>{data.map(...)}</div>; |
| 248 | + * } |
| 249 | + * ``` |
| 250 | + */ |
| 251 | +export function useAutoRefresh( |
| 252 | + object: string, |
| 253 | + refetch: () => void, |
| 254 | + options?: { recordId?: string } |
| 255 | +): void { |
| 256 | + const handleEvent = useCallback((_event: DataEvent) => { |
| 257 | + // Refetch on any data change |
| 258 | + refetch(); |
| 259 | + }, [refetch]); |
| 260 | + |
| 261 | + useDataSubscriptionCallback(object, handleEvent, options); |
| 262 | +} |
0 commit comments