Skip to content

Commit

Permalink
perf: 调整防抖函数使用位置
Browse files Browse the repository at this point in the history
  • Loading branch information
lengyibai committed Jul 2, 2024
1 parent 5e2efa2 commit 06d1231
Showing 1 changed file with 70 additions and 70 deletions.
140 changes: 70 additions & 70 deletions src/layout/components/Sidebar/hooks/useCollapse.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { onScopeDispose, ref } from "vue";

import { _debounce, dayjs } from "@/utils/tool";

/** 是否已注册resize事件 */
let is_registered = false;
/** 事件池 */
const eventPool = new Map<number, (status: boolean) => void>();

const ExposeData = {
/** 折叠 */
collapse: ref(false),
};
const { collapse } = ExposeData;

/** @description 侧边栏折叠 */
const useCollapse = (callback?: (status: boolean) => void) => {
/** 生成事件ID */
const event_id = dayjs().valueOf();

/** @description 折叠状态发生改变时调用事件池 */
const triggerCollapse = () => {
for (const [_, fn] of eventPool) {
fn(collapse.value);
}
};
const ExposeMethods = {
/** @description 切换折叠 */
toggleCollapse() {
collapse.value = !collapse.value;
triggerCollapse();
},

/** @description 控制折叠
* @param v 折叠状态
*/
setCollapse(v: boolean) {
collapse.value = v;
triggerCollapse();
},
};

//注入事件池
callback && eventPool.set(event_id, callback);

//只允许注册一次resize事件
if (!is_registered) {
is_registered = true;

const debounceSidebarStatus = _debounce(() => {
collapse.value = window.innerWidth < 640;
triggerCollapse();
}, 250);

debounceSidebarStatus();
window.addEventListener("resize", debounceSidebarStatus);
}

onScopeDispose(() => {
eventPool.delete(event_id);
});

return {
...ExposeData,
...ExposeMethods,
};
};

export { useCollapse };
/* eslint-disable @typescript-eslint/no-unused-vars */
import { onScopeDispose, ref } from "vue";

import { _debounce, dayjs } from "@/utils/tool";

/** 是否已注册resize事件 */
let is_registered = false;
/** 事件池 */
const eventPool = new Map<number, (status: boolean) => void>();

const ExposeData = {
/** 折叠 */
collapse: ref(false),
};
const { collapse } = ExposeData;

/** @description 侧边栏折叠 */
const useCollapse = (callback?: (status: boolean) => void) => {
/** 生成事件ID */
const event_id = dayjs().valueOf();

/** @description 折叠状态发生改变时调用事件池 */
const triggerCollapse = _debounce(() => {
for (const [_, fn] of eventPool) {
fn(collapse.value);
}
}, 250);
const ExposeMethods = {
/** @description 切换折叠 */
toggleCollapse() {
collapse.value = !collapse.value;
triggerCollapse();
},

/** @description 控制折叠
* @param v 折叠状态
*/
setCollapse(v: boolean) {
collapse.value = v;
triggerCollapse();
},
};

//注入事件池
callback && eventPool.set(event_id, callback);

//只允许注册一次resize事件
if (!is_registered) {
is_registered = true;

const sidebarStatus = () => {
collapse.value = window.innerWidth < 640;
triggerCollapse();
};

sidebarStatus();
window.addEventListener("resize", sidebarStatus);
}

onScopeDispose(() => {
eventPool.delete(event_id);
});

return {
...ExposeData,
...ExposeMethods,
};
};

export { useCollapse };

0 comments on commit 06d1231

Please sign in to comment.