Last active
October 20, 2025 19:07
-
-
Save developit/7227a5b2499aae0ea27c36129e64259c to your computer and use it in GitHub Desktop.
Revisions
-
developit revised this gist
Oct 20, 2025 . 1 changed file with 4 additions and 6 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -3,11 +3,11 @@ import { useState, useEffect } from 'preact/hooks'; const cache = new Map<string, any>(); const inflight = new Map<string, Promise<any>>(); export interface SWRResponse<T> { data: T | undefined; error: Error | null; loading: boolean; } export function useSWR<T>(key: string | null, fetcher: (key: string) => Promise<T>): SWRResponse<T> { const [data, setData] = useState<T | undefined>(cache.get(key ?? '')); @@ -17,7 +17,7 @@ export function useSWR<T>(key: string | null, fetcher: (key: string) => Promise< useEffect(() => { if (!key) return; (async () => { if (inflight.has(key)) { try { const result = await inflight.get(key); @@ -46,9 +46,7 @@ export function useSWR<T>(key: string | null, fetcher: (key: string) => Promise< setLoading(false); inflight.delete(key); } })(); }, [key]); return { data, error, loading }; -
developit revised this gist
Oct 20, 2025 . 1 changed file with 1 addition and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ type SWRResponse<T> = { loading: boolean; }; export function useSWR<T>(key: string | null, fetcher: (key: string) => Promise<T>): SWRResponse<T> { const [data, setData] = useState<T | undefined>(cache.get(key ?? '')); const [error, setError] = useState<Error | null>(null); const [loading, setLoading] = useState(!cache.has(key ?? '')); -
developit created this gist
Oct 20, 2025 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ import { useState, useEffect } from 'preact/hooks'; const cache = new Map<string, any>(); const inflight = new Map<string, Promise<any>>(); type SWRResponse<T> = { data: T | undefined; error: Error | null; loading: boolean; }; function useSWR<T>(key: string | null, fetcher: (key: string) => Promise<T>): SWRResponse<T> { const [data, setData] = useState<T | undefined>(cache.get(key ?? '')); const [error, setError] = useState<Error | null>(null); const [loading, setLoading] = useState(!cache.has(key ?? '')); useEffect(() => { if (!key) return; const load = async () => { if (inflight.has(key)) { try { const result = await inflight.get(key); setData(result); setError(null); } catch (err) { setError(err as Error); } finally { setLoading(false); } return; } setLoading(true); const promise = fetcher(key); inflight.set(key, promise); try { const result = await promise; cache.set(key, result); setData(result); setError(null); } catch (err) { setError(err as Error); } finally { setLoading(false); inflight.delete(key); } }; load(); }, [key]); return { data, error, loading }; }