From 4b594c9b514ad352823248700e372519e2fc3881 Mon Sep 17 00:00:00 2001 From: Link Date: Mon, 8 Feb 2021 19:18:00 +0800 Subject: [PATCH] feat: the window page controller adds the maximum number of stacks and vitality --- src/pages/header/component/push-shift.tsx | 2 +- src/router/index.ts | 4 +- src/store/index.ts | 78 ++++++++++++++++------- 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/pages/header/component/push-shift.tsx b/src/pages/header/component/push-shift.tsx index f937aefb..663abfee 100644 --- a/src/pages/header/component/push-shift.tsx +++ b/src/pages/header/component/push-shift.tsx @@ -55,7 +55,7 @@ export const PushShift = defineComponent({ ) const router = useRouter() - const handleRouteCommand = (payload: string) => { + const handleRouteCommand = (payload: COMMAND) => { const { historyRoute } = store.state routeCanBeCollect(false) if (payload === COMMAND.FORWARD) { diff --git a/src/router/index.ts b/src/router/index.ts index d38bddce..f4279fd7 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -247,8 +247,8 @@ export const baseNavRouter: RouteRecordRaw[] = [ component: Moments, meta: { name: '朋友', - browser: true, - electron: true, + browser: false, + electron: false, canBeCollect: true } }, diff --git a/src/store/index.ts b/src/store/index.ts index a887db1a..d6956d71 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,7 +1,11 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { createStore, MutationTree, createLogger } from 'vuex' import { FooterMutations } from '@/interface' import { AllMutations } from '@/interface/index' import { getNodeEnv, toFixed } from '@/utils/index' +import clone from 'lodash/cloneDeep' +import each from 'lodash/each' +import pull from 'lodash/pull' import createPersistedState from 'vuex-persistedstate' import modules from '@/modules/index' @@ -13,20 +17,58 @@ export const enum RootMutations { UPDATE_PERECENTAGE = 'UPDATE_PERECENTAGE' } +export interface CustomRouter { + life: number + url: string +} + interface HistoryRoute { - before: string[] - after: string[] - needRoute: string + [x: string]: any + before: CustomRouter[] + after: CustomRouter[] + needRoute?: string canBeCollect: boolean } export interface RootState { - // eslint-disable-next-line @typescript-eslint/no-explicit-any [x: string]: any historyRoute: HistoryRoute percentage: number } +function removeExpired(route: CustomRouter[]) { + const life = 2 * 24 * 60 * 60 * 1000 + const heap = 20 + // Maximum capacity + const routeTp = clone(route).slice(-1 * (heap - 1)) + each(routeTp, value => { + if (Date.now() - value.life > life) { + pull(routeTp, value) + } + }) + return routeTp +} + +function customRouterBase(state: RootState, originKey: string, route: string) { + const origin = state.historyRoute[originKey] as CustomRouter[] + const afterLast = origin[origin.length - 1] + if (afterLast?.url !== route) { + state.historyRoute[originKey] = removeExpired(origin) + state.historyRoute[originKey].push({ + life: Date.now(), + url: route + }) + } +} + +function customRouterBack(state: RootState, route: string) { + customRouterBase(state, 'after', route) +} + +function customRouterForward(state: RootState, route: string) { + customRouterBase(state, 'before', route) +} + const state: RootState = { historyRoute: { canBeCollect: false, @@ -38,30 +80,18 @@ const state: RootState = { } const mutations: MutationTree = { - [RootMutations.SET_HISTORY_ROUTE](state, oldRoute: string) { - const before = state.historyRoute.before - const beforeLast = before[before.length - 1] - if (beforeLast !== oldRoute) { - state.historyRoute.before.push(oldRoute) - } + [RootMutations.SET_HISTORY_ROUTE](state, route: string) { + customRouterForward(state, route) }, [RootMutations.BACK_HISTORY_ROUTE](state, route: string) { - const before = state.historyRoute.before.pop() as string - const after = state.historyRoute.after - const afterLast = after[after.length - 1] - if (afterLast !== route) { - state.historyRoute.after.push(route) - } - state.historyRoute.needRoute = before + const before = state.historyRoute.before.pop() + state.historyRoute.needRoute = before?.url + customRouterBack(state, route) }, [RootMutations.FORWARD_HISTORY_ROUTE](state, route: string) { - const after = state.historyRoute.after.pop() as string - const before = state.historyRoute.before - const beforeLast = before[before.length - 1] - if (beforeLast !== route) { - state.historyRoute.before.push(route) - } - state.historyRoute.needRoute = after + const after = state.historyRoute.after.pop() + state.historyRoute.needRoute = after?.url + customRouterForward(state, route) }, [RootMutations.CAN_BE_COLLECT](state, collect: boolean) { state.historyRoute.canBeCollect = collect