| 1 | import { useEffect, useState } from "react"; |
| 2 | |
| 3 | import { useServiceContainer } from "../services/ServiceContainerContext.js"; |
| 4 | import { ServiceResult } from "../services/types.js"; |
| 5 | |
| 6 | /** |
| 7 | * React hook for consuming services reactively |
| 8 | * Automatically subscribes to service changes and provides loading/error states |
| 9 | */ |
| 10 | export function useService<T>(serviceName: string): ServiceResult<T> & { |
| 11 | reload: () => Promise<void>; |
| 12 | } { |
| 13 | const container = useServiceContainer(); |
| 14 | const [result, setResult] = useState<ServiceResult<T>>(() => |
| 15 | container.getSync<T>(serviceName), |
| 16 | ); |
| 17 | |
| 18 | useEffect(() => { |
| 19 | // Get initial state |
| 20 | const initialResult = container.getSync<T>(serviceName); |
| 21 | setResult(initialResult); |
| 22 | |
| 23 | // Auto-load if idle |
| 24 | if (initialResult.state === "idle") { |
| 25 | container.load<T>(serviceName).catch(() => { |
| 26 | // Error is handled by the service container and will trigger the error event |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | // Set up event listeners |
| 31 | const onLoading = () => { |
| 32 | setResult((prev) => ({ ...prev, state: "loading", error: null })); |
| 33 | }; |
| 34 | |
| 35 | const onReady = (value: T) => { |
| 36 | setResult({ |
| 37 | value, |
| 38 | state: "ready", |
| 39 | error: null, |
| 40 | lastUpdated: new Date(), |
| 41 | }); |
| 42 | }; |
| 43 | |
| 44 | const onError = (error: Error) => { |
| 45 | setResult((prev) => ({ |
| 46 | ...prev, |
| 47 | state: "error", |
| 48 | error, |
| 49 | lastUpdated: new Date(), |
| 50 | })); |
| 51 | }; |
| 52 | |
| 53 | const onChanged = (value: T) => { |
| 54 | setResult((prev) => ({ |
| 55 | ...prev, |
| 56 | value, |
| 57 | error: null, |
| 58 | lastUpdated: new Date(), |
| 59 | })); |
| 60 | }; |
| 61 | |
| 62 | // Subscribe to events |
| 63 | container.on(`${serviceName}:loading`, onLoading); |
| 64 | container.on(`${serviceName}:ready`, onReady); |
| 65 | container.on(`${serviceName}:error`, onError); |
| 66 | container.on(`${serviceName}:changed`, onChanged); |
| 67 | |
| 68 | // Cleanup |
| 69 | return () => { |
| 70 | container.off(`${serviceName}:loading`, onLoading); |
| 71 | container.off(`${serviceName}:ready`, onReady); |
| 72 | container.off(`${serviceName}:error`, onError); |
| 73 | container.off(`${serviceName}:changed`, onChanged); |
| 74 | }; |
| 75 | }, [serviceName, container]); |
| 76 | |
| 77 | const reload = async (): Promise<void> => { |
| 78 | await container.reload(serviceName); |
| 79 | }; |
| 80 | |
| 81 | return { |
| 82 | ...result, |
| 83 | reload, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Hook for consuming multiple services |
| 89 | */ |
| 90 | export function useServices<T extends Record<string, any>>( |
| 91 | serviceNames: (keyof T)[], |
| 92 | ): { |
| 93 | services: Partial<T>; |
| 94 | loading: boolean; |
| 95 | error: Error | null; |
| 96 | allReady: boolean; |
| 97 | } { |
| 98 | const container = useServiceContainer(); |
| 99 | const [services, setServices] = useState<Partial<T>>({}); |
| 100 | const [loading, setLoading] = useState(true); |
| 101 | const [error, setError] = useState<Error | null>(null); |
| 102 | |
| 103 | useEffect(() => { |
| 104 | const updateState = () => { |
| 105 | const newServices: Partial<T> = {}; |
| 106 | let hasLoading = false; |
| 107 | let hasError: Error | null = null; |
| 108 | |
| 109 | for (const serviceName of serviceNames) { |
| 110 | const result = container.getSync(serviceName as string); |
| 111 | |
| 112 | if (result.state === "loading") { |
| 113 | hasLoading = true; |
| 114 | } else if (result.state === "error") { |
| 115 | hasError = result.error; |
| 116 | } else if (result.state === "idle") { |
| 117 | // Auto-load idle services |
| 118 | container.load(serviceName as string).catch(() => {}); |
| 119 | } else if (result.state === "ready" && result.value !== null) { |
| 120 | newServices[serviceName] = result.value as T[keyof T]; |
| 121 | } else { |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | setServices(newServices); |
| 126 | setLoading(hasLoading); |
| 127 | setError(hasError); |
| 128 | }; |
| 129 | |
| 130 | // Initial state |
| 131 | updateState(); |
| 132 | |
| 133 | // Set up listeners for all services |
| 134 | const listeners: Array<() => void> = []; |
| 135 | |
| 136 | for (const serviceName of serviceNames) { |
| 137 | const name = serviceName as string; |
| 138 | |
| 139 | const onAnyChange = () => updateState(); |
| 140 | |
| 141 | container.on(`${name}:loading`, onAnyChange); |
| 142 | container.on(`${name}:ready`, onAnyChange); |
| 143 | container.on(`${name}:error`, onAnyChange); |
| 144 | container.on(`${name}:changed`, onAnyChange); |
| 145 | |
| 146 | listeners.push(() => { |
| 147 | container.off(`${name}:loading`, onAnyChange); |
| 148 | container.off(`${name}:ready`, onAnyChange); |
| 149 | container.off(`${name}:error`, onAnyChange); |
| 150 | container.off(`${name}:changed`, onAnyChange); |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | // Cleanup |
| 155 | return () => { |
| 156 | listeners.forEach((cleanup) => cleanup()); |
| 157 | }; |
| 158 | }, [serviceNames.join(","), container]); |
| 159 | |
| 160 | const allReady = serviceNames.every((name) => |
| 161 | container.isReady(name as string), |
| 162 | ); |
| 163 | |
| 164 | return { services, loading, error, allReady }; |
| 165 | } |