| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {RepoRelativePath} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 11 | import {useMemo} from 'react'; |
| b69ab31 | | | 12 | import {LRU} from 'shared/LRU'; |
| b69ab31 | | | 13 | import serverAPI from './ClientToServerAPI'; |
| b69ab31 | | | 14 | import {t} from './i18n'; |
| b69ab31 | | | 15 | import {writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 16 | import {GeneratedStatus} from './types'; |
| b69ab31 | | | 17 | import {registerDisposable} from './utils'; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | export const generatedFileCache = new LRU<RepoRelativePath, GeneratedStatus>(1500); |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | /** To avoid sending multiple redundant fetch requests, also save which files are being fetched right now */ |
| b69ab31 | | | 22 | const currentlyFetching = new Set<RepoRelativePath>(); |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | /** |
| b69ab31 | | | 25 | * Generated files are cached in `generatedFileCache` LRU. |
| b69ab31 | | | 26 | * For historical reasons, the files are not an atom. |
| b69ab31 | | | 27 | * In order to allow rerender dependencies when we update file statuses, |
| b69ab31 | | | 28 | * store a generation index in recoil. |
| b69ab31 | | | 29 | * This state should generally be used through useGeneratedFileStatus helpers. |
| b69ab31 | | | 30 | */ |
| b69ab31 | | | 31 | const generatedFileGeneration = atom<number>(0); |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | registerDisposable( |
| b69ab31 | | | 34 | currentlyFetching, |
| b69ab31 | | | 35 | serverAPI.onMessageOfType('fetchedGeneratedStatuses', event => { |
| b69ab31 | | | 36 | for (const [path, status] of Object.entries(event.results)) { |
| b69ab31 | | | 37 | generatedFileCache.set(path, status); |
| b69ab31 | | | 38 | currentlyFetching.delete(path); |
| b69ab31 | | | 39 | } |
| b69ab31 | | | 40 | writeAtom(generatedFileGeneration, old => old + 1); |
| b69ab31 | | | 41 | }), |
| b69ab31 | | | 42 | import.meta.hot, |
| b69ab31 | | | 43 | ); |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | export function useGeneratedFileStatus(path: RepoRelativePath): GeneratedStatus { |
| b69ab31 | | | 46 | useAtomValue(generatedFileGeneration); // update if we get new statuses |
| b69ab31 | | | 47 | const found = generatedFileCache.get(path); |
| b69ab31 | | | 48 | if (found == null) { |
| b69ab31 | | | 49 | fetchMissingGeneratedFileStatuses([path]); |
| b69ab31 | | | 50 | return GeneratedStatus.Manual; |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | return found; |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | export function getGeneratedFilesFrom(paths: ReadonlyArray<RepoRelativePath>) { |
| b69ab31 | | | 56 | return Object.fromEntries( |
| b69ab31 | | | 57 | paths.map(path => [path, generatedFileCache.get(path) ?? GeneratedStatus.Manual]), |
| b69ab31 | | | 58 | ); |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | export function useGeneratedFileStatuses( |
| b69ab31 | | | 62 | paths: ReadonlyArray<RepoRelativePath>, |
| b69ab31 | | | 63 | ): Record<RepoRelativePath, GeneratedStatus> { |
| b69ab31 | | | 64 | const generation = useAtomValue(generatedFileGeneration); // update if we get new statuses |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | fetchMissingGeneratedFileStatuses(paths); |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | return useMemo(() => { |
| b69ab31 | | | 69 | return getGeneratedFilesFrom(paths); |
| b69ab31 | | | 70 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| b69ab31 | | | 71 | }, [generation, paths]); |
| b69ab31 | | | 72 | } |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | export function getCachedGeneratedFileStatuses( |
| b69ab31 | | | 75 | paths: ReadonlyArray<RepoRelativePath>, |
| b69ab31 | | | 76 | ): Record<RepoRelativePath, GeneratedStatus | undefined> { |
| b69ab31 | | | 77 | return Object.fromEntries(paths.map(path => [path, generatedFileCache.get(path)])); |
| b69ab31 | | | 78 | } |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | /** |
| b69ab31 | | | 81 | * Hint that this set of files are being used, any files missing from the generated files cache |
| b69ab31 | | | 82 | * should be checked on the server. |
| b69ab31 | | | 83 | * No-op if all files already in the cache. |
| b69ab31 | | | 84 | */ |
| b69ab31 | | | 85 | export function fetchMissingGeneratedFileStatuses(files: ReadonlyArray<RepoRelativePath>) { |
| b69ab31 | | | 86 | let changed = false; |
| b69ab31 | | | 87 | const notCached = files |
| b69ab31 | | | 88 | .filter(file => generatedFileCache.get(file) == null && !currentlyFetching.has(file)) |
| b69ab31 | | | 89 | .filter(path => { |
| b69ab31 | | | 90 | const isGeneratedTestedFromPath = |
| b69ab31 | | | 91 | path.indexOf('__generated__') !== -1 || path.indexOf('/generated/') !== -1; |
| b69ab31 | | | 92 | if (isGeneratedTestedFromPath) { |
| b69ab31 | | | 93 | generatedFileCache.set(path, GeneratedStatus.Generated); |
| b69ab31 | | | 94 | changed = true; |
| b69ab31 | | | 95 | } |
| b69ab31 | | | 96 | return !isGeneratedTestedFromPath; |
| b69ab31 | | | 97 | }); |
| b69ab31 | | | 98 | if (changed) { |
| b69ab31 | | | 99 | writeAtom(generatedFileGeneration, old => old + 1); |
| b69ab31 | | | 100 | } |
| b69ab31 | | | 101 | if (notCached.length > 0) { |
| b69ab31 | | | 102 | for (const file of notCached) { |
| b69ab31 | | | 103 | currentlyFetching.add(file); |
| b69ab31 | | | 104 | } |
| b69ab31 | | | 105 | serverAPI.postMessage({ |
| b69ab31 | | | 106 | type: 'fetchGeneratedStatuses', |
| b69ab31 | | | 107 | paths: notCached, |
| b69ab31 | | | 108 | }); |
| b69ab31 | | | 109 | } |
| b69ab31 | | | 110 | } |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | export function generatedStatusToLabel(status: GeneratedStatus | undefined): string { |
| b69ab31 | | | 113 | if (status === GeneratedStatus.Generated) { |
| b69ab31 | | | 114 | return 'generated'; |
| b69ab31 | | | 115 | } else if (status === GeneratedStatus.PartiallyGenerated) { |
| b69ab31 | | | 116 | return 'partial'; |
| b69ab31 | | | 117 | } else { |
| b69ab31 | | | 118 | return 'manual'; |
| b69ab31 | | | 119 | } |
| b69ab31 | | | 120 | } |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | export function generatedStatusDescription( |
| b69ab31 | | | 123 | status: GeneratedStatus | undefined, |
| b69ab31 | | | 124 | ): string | undefined { |
| b69ab31 | | | 125 | if (status === GeneratedStatus.Generated) { |
| b69ab31 | | | 126 | return t('This file is generated'); |
| b69ab31 | | | 127 | } else if (status === GeneratedStatus.PartiallyGenerated) { |
| b69ab31 | | | 128 | return t('This file is partially generated'); |
| b69ab31 | | | 129 | } else { |
| b69ab31 | | | 130 | return undefined; |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | } |