Skip to content

Commit

Permalink
perf: 语言包缓存判断优化
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Sep 16, 2022
1 parent b522084 commit 4dda883
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/components/meComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineComponent({
transition: Object as PropType<TransitionProps>,
},
setup(props, { attrs }) {
const { loadMessages, clearCache } = useLoadMessages();
const loadMessages = useLoadMessages();
const componentIs: Ref<any> = ref(undefined);
const key = ref(props.componentKey);
const _attrs = ref(attrs);
Expand All @@ -24,7 +24,6 @@ export default defineComponent({
async (is) => {
if (is) {
localeConfig.loadMessageConfig.componentLoad && (await Promise.allSettled(loadMessages(is as any, false))); // 自动加载语言包
clearCache();
componentIs.value = is;
key.value = props.componentKey;
_attrs.value = attrs;
Expand Down
10 changes: 4 additions & 6 deletions src/locales/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const asyncUseLocalesI18n = async <Options extends UseI18nOptions = UseI1
return res;
};

const loadComponentCache = new WeakSet<ComponentOptions>();
/**
* 获取异步导入组件及其子孙的语言包函数
* @returns
Expand All @@ -72,10 +73,6 @@ export const useLoadMessages = () => {
locale: string | undefined = undefined,
importArr: Array<Promise<any>> = [],
) => {
if (cache.has(options)) {
return importArr;
}
cache.add(options);
if (typeof options === 'string') {
const component = app.component(capitalize(camelize(options)));
loadMessages(component as ComponentOptions, isLoading, locale, importArr);
Expand All @@ -85,7 +82,8 @@ export const useLoadMessages = () => {
loadMessages(options.type, isLoading, locale, importArr);
return importArr;
}
if (typeof options === 'object') {
if (typeof options === 'object' && !loadComponentCache.has(options)) {
loadComponentCache.add(options);
if ((<ComponentOptions>options).components) {
Object.values((<ComponentOptions>options).components!).forEach((component) => {
loadMessages(component as ComponentOptions, isLoading, locale, importArr);
Expand All @@ -100,5 +98,5 @@ export const useLoadMessages = () => {
}
return importArr;
};
return { loadMessages, clearCache: () => cache.clear() };
return loadMessages;
};

0 comments on commit 4dda883

Please sign in to comment.