Skip to content

Commit

Permalink
perf: tsx use useExpose
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Dec 8, 2020
1 parent 29461a8 commit 9bb7514
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
- 打包代码拆分(试验)
- 提取上传地址到全局变量,打包可以动态配置

### ✨ Refactor

- tree 组件 ref 函数调用删除 `$`

### ⚡ Performance Improvements

- 页面切换 loading 逻辑修改。对于已经加载过的页面不管有没有关闭,再次打开不会在显示 loading(已经打开过的页面再次打开速度比较快,可以不需要 loading,同理顶部进度条逻辑也一样),刷新后恢复。
Expand Down
16 changes: 10 additions & 6 deletions src/components/Container/src/ScrollContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@

<script lang="ts">
import { defineComponent, ref, unref, nextTick } from 'vue';
import { Scrollbar } from '/@/components/Scrollbar';
import { Scrollbar, ScrollbarType } from '/@/components/Scrollbar';
import { useScrollTo } from '/@/hooks/event/useScrollTo';
export default defineComponent({
name: 'ScrollContainer',
components: { Scrollbar },
setup() {
const scrollbarRef = ref<RefInstanceType<any>>(null);
const scrollbarRef = ref<Nullable<ScrollbarType>>(null);
function scrollTo(to: number, duration = 500) {
const scrollbar = unref(scrollbarRef);
if (!scrollbar) return;
nextTick(() => {
const wrap = unref(scrollbar.wrap);
if (!wrap) return;
const { start } = useScrollTo({
el: unref(scrollbar.$.wrap),
el: wrap,
to,
duration,
});
Expand All @@ -38,17 +40,19 @@
function getScrollWrap() {
const scrollbar = unref(scrollbarRef);
if (!scrollbar) return null;
return scrollbar.$.wrap;
return scrollbar.wrap;
}
function scrollBottom() {
const scrollbar = unref(scrollbarRef);
if (!scrollbar) return;
nextTick(() => {
const scrollHeight = scrollbar.$.wrap.scrollHeight as number;
const wrap = unref(scrollbar.wrap);
if (!wrap) return;
const scrollHeight = wrap.scrollHeight as number;
const { start } = useScrollTo({
el: unref(scrollbar.$.wrap),
el: wrap,
to: scrollHeight,
});
start();
Expand Down
12 changes: 1 addition & 11 deletions src/components/Form/src/BasicForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,7 @@
import type { Ref, WatchStopHandle } from 'vue';
import type { ValidateFields } from 'ant-design-vue/lib/form/interface';
import {
defineComponent,
reactive,
ref,
computed,
unref,
toRef,
onMounted,
watch,
toRefs,
} from 'vue';
import { defineComponent, reactive, ref, computed, unref, onMounted, watch, toRefs } from 'vue';
import { Form, Row } from 'ant-design-vue';
import FormItem from './FormItem';
import { basicProps } from './props';
Expand Down
8 changes: 7 additions & 1 deletion src/components/Scrollbar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
* copy from element-ui
*/

export { default as Scrollbar } from './src/Scrollbar';
import Scrollbar from './src/Scrollbar';
import { withInstall } from '../util';

withInstall(Scrollbar);

export { Scrollbar };
export type { ScrollbarType } from './src/types';
8 changes: 4 additions & 4 deletions src/components/Scrollbar/src/Scrollbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import {
onBeforeUnmount,
} from 'vue';
import { getSlot } from '/@/utils/helper/tsxHelper';
import { tryTsxEmit } from '/@/utils/helper/vueHelper';
import './index.less';
import { useExpose } from '/@/hooks/core/useExpose';
import { ScrollbarType } from './types';

export default defineComponent({
name: 'Scrollbar',
Expand Down Expand Up @@ -65,10 +66,9 @@ export default defineComponent({
}

onMounted(() => {
tryTsxEmit<any>((instance) => {
instance.wrap = unref(wrapElRef);
useExpose<ScrollbarType>({
wrap: unref(wrapElRef),
});

const { native, noresize } = props;
const resizeEl = unref(resizeRef);
const warpEl = unref(wrapElRef);
Expand Down
4 changes: 4 additions & 0 deletions src/components/Scrollbar/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ export interface BarMap {
vertical: BarMapItem;
horizontal: BarMapItem;
}

export interface ScrollbarType {
wrap: ElRef;
}
5 changes: 4 additions & 1 deletion src/components/Table/src/BasicTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import { basicProps } from './props';
import { ROW_KEY } from './const';
import './style/index.less';
import { useExpose } from '/@/hooks/core/useExpose';
export default defineComponent({
props: basicProps,
components: { Table, BasicForm },
Expand Down Expand Up @@ -309,6 +310,8 @@
wrapRef,
});
useExpose<TableActionType>(tableAction);
emit('register', tableAction);
return {
tableElRef,
Expand All @@ -322,7 +325,7 @@
getRowClassName,
wrapRef,
tableAction,
...tableAction,
redoHeight,
};
},
});
Expand Down
27 changes: 14 additions & 13 deletions src/components/Tree/src/BasicTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { useContextMenu, ContextMenuItem } from '/@/hooks/web/useContextMenu';
import { isFunction } from '/@/utils/is';
import { omit } from 'lodash-es';
import { extendSlots } from '/@/utils/helper/tsxHelper';
import { tryTsxEmit } from '/@/utils/helper/vueHelper';

import { basicProps } from './props';
import { useTree } from './useTree';
import { useExpose } from '/@/hooks/core/useExpose';

interface State {
expandedKeys: Keys;
Expand Down Expand Up @@ -182,20 +182,21 @@ export default defineComponent({
state.checkedKeys = props.checkedKeys;
});

tryTsxEmit<TreeActionType>((currentInstance) => {
currentInstance.setExpandedKeys = setExpandedKeys;
currentInstance.getExpandedKeys = getExpandedKeys;
currentInstance.setSelectedKeys = setSelectedKeys;
currentInstance.getSelectedKeys = getSelectedKeys;
currentInstance.setCheckedKeys = setCheckedKeys;
currentInstance.getCheckedKeys = getCheckedKeys;
currentInstance.insertNodeByKey = insertNodeByKey;
currentInstance.deleteNodeByKey = deleteNodeByKey;
currentInstance.updateNodeByKey = updateNodeByKey;
currentInstance.filterByLevel = (level: number) => {
useExpose<TreeActionType>({
setExpandedKeys,
getExpandedKeys,
setSelectedKeys,
getSelectedKeys,
setCheckedKeys,
getCheckedKeys,
insertNodeByKey,
deleteNodeByKey,
updateNodeByKey,
filterByLevel: (level: number) => {
state.expandedKeys = filterByLevel(level);
};
},
});

return () => {
return (
<Tree {...unref(getBindValues)} class={prefixCls}>
Expand Down
6 changes: 3 additions & 3 deletions src/components/Verify/src/DragVerify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { basicProps } from './props';
import { getSlot } from '/@/utils/helper/tsxHelper';
import './DragVerify.less';
import { CheckOutlined, DoubleRightOutlined } from '@ant-design/icons-vue';
import { tryTsxEmit } from '/@/utils/helper/vueHelper';
import type { DragVerifyActionType } from './types';
import { useExpose } from '/@/hooks/core/useExpose';
export default defineComponent({
name: 'BaseDargVerify',
props: basicProps,
Expand Down Expand Up @@ -211,8 +211,8 @@ export default defineComponent({
contentEl.style.width = unref(getContentStyleRef).width;
}

tryTsxEmit<DragVerifyActionType>((instance) => {
instance.resume = resume;
useExpose<DragVerifyActionType>({
resume,
});

return () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Verify/src/ImgRotate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineComponent({
props: rotateProps,
emits: ['success', 'change', 'update:value'],
setup(props, { emit, attrs }) {
const basicRef = ref<RefInstanceType<DragVerifyActionType>>(null);
const basicRef = ref<Nullable<DragVerifyActionType>>(null);
const state = reactive({
showTip: false,
isPassing: false,
Expand Down Expand Up @@ -113,7 +113,7 @@ export default defineComponent({
}
state.isPassing = false;

basicEl.$.resume();
basicEl.resume();
handleImgOnLoad();
}

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/core/useExpose.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCurrentInstance } from 'vue';

// expose public api
export function useExpose(apis: Record<string, any>) {
export function useExpose<T>(apis: T) {
const instance = getCurrentInstance();
if (instance) {
Object.assign(instance.proxy, apis);
Expand Down
4 changes: 0 additions & 4 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ declare type Dictionary<T> = Record<string, T>;

declare type Nullable<T> = T | null;

declare type RefInstanceType<T> = {
$: T;
} | null;

declare type RefType<T> = T | null;

declare type CustomizedHTMLElement<T> = HTMLElement & T;
Expand Down
14 changes: 7 additions & 7 deletions src/views/demo/comp/verify/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@
components: { BasicDragVerify, BugOutlined, RightOutlined },
setup() {
const { createMessage } = useMessage();
const el1 = ref<RefInstanceType<DragVerifyActionType>>(null);
const el2 = ref<RefInstanceType<DragVerifyActionType>>(null);
const el3 = ref<RefInstanceType<DragVerifyActionType>>(null);
const el4 = ref<RefInstanceType<DragVerifyActionType>>(null);
const el5 = ref<RefInstanceType<DragVerifyActionType>>(null);
const el1 = ref<Nullable<DragVerifyActionType>>(null);
const el2 = ref<Nullable<DragVerifyActionType>>(null);
const el3 = ref<Nullable<DragVerifyActionType>>(null);
const el4 = ref<Nullable<DragVerifyActionType>>(null);
const el5 = ref<Nullable<DragVerifyActionType>>(null);
function handleSuccess(data: PassingData) {
const { time } = data;
createMessage.success(`校验成功,耗时${time}秒`);
}
function handleBtnClick(elRef: RefInstanceType<DragVerifyActionType>) {
function handleBtnClick(elRef: Nullable<DragVerifyActionType>) {
if (!elRef) {
return;
}
elRef.$.resume();
elRef.resume();
}
return {
handleSuccess,
Expand Down
4 changes: 2 additions & 2 deletions src/views/demo/tree/ActionTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
export default defineComponent({
components: { BasicTree, CollapseContainer },
setup() {
const treeRef = ref<RefInstanceType<TreeActionType>>(null);
const treeRef = ref<Nullable<TreeActionType>>(null);
const { createMessage } = useMessage();
function getTree() {
const tree = unref(treeRef);
if (!tree) {
throw new Error('tree is null!');
}
return tree.$;
return tree;
}
function handleLevel(level: number) {
Expand Down

0 comments on commit 9bb7514

Please sign in to comment.