MCPcopy Index your code
hub / github.com/continuedev/continue / useService

Function useService

extensions/cli/src/hooks/useService.ts:10–85  ·  view source on GitHub ↗
(serviceName: string)

Source from the content-addressed store, hash-verified

8 * Automatically subscribes to service changes and provides loading/error states
9 */
10export 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

Callers 1

UpdateSelectorFunction · 0.85

Calls 6

useServiceContainerFunction · 0.85
getSyncMethod · 0.80
onMethod · 0.65
offMethod · 0.65
loadMethod · 0.45
reloadMethod · 0.45

Tested by

no test coverage detected