Skip to content

Commit

Permalink
Merge pull request #81 from ant-design/beta
Browse files Browse the repository at this point in the history
🐛 fix: emotion instance should use same cache (#80)
  • Loading branch information
arvinxx authored Jul 20, 2023
2 parents 3870df1 + a114763 commit 7b1a7bf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [3.4.2-beta.1](https://github.com/ant-design/antd-style/compare/v3.4.1...v3.4.2-beta.1) (2023-07-20)

### 🐛 Bug Fixes

- Emotion instance should use same cache, closes [#80](https://github.com/ant-design/antd-style/issues/80) ([17f804e](https://github.com/ant-design/antd-style/commit/17f804e))

## [3.4.1](https://github.com/ant-design/antd-style/compare/v3.4.0...v3.4.1) (2023-07-02)

### 🐛 Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "antd-style",
"version": "3.4.1",
"version": "3.4.2-beta.1",
"description": "a css-in-js solution for application combine with antd v5 token system and emotion",
"keywords": [
"antd",
Expand Down
16 changes: 12 additions & 4 deletions src/core/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { EmotionCache } from '@emotion/css/create-instance';
export class CacheManager {
private _cacheList: EmotionCache[] = [cache];

add(cache: EmotionCache) {
if (this.hasCache(cache)) return;

this._cacheList.push(cache);
add(cache: EmotionCache): EmotionCache {
const existCache = this.getCache(cache.key);
if (existCache) {
return existCache;
} else {
this._cacheList.push(cache);
return cache;
}
}

delete(cache: EmotionCache) {
Expand All @@ -18,6 +22,10 @@ export class CacheManager {
return this._cacheList.some((c) => c.key === cache.key);
}

getCache(key: string) {
return this._cacheList.find((c) => c.key === key);
}

getCacheList() {
return this._cacheList;
}
Expand Down
2 changes: 1 addition & 1 deletion src/factories/createStyleProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const createStyleProvider = (EmotionContext: Context<Emotion>): FC<StyleP

if (cacheManager) {
// add 方法有幂等
cacheManager.add(instance.cache);
instance.cache = cacheManager.add(instance.cache);
}

return instance;
Expand Down
2 changes: 1 addition & 1 deletion src/functions/createInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const createInstance = <T = any>(options: CreateOptions<T>) => {
const StyleProvider = createStyleProvider(EmotionContext);

// 将 cache 存到全局管理器中
cacheManager.add(emotion.cache);
emotion.cache = cacheManager.add(emotion.cache);

// ******* 下面这些都和主题相关,如果做了任何改动,都需要排查一遍 ************* //

Expand Down

0 comments on commit 7b1a7bf

Please sign in to comment.