refactor(hooks): 重构hook函数取消监听方式

This commit is contained in:
燕博文 2022-12-10 17:52:31 +08:00
parent 70aeefea02
commit fd9488673c
3 changed files with 91 additions and 88 deletions

View File

@ -1,4 +1,4 @@
import { nextTick, onUnmounted, ref, watch } from 'vue'; import { nextTick, effectScope, onScopeDispose, ref, watch } from 'vue';
import type { ComputedRef, Ref } from 'vue'; import type { ComputedRef, Ref } from 'vue';
import * as echarts from 'echarts/core'; import * as echarts from 'echarts/core';
import { BarChart, GaugeChart, LineChart, PictorialBarChart, PieChart, RadarChart, ScatterChart } from 'echarts/charts'; import { BarChart, GaugeChart, LineChart, PictorialBarChart, PieChart, RadarChart, ScatterChart } from 'echarts/charts';
@ -128,42 +128,44 @@ export function useEcharts(
render(); render();
} }
const stopSizeWatch = watch([width, height], ([newWidth, newHeight]) => { const scope = effectScope();
initialSize.width = newWidth;
initialSize.height = newHeight; scope.run(() => {
if (newWidth === 0 && newHeight === 0) { watch([width, height], ([newWidth, newHeight]) => {
// 节点被删除 将chart置为空 initialSize.width = newWidth;
chart = null; initialSize.height = newHeight;
} if (newWidth === 0 && newHeight === 0) {
if (canRender()) { // 节点被删除 将chart置为空
if (!isRendered()) { chart = null;
render();
} else {
resize();
} }
} if (canRender()) {
if (!isRendered()) {
render();
} else {
resize();
}
}
});
watch(
options,
newValue => {
update(newValue);
},
{ deep: true }
);
watch(
() => theme.darkMode,
() => {
updateTheme();
}
);
}); });
const stopOptionWatch = watch( onScopeDispose(() => {
options,
newValue => {
update(newValue);
},
{ deep: true }
);
const stopDarkModeWatch = watch(
() => theme.darkMode,
() => {
updateTheme();
}
);
onUnmounted(() => {
destroy(); destroy();
stopSizeWatch(); scope.stop();
stopOptionWatch();
stopDarkModeWatch();
}); });
return { return {

View File

@ -1,4 +1,4 @@
import { computed, ref } from 'vue'; import { computed, onScopeDispose, ref } from 'vue';
import { useBoolean } from '../common'; import { useBoolean } from '../common';
/** /**
@ -42,6 +42,8 @@ export default function useCountDown(second: number) {
counts.value = 0; counts.value = 0;
} }
onScopeDispose(stop);
return { return {
counts, counts,
isCounting, isCounting,

View File

@ -1,4 +1,4 @@
import { onUnmounted, watch } from 'vue'; import { effectScope, onScopeDispose, watch } from 'vue';
import { useOsTheme } from 'naive-ui'; import { useOsTheme } from 'naive-ui';
import type { GlobalThemeOverrides } from 'naive-ui'; import type { GlobalThemeOverrides } from 'naive-ui';
import { useElementSize } from '@vueuse/core'; import { useElementSize } from '@vueuse/core';
@ -12,67 +12,66 @@ export default function subscribeThemeStore() {
const osTheme = useOsTheme(); const osTheme = useOsTheme();
const { width } = useElementSize(document.documentElement); const { width } = useElementSize(document.documentElement);
const { addDarkClass, removeDarkClass } = handleCssDarkMode(); const { addDarkClass, removeDarkClass } = handleCssDarkMode();
const scope = effectScope();
// 监听主题颜色 scope.run(() => {
const stopThemeColor = watch( // 监听主题颜色
() => theme.themeColor, watch(
newValue => { () => theme.themeColor,
localStg.set('themeColor', newValue); newValue => {
}, localStg.set('themeColor', newValue);
{ immediate: true } },
); { immediate: true }
);
// 监听naiveUI themeOverrides // 监听naiveUI themeOverrides
const stopThemeOverrides = watch( watch(
() => theme.naiveThemeOverrides, () => theme.naiveThemeOverrides,
newValue => { newValue => {
if (newValue.common) { if (newValue.common) {
addThemeCssVarsToHtml(newValue.common); addThemeCssVarsToHtml(newValue.common);
}
},
{ immediate: true }
);
// 监听暗黑模式
watch(
() => theme.darkMode,
newValue => {
if (newValue) {
addDarkClass();
} else {
removeDarkClass();
}
},
{
immediate: true
} }
}, );
{ immediate: true }
);
// 监听暗黑模式 // 监听操作系统主题模式
const stopDarkMode = watch( watch(
() => theme.darkMode, osTheme,
newValue => { newValue => {
if (newValue) { const isDark = newValue === 'dark';
addDarkClass(); theme.setAutoFollowSystemMode(isDark);
},
{ immediate: true }
);
// 禁用横向滚动(页面切换时,过渡动画会产生水平方向的滚动条, 小于最小宽度时,不禁止)
watch(width, newValue => {
if (newValue < theme.layout.minWidth) {
document.documentElement.style.overflowX = 'auto';
} else { } else {
removeDarkClass(); document.documentElement.style.overflowX = 'hidden';
} }
}, });
{
immediate: true
}
);
// 监听操作系统主题模式
const stopOsTheme = watch(
osTheme,
newValue => {
const isDark = newValue === 'dark';
theme.setAutoFollowSystemMode(isDark);
},
{ immediate: true }
);
// 禁用横向滚动(页面切换时,过渡动画会产生水平方向的滚动条, 小于最小宽度时,不禁止)
const stopWidth = watch(width, newValue => {
if (newValue < theme.layout.minWidth) {
document.documentElement.style.overflowX = 'auto';
} else {
document.documentElement.style.overflowX = 'hidden';
}
}); });
onUnmounted(() => { onScopeDispose(() => {
stopThemeColor(); scope.stop();
stopThemeOverrides();
stopDarkMode();
stopOsTheme();
stopWidth();
}); });
} }