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
48 changes: 0 additions & 48 deletions src/hooks/useCompatibleInsertionEffect.tsx

This file was deleted.

22 changes: 4 additions & 18 deletions src/hooks/useEffectCleanupRegister.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { warning } from 'rc-util/lib/warning';
import * as React from 'react';

const fullClone = {
...React,
};
const { useInsertionEffect } = fullClone;
import { useEffect } from 'react';
import type { DependencyList } from 'react';

// DO NOT register functions in useEffect cleanup function, or functions that registered will never be called.
const useCleanupRegister = (deps?: React.DependencyList) => {
const useEffectCleanupRegister = (deps?: DependencyList) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

纯血 css var 的话应该也不需要统计 cleanup 了?

const effectCleanups: (() => void)[] = [];
let cleanupFlag = false;
function register(fn: () => void) {
Expand All @@ -23,7 +19,7 @@ const useCleanupRegister = (deps?: React.DependencyList) => {
effectCleanups.push(fn);
}

React.useEffect(() => {
useEffect(() => {
// Compatible with strict mode
cleanupFlag = false;
return () => {
Expand All @@ -37,14 +33,4 @@ const useCleanupRegister = (deps?: React.DependencyList) => {
return register;
};

const useRun = () => {
return function (fn: () => void) {
fn();
};
};

// Only enable register in React 18
const useEffectCleanupRegister =
typeof useInsertionEffect !== 'undefined' ? useCleanupRegister : useRun;

export default useEffectCleanupRegister;
15 changes: 4 additions & 11 deletions src/hooks/useGlobalCache.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';
import { pathKey, type KeyType } from '../Cache';
import StyleContext from '../StyleContext';
import useCompatibleInsertionEffect from './useCompatibleInsertionEffect';
import useEffectCleanupRegister from './useEffectCleanupRegister';
import useHMR from './useHMR';
import { useInsertionEffect } from 'react';

export type ExtractStyle<CacheValue> = (
cache: CacheValue,
Expand Down Expand Up @@ -74,20 +74,13 @@ export default function useGlobalCache<CacheType>(
const cacheContent = cacheEntity![1];

// Remove if no need anymore
useCompatibleInsertionEffect(
useInsertionEffect(
() => {
onCacheEffect?.(cacheContent);
},
(polyfill) => {
// It's bad to call build again in effect.
// But we have to do this since StrictMode will call effect twice
// which will clear cache on the first time.
buildCache(([times, cache]) => {
if (polyfill && times === 0) {
onCacheEffect?.(cacheContent);
}
return [times + 1, cache];
});
buildCache(([times, cache]) => [times + 1, cache]);

return () => {
globalCache.opUpdate(fullPathStr, (prevCache) => {
Expand All @@ -100,7 +93,7 @@ export default function useGlobalCache<CacheType>(
// With polyfill, registered callback will always be called synchronously
// But without polyfill, it will be called in effect clean up,
// And by that time this cache is cleaned up.
if (polyfill || !globalCache.opGet(fullPathStr)) {
if (!globalCache.opGet(fullPathStr)) {
onCacheRemove?.(cache, false);
}
});
Expand Down
230 changes: 0 additions & 230 deletions tests/legacy.spec.tsx

This file was deleted.