Blame view

src/logics/mitt/tabChange.ts 884 Bytes
vben authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Used to monitor routing changes to change the status of menus and tabs. There is no need to monitor the route, because the route status change is affected by the page rendering time, which will be slow
 */

import Mitt from '/@/utils/mitt';
import type { RouteLocationNormalized } from 'vue-router';
import { getRoute } from '/@/router/helper/routeHelper';

const mitt = new Mitt();

const key = Symbol();

let lastChangeTab: RouteLocationNormalized;

export function setLastChangeTab(lastChangeRoute: RouteLocationNormalized) {
16
17
18
  const r = getRoute(lastChangeRoute);
  mitt.emit(key, r);
  lastChangeTab = r;
vben authored
19
20
21
22
23
24
25
}

export function listenerLastChangeTab(
  callback: (route: RouteLocationNormalized) => void,
  immediate = true
) {
  mitt.on(key, callback);
vben authored
26
  immediate && lastChangeTab && callback(lastChangeTab);
vben authored
27
}
28
29
30
31

export function removeTabChangeListener() {
  mitt.clear();
}