ant-design/components/watermark/useSingletonCache.ts
ug db284a46e2
Some checks failed
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
Upgrade Dependencies / upgrade-deps (push) Has been cancelled
Issue Inactivity Reminder / reminder_job (push) Has been cancelled
Issue Close Require / issue-close-require (push) Has been cancelled
fix Issue52048 Watermark Component Re-render Causes Page Freeze (#52897)
* fix(Watermark):Re-render Causes Page Freeze

* fix(Watermark):Re-render Causes Page Freeze

* fix(Watermark):Re-render Causes Page Freeze

* Update components/watermark/useCache.ts

Co-authored-by: afc163 <afc163@gmail.com>
Signed-off-by: ug <62086147+765477020@users.noreply.github.com>

* refactor: add useSingletonCache

* test: add test case

---------

Signed-off-by: ug <62086147+765477020@users.noreply.github.com>
Co-authored-by: liuqiang <qiang.liu@xinjifamily.com>
Co-authored-by: afc163 <afc163@gmail.com>
Co-authored-by: 二货机器人 <smith3816@gmail.com>
2025-02-24 15:33:37 +08:00

26 lines
775 B
TypeScript

import * as React from 'react';
import isEqual from 'rc-util/lib/isEqual';
export type GetCache<T, R> = (cacheKeys: T, callback: () => R) => R;
/**
* Singleton cache will only take latest `cacheParams` as key
* and return the result for callback matching.
*/
export default function useSingletonCache<T extends any[], R>(): GetCache<T, R> {
const cacheRef = React.useRef<[any[] | null, R | null]>([null, null]);
const getCache: GetCache<T, R> = (cacheKeys, callback) => {
const filteredKeys = cacheKeys.map((item) =>
item instanceof HTMLElement || isNaN(item) ? '' : item,
);
if (!isEqual(cacheRef.current[0], filteredKeys)) {
cacheRef.current = [filteredKeys, callback()];
}
return cacheRef.current[1]!;
};
return getCache;
}