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(ConfigProvider): 修复 config-provider 同时存在 provide 和 setup#provide 导致卡顿的性能问题 #1113

Merged
merged 3 commits into from
Jul 2, 2022
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
11 changes: 1 addition & 10 deletions src/config-provider/config-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Vue, { PropType, VNode } from 'vue';
import cloneDeep from 'lodash/cloneDeep';
import { provide, computed } from '@vue/composition-api';
import { GlobalConfigProvider } from './type';
import { configProviderInjectKey, defaultGlobalConfig, mergeWith } from './context';
import { defaultGlobalConfig, mergeWith } from './context';

const ConfigProvider = Vue.extend({
name: 'TConfigProvider',
Expand All @@ -29,14 +28,6 @@ const ConfigProvider = Vue.extend({
},
},

setup(props) {
const defaultData = cloneDeep(defaultGlobalConfig);
provide(
configProviderInjectKey,
computed(() => mergeWith(defaultData, props.globalConfig)),
);
},

render(): VNode {
if (this.$slots.default.length === 1) {
return this.$slots.default[0];
Expand Down
3 changes: 0 additions & 3 deletions src/config-provider/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { InjectionKey, ComputedRef } from '@vue/composition-api';
import merge from 'lodash/merge';
import _mergeWith from 'lodash/mergeWith';
import defaultConfig from '../_common/js/global-config/default-config';
Expand All @@ -18,8 +17,6 @@ export type Locale = typeof defaultZhCN;
// 导出全局配置(包括语言配置)全部类型
export * from './type';

export const configProviderInjectKey: InjectionKey<ComputedRef<GlobalConfigProvider>> = Symbol('configProvide');

// deal with https://github.com/lodash/lodash/issues/1313
export const mergeWith = (defaultGlobalConfig: GlobalConfigProvider, injectConfig: GlobalConfigProvider) => _mergeWith(defaultGlobalConfig, injectConfig, (objValue, srcValue) => {
if (Array.isArray(objValue)) {
Expand Down
6 changes: 3 additions & 3 deletions src/config-provider/useConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed, inject, h } from '@vue/composition-api';
import { GlobalConfigProvider, defaultGlobalConfig, configProviderInjectKey } from './context';
import { GlobalConfigProvider, defaultGlobalConfig } from './context';

/**
* component global config
Expand All @@ -8,8 +8,8 @@ import { GlobalConfigProvider, defaultGlobalConfig, configProviderInjectKey } fr
* useConfig('pagination')
*/
export function useConfig<T extends keyof GlobalConfigProvider>(componentName?: T) {
const injectGlobalConfig = inject(configProviderInjectKey, null);
const mergedGlobalConfig = computed(() => injectGlobalConfig?.value || defaultGlobalConfig);
const injectGlobalConfig = inject<GlobalConfigProvider>('globalConfig', null);
const mergedGlobalConfig = computed(() => injectGlobalConfig || defaultGlobalConfig);
const global = computed(() => mergedGlobalConfig.value[componentName]);

const classPrefix = computed(() => mergedGlobalConfig.value.classPrefix);
Expand Down