Skip to content

Commit

Permalink
🐛 fix: emotion instance should use same cache (#80)
Browse files Browse the repository at this point in the history
* feat: createGlobal

* fix: mutiple emotion instance with same cache

* Revert "feat: createGlobal"

This reverts commit da4b2e0.

* chore: code clean
  • Loading branch information
MadCcc committed Jul 20, 2023
1 parent 3870df1 commit 17f804e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
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 17f804e

Please sign in to comment.