Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: emotion instance should use same cache #80

Merged
merged 4 commits into from
Jul 20, 2023
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
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