Skip to content

Commit eaedf7f

Browse files
committed
fix(projects): tab closure did not remove cache correctly.
1 parent 41b5f49 commit eaedf7f

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/layouts/modules/global-tab/index.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useElementBounding } from '@vueuse/core';
55
import { PageTab } from '@sa/materials';
66
import { useAppStore } from '@/store/modules/app';
77
import { useThemeStore } from '@/store/modules/theme';
8-
import { useRouteStore } from '@/store/modules/route';
98
import { useTabStore } from '@/store/modules/tab';
109
import { isPC } from '@/utils/agent';
1110
import BetterScroll from '@/components/custom/better-scroll.vue';
@@ -18,7 +17,6 @@ defineOptions({
1817
const route = useRoute();
1918
const appStore = useAppStore();
2019
const themeStore = useThemeStore();
21-
const routeStore = useRouteStore();
2220
const tabStore = useTabStore();
2321
2422
const bsWrapper = ref<HTMLElement>();
@@ -82,12 +80,8 @@ function getContextMenuDisabledKeys(tabId: string) {
8280
return disabledKeys;
8381
}
8482
85-
async function handleCloseTab(tab: App.Global.Tab) {
86-
await tabStore.removeTab(tab.id);
87-
88-
if (themeStore.resetCacheStrategy === 'close') {
89-
routeStore.resetRouteCache(tab.routeKey);
90-
}
83+
function handleCloseTab(tab: App.Global.Tab) {
84+
tabStore.removeTab(tab.id);
9185
}
9286
9387
async function refresh() {

src/store/modules/tab/index.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,22 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
9898
const removeTabIndex = tabs.value.findIndex(tab => tab.id === tabId);
9999
if (removeTabIndex === -1) return;
100100

101+
const removedTabRouteKey = tabs.value[removeTabIndex].routeKey;
101102
const isRemoveActiveTab = activeTabId.value === tabId;
102103
const nextTab = tabs.value[removeTabIndex + 1] || homeTab.value;
103104

105+
// remove tab
104106
tabs.value.splice(removeTabIndex, 1);
107+
108+
// if current tab is removed, then switch to next tab
105109
if (isRemoveActiveTab && nextTab) {
106110
await switchRouteByTab(nextTab);
107111
}
112+
113+
// reset route cache if cache strategy is close
114+
if (themeStore.resetCacheStrategy === 'close') {
115+
routeStore.resetRouteCache(removedTabRouteKey);
116+
}
108117
}
109118

110119
/** remove active tab */
@@ -131,9 +140,26 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
131140
*/
132141
async function clearTabs(excludes: string[] = []) {
133142
const remainTabIds = [...getFixedTabIds(tabs.value), ...excludes];
134-
const removedTabsIds = tabs.value.map(tab => tab.id).filter(id => !remainTabIds.includes(id));
143+
144+
// Identify tabs to be removed and collect their routeKeys if strategy is 'close'
145+
const tabsToRemove = tabs.value.filter(tab => !remainTabIds.includes(tab.id));
146+
const routeKeysToReset: RouteKey[] = [];
147+
148+
if (themeStore.resetCacheStrategy === 'close') {
149+
for (const tab of tabsToRemove) {
150+
routeKeysToReset.push(tab.routeKey);
151+
}
152+
}
153+
154+
const removedTabsIds = tabsToRemove.map(tab => tab.id);
155+
156+
// If no tabs are actually being removed based on excludes and fixed tabs, exit
157+
if (removedTabsIds.length === 0) {
158+
return;
159+
}
135160

136161
const isRemoveActiveTab = removedTabsIds.includes(activeTabId.value);
162+
// filterTabsByIds returns tabs NOT in removedTabsIds, so these are the tabs that will remain
137163
const updatedTabs = filterTabsByIds(removedTabsIds, tabs.value);
138164

139165
function update() {
@@ -142,13 +168,21 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
142168

143169
if (!isRemoveActiveTab) {
144170
update();
145-
return;
171+
} else {
172+
const activeTabCandidate = updatedTabs[updatedTabs.length - 1] || homeTab.value;
173+
174+
if (activeTabCandidate) {
175+
// Ensure there's a tab to switch to
176+
await switchRouteByTab(activeTabCandidate);
177+
}
178+
// Update the tabs array regardless of switch success or if a candidate was found
179+
update();
146180
}
147181

148-
const activeTab = updatedTabs[updatedTabs.length - 1] || homeTab.value;
149-
150-
await switchRouteByTab(activeTab);
151-
update();
182+
// After tabs are updated and route potentially switched, reset cache for removed tabs
183+
for (const routeKey of routeKeysToReset) {
184+
routeStore.resetRouteCache(routeKey);
185+
}
152186
}
153187

154188
const { routerPushByKey } = useRouterPush();

0 commit comments

Comments
 (0)