-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix(models-dev): prevent OOM crash on first launch #26781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,7 @@ compiled_raycast_rust | |
|
|
||
| # misc | ||
| .DS_Store | ||
|
|
||
| # Local files | ||
| .claude/ | ||
| GITHUB_ISSUE.md | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,65 @@ | ||
| import { showToast, Toast } from "@raycast/api"; | ||
| import { useCachedPromise } from "@raycast/utils"; | ||
| import { Cache, showToast, Toast } from "@raycast/api"; | ||
| import { useState, useEffect, useRef } from "react"; | ||
| import { fetchModelsData } from "../lib/api"; | ||
| import type { ModelsData } from "../lib/types"; | ||
|
|
||
| const cache = new Cache(); | ||
| const CACHE_KEY = "models-data"; | ||
|
|
||
| /** | ||
| * Hook to fetch models data from models.dev | ||
| * useCachedPromise persists the last successful result to disk automatically, | ||
| * serving stale-while-revalidate on subsequent opens with no extra heap allocation. | ||
| * | ||
| * Uses direct fetch + Cache API instead of useCachedPromise to avoid | ||
| * memory spikes during fresh cache population. See: | ||
| * https://github.com/raycast/utils/issues/XXX | ||
| */ | ||
| export function useModelsData() { | ||
| return useCachedPromise(fetchModelsData, [], { | ||
| keepPreviousData: true, | ||
| onError: (error) => { | ||
| showToast({ | ||
| style: Toast.Style.Failure, | ||
| title: "Failed to load models", | ||
| message: error instanceof Error ? error.message : "Unknown error", | ||
| }); | ||
| }, | ||
| const [data, setData] = useState<ModelsData | null>(() => { | ||
| const cached = cache.get(CACHE_KEY); | ||
| if (cached) { | ||
| try { | ||
| return JSON.parse(cached) as ModelsData; | ||
| } catch { | ||
| cache.remove(CACHE_KEY); | ||
| } | ||
| } | ||
| return null; | ||
| }); | ||
|
|
||
| const [isLoading, setIsLoading] = useState(!data); | ||
| const fetchedRef = useRef(false); | ||
|
|
||
| useEffect(() => { | ||
| // Already have cached data or already fetching | ||
| if (fetchedRef.current) return; | ||
| fetchedRef.current = true; | ||
|
|
||
| // If we have cached data, still revalidate in background | ||
| const shouldRevalidate = !!data; | ||
|
|
||
| if (!shouldRevalidate) { | ||
| setIsLoading(true); | ||
| } | ||
carlesandres marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fetchModelsData() | ||
| .then((result) => { | ||
| setData(result); | ||
| setIsLoading(false); | ||
| // Cache write happens after state update to reduce peak memory | ||
| cache.set(CACHE_KEY, JSON.stringify(result)); | ||
| }) | ||
| .catch((error) => { | ||
| setIsLoading(false); | ||
| // Only show error if we don't have cached data to fall back on | ||
| if (!data) { | ||
| showToast({ | ||
| style: Toast.Style.Failure, | ||
| title: "Failed to load models", | ||
| message: error instanceof Error ? error.message : "Unknown error", | ||
| }); | ||
| } | ||
| }); | ||
| }, []); | ||
|
|
||
| return { data, isLoading }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.