Showing the changes to packages/react/src (1 of 14 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { Atom, Result } from "@effect-atom/atom"; |
| 2 | −import { RegistryProvider, useAtomValue } from "@effect-atom/atom-react"; | |
| 2 | +import { RegistryContext, RegistryProvider, useAtomValue } from "@effect-atom/atom-react"; | |
| 3 | 3 | import type { |
| 4 | 4 | LocalInstallation, |
| 5 | 5 | Source, |
| @@ -315,6 +315,42 @@ export const useSourceDiscovery = (input: { · 309 unmodified lines | ||
| 315 | 315 | return workspace.enabled ? results : pendingLoadable(workspace.workspace); |
| 316 | 316 | }; |
| 317 | 317 | |
| 318 | +// --------------------------------------------------------------------------- | |
| 319 | +// Prefetching | |
| 320 | +// --------------------------------------------------------------------------- | |
| 321 | + | |
| 322 | +/** | |
| 323 | + * Returns a function that imperatively triggers a fetch for a tool detail. | |
| 324 | + * Because the atom family uses `Atom.keepAlive`, the fetched result persists | |
| 325 | + * in the registry. When the user later selects the tool, `useSourceToolDetail` | |
| 326 | + * gets an instant cache hit. | |
| 327 | + * | |
| 328 | + * Usage: call the returned function on pointer-enter with a short delay. | |
| 329 | + * It returns a cleanup function (unmounts the atom subscription). | |
| 330 | + */ | |
| 331 | +export const usePrefetchToolDetail = () => { | |
| 332 | + const registry = React.useContext(RegistryContext); | |
| 333 | + const workspace = useWorkspaceRequestContext(); | |
| 334 | + | |
| 335 | + return React.useCallback( | |
| 336 | + (sourceId: string, toolPath: string): (() => void) => { | |
| 337 | + if (!workspace.enabled) return () => {}; | |
| 338 | + const requestedSourceId = sourceId as Source["id"]; | |
| 339 | + const atom = sourceInspectionToolAtom( | |
| 340 | + encodeAtomKey([ | |
| 341 | + workspace.enabled, | |
| 342 | + workspace.workspaceId, | |
| 343 | + workspace.accountId, | |
| 344 | + requestedSourceId, | |
| 345 | + toolPath, | |
| 346 | + ]), | |
| 347 | + ); | |
| 348 | + // mount triggers the fetch; returns a dispose function | |
| 349 | + return registry.mount(atom); | |
| 350 | + }, | |
| 351 | + [registry, workspace.enabled, workspace.workspaceId, workspace.accountId], | |
| 352 | + ); | |
| 353 | +}; | |
| 318 | 354 | |
| 319 | 355 | export type { |
| 320 | 356 | LocalInstallation, |