-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-js): Fetching custom role in OrganizationSwitcher with cache (
- Loading branch information
1 parent
3ec07c1
commit 741546b
Showing
11 changed files
with
134 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
--- | ||
|
||
Bug fix: fetch custom roles in OrganizationSwitcher |
This file contains 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 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 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 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 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 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 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 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 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,44 +1,119 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { useCallback, useEffect, useRef, useSyncExternalStore } from 'react'; | ||
|
||
import { useLoadingStatus } from './useLoadingStatus'; | ||
import { useSafeState } from './useSafeState'; | ||
export type State<Data = any, Error = any> = { | ||
data: Data | null; | ||
error: Error | null; | ||
/** | ||
* if there's an ongoing request and no "loaded data" | ||
*/ | ||
isLoading: boolean; | ||
/** | ||
* if there's a request or revalidation loading | ||
*/ | ||
isValidating: boolean; | ||
cachedAt?: number; | ||
}; | ||
|
||
/** | ||
* Global cache for storing status of fetched resources | ||
*/ | ||
let requestCache = new Map<string, State>(); | ||
|
||
/** | ||
* A set to store subscribers in order to notify when the value of a key of `requestCache` changes | ||
*/ | ||
const subscribers = new Set<() => void>(); | ||
|
||
/** | ||
* This utility should only be used in tests to clear previously fetched data | ||
*/ | ||
export const clearFetchCache = () => { | ||
requestCache = new Map<string, State>(); | ||
}; | ||
|
||
const serialize = (obj: unknown) => JSON.stringify(obj); | ||
|
||
export const useFetch = <T>( | ||
const useCache = <K = any, V = any>( | ||
key: K, | ||
): { | ||
getCache: () => State<V> | undefined; | ||
setCache: (state: State) => void; | ||
subscribeCache: (callback: () => void) => () => void; | ||
} => { | ||
const serializedKey = serialize(key); | ||
const get = useCallback(() => requestCache.get(serializedKey), [serializedKey]); | ||
const set = useCallback( | ||
(data: State) => { | ||
requestCache.set(serializedKey, data); | ||
subscribers.forEach(callback => callback()); | ||
}, | ||
[serializedKey], | ||
); | ||
const subscribe = useCallback((callback: () => void) => { | ||
subscribers.add(callback); | ||
return () => subscribers.delete(callback); | ||
}, []); | ||
|
||
return { | ||
getCache: get, | ||
setCache: set, | ||
subscribeCache: subscribe, | ||
}; | ||
}; | ||
|
||
export const useFetch = <K, T>( | ||
fetcher: ((...args: any) => Promise<T>) | undefined, | ||
params: any, | ||
params: K, | ||
callbacks?: { | ||
onSuccess?: (data: T) => void; | ||
}, | ||
) => { | ||
const [data, setData] = useSafeState<T | null>(null); | ||
const requestStatus = useLoadingStatus({ | ||
status: 'loading', | ||
}); | ||
|
||
const { subscribeCache, getCache, setCache } = useCache<K, T>(params); | ||
const fetcherRef = useRef(fetcher); | ||
|
||
const cached = useSyncExternalStore(subscribeCache, getCache); | ||
|
||
useEffect(() => { | ||
if (!fetcherRef.current) { | ||
const fetcherMissing = !fetcherRef.current; | ||
const isCacheStale = Date.now() - (getCache()?.cachedAt || 0) < 1000 * 60 * 2; //cache for 2 minutes; | ||
const isRequestOnGoing = getCache()?.isValidating; | ||
|
||
if (fetcherMissing || isCacheStale || isRequestOnGoing) { | ||
return; | ||
} | ||
requestStatus.setLoading(); | ||
fetcherRef | ||
.current(params) | ||
|
||
setCache({ | ||
data: null, | ||
isLoading: !getCache(), | ||
isValidating: true, | ||
error: null, | ||
}); | ||
fetcherRef.current!(params) | ||
.then(result => { | ||
requestStatus.setIdle(); | ||
if (typeof result !== 'undefined') { | ||
setData(typeof result === 'object' ? { ...result } : result); | ||
callbacks?.onSuccess?.(typeof result === 'object' ? { ...result } : result); | ||
const data = typeof result === 'object' ? { ...result } : result; | ||
setCache({ | ||
data, | ||
isLoading: false, | ||
isValidating: false, | ||
error: null, | ||
cachedAt: Date.now(), | ||
}); | ||
callbacks?.onSuccess?.(data); | ||
} | ||
}) | ||
.catch(() => { | ||
requestStatus.setError(); | ||
setData(null); | ||
setCache({ | ||
data: null, | ||
isLoading: false, | ||
isValidating: false, | ||
error: true, | ||
cachedAt: Date.now(), | ||
}); | ||
}); | ||
}, [JSON.stringify(params)]); | ||
}, [serialize(params), setCache, getCache]); | ||
|
||
return { | ||
status: requestStatus, | ||
data, | ||
...cached, | ||
}; | ||
}; |
This file contains 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