Skip to content

Commit 5efc4c7

Browse files
committed
perf: perf the control logic of Tab
1 parent 4f5e715 commit 5efc4c7

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

packages/stores/src/modules/tabbar.test.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('useAccessStore', () => {
2121
const store = useTabbarStore();
2222
const tab: any = {
2323
fullPath: '/home',
24+
key: 'home',
2425
meta: {},
2526
name: 'Home',
2627
path: '/home',
@@ -35,6 +36,7 @@ describe('useAccessStore', () => {
3536
const newTab: any = {
3637
fullPath: '/new',
3738
meta: {},
39+
key: '/new',
3840
name: 'New',
3941
path: '/new',
4042
};
@@ -46,12 +48,14 @@ describe('useAccessStore', () => {
4648
const store = useTabbarStore();
4749
const initialTab: any = {
4850
fullPath: '/existing',
49-
meta: {},
51+
meta: {
52+
fullPathKey: false,
53+
},
5054
name: 'Existing',
5155
path: '/existing',
5256
query: {},
5357
};
54-
store.tabs.push(initialTab);
58+
store.addTab(initialTab);
5559
const updatedTab = { ...initialTab, query: { id: '1' } };
5660
store.addTab(updatedTab);
5761
expect(store.tabs.length).toBe(1);
@@ -157,7 +161,7 @@ describe('useAccessStore', () => {
157161
path: '/contact',
158162
} as any);
159163

160-
await store._bulkCloseByPaths(['/home', '/contact']);
164+
await store._bulkCloseByKeys(['/home', '/contact']);
161165

162166
expect(store.tabs).toHaveLength(1);
163167
expect(store.tabs[0]?.name).toBe('About');
@@ -183,9 +187,8 @@ describe('useAccessStore', () => {
183187
name: 'Contact',
184188
path: '/contact',
185189
};
186-
store.addTab(targetTab);
187-
188-
await store.closeLeftTabs(targetTab);
190+
const addTargetTab = store.addTab(targetTab);
191+
await store.closeLeftTabs(addTargetTab);
189192

190193
expect(store.tabs).toHaveLength(1);
191194
expect(store.tabs[0]?.name).toBe('Contact');
@@ -205,15 +208,15 @@ describe('useAccessStore', () => {
205208
name: 'About',
206209
path: '/about',
207210
};
208-
store.addTab(targetTab);
211+
const addTargetTab = store.addTab(targetTab);
209212
store.addTab({
210213
fullPath: '/contact',
211214
meta: {},
212215
name: 'Contact',
213216
path: '/contact',
214217
} as any);
215218

216-
await store.closeOtherTabs(targetTab);
219+
await store.closeOtherTabs(addTargetTab);
217220

218221
expect(store.tabs).toHaveLength(1);
219222
expect(store.tabs[0]?.name).toBe('About');
@@ -227,7 +230,7 @@ describe('useAccessStore', () => {
227230
name: 'Home',
228231
path: '/home',
229232
};
230-
store.addTab(targetTab);
233+
const addTargetTab = store.addTab(targetTab);
231234
store.addTab({
232235
fullPath: '/about',
233236
meta: {},
@@ -241,7 +244,7 @@ describe('useAccessStore', () => {
241244
path: '/contact',
242245
} as any);
243246

244-
await store.closeRightTabs(targetTab);
247+
await store.closeRightTabs(addTargetTab);
245248

246249
expect(store.tabs).toHaveLength(1);
247250
expect(store.tabs[0]?.name).toBe('Home');

packages/stores/src/modules/tabbar.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ export const useTabbarStore = defineStore('core-tabbar', {
104104
* @zh_CN 添加标签页
105105
* @param routeTab
106106
*/
107-
addTab(routeTab: TabDefinition) {
107+
addTab(routeTab: TabDefinition): TabDefinition {
108108
const tab = cloneTab(routeTab);
109109
// 如果未设置key,设置tab的key
110110
if (!tab.key) {
111111
tab.key = getTabKey(routeTab);
112112
}
113113
if (!isTabShown(tab)) {
114-
return;
114+
return tab;
115115
}
116116

117117
const tabIndex = this.tabs.findIndex((item) => {
@@ -165,6 +165,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
165165
this.tabs.splice(tabIndex, 1, mergedTab);
166166
}
167167
this.updateCacheTabs();
168+
return tab;
168169
},
169170
/**
170171
* @zh_CN 关闭所有标签页
@@ -593,7 +594,9 @@ function getTabKey(tab: RouteLocationNormalized | RouteRecordNormalized) {
593594
if (pageKey) {
594595
return pageKey as string;
595596
}
596-
return decodeURIComponent(((fullPathKey ?? true) ? fullPath : path) || path);
597+
const rawKey =
598+
fullPathKey === true || fullPathKey === undefined ? fullPath : path;
599+
return decodeURIComponent(rawKey || path);
597600
}
598601

599602
function routeToTab(route: RouteRecordNormalized) {

0 commit comments

Comments
 (0)