Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/hooks/useGlobalCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type ExtractStyle<CacheValue> = (
},
) => [order: number, styleId: string, style: string] | null;

const effectMap = new Map<string, boolean>();

export default function useGlobalCache<CacheType>(
prefix: string,
keyPath: KeyType[],
Expand Down Expand Up @@ -73,7 +75,15 @@ export default function useGlobalCache<CacheType>(
// Remove if no need anymore
useInsertionEffect(() => {
buildCache(([times, cache]) => [times + 1, cache]);
onCacheEffect?.(cacheContent);
if (!effectMap.has(fullPathStr)) {
onCacheEffect?.(cacheContent);
effectMap.set(fullPathStr, true);

// 微任务清理混存,可以认为是单次 batch render 中只触发一次 effect
Promise.resolve().then(() => {
effectMap.delete(fullPathStr);
});
}

return () => {
globalCache.opUpdate(fullPathStr, (prevCache) => {
Expand All @@ -82,6 +92,7 @@ export default function useGlobalCache<CacheType>(

if (nextCount === 0) {
onCacheRemove?.(cache, false);
effectMap.delete(fullPathStr);
return null;
}

Expand Down
4 changes: 3 additions & 1 deletion tests/animation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe('animation', () => {
});
});

it('re-mount should not missing animation style', () => {
it('re-mount should not missing animation style', async () => {
function genComp(cls: string) {
return () => {
const [token, hashId] = useCacheToken(theme, [baseToken], {
Expand Down Expand Up @@ -211,6 +211,8 @@ describe('animation', () => {
// Clean up
document.head.innerHTML = '';

await Promise.resolve();

// Render again
render(
<StyleProvider cache={createCache()}>
Expand Down