优化定时任务

This commit is contained in:
Rewrite0
2023-05-27 23:18:29 +08:00
parent 87a814550b
commit ffe1139d60
4 changed files with 40 additions and 10 deletions

View File

@@ -1,8 +1,14 @@
<script lang="ts" setup>
const { onUpdate } = useLogStore();
const { onUpdate, offUpdate } = useLogStore();
const { log } = storeToRefs(useLogStore());
onBeforeMount(() => onUpdate());
onActivated(() => {
onUpdate();
});
onDeactivated(() => {
offUpdate();
});
definePage({
name: 'Log',

View File

@@ -1,9 +1,12 @@
export const useLogStore = defineStore('log', () => {
const log = ref('');
const timer = ref<NodeJS.Timer | null>(null);
const { auth } = useAuth();
const get = async () => {
log.value = await apiLog.getLog();
if (auth.value !== '') {
log.value = await apiLog.getLog();
}
};
const onUpdate = () => {
@@ -11,14 +14,15 @@ export const useLogStore = defineStore('log', () => {
timer.value = setInterval(() => get(), 3000);
};
const removeUpdate = () => {
const offUpdate = () => {
clearInterval(Number(timer.value));
timer.value = null;
};
return {
log,
get,
onUpdate,
removeUpdate,
offUpdate,
};
});

View File

@@ -1,10 +1,13 @@
export const useProgramStore = defineStore('program', () => {
const message = useMessage();
const { auth } = useAuth();
const running = ref(false);
const timer = ref<NodeJS.Timer | null>(null);
const getStatus = async () => {
running.value = await apiProgram.status();
if (auth.value !== '') {
running.value = await apiProgram.status();
}
};
const onUpdate = () => {
@@ -12,6 +15,11 @@ export const useProgramStore = defineStore('program', () => {
timer.value = setInterval(() => getStatus(), 3000);
};
const offUpdate = () => {
clearInterval(Number(timer.value));
timer.value = null;
};
function handleMessage(handle: string, success: boolean) {
if (success) {
message.success(`${handle} Success!`);
@@ -44,6 +52,7 @@ export const useProgramStore = defineStore('program', () => {
running,
getStatus,
onUpdate,
offUpdate,
start,
pause,

View File

@@ -2,8 +2,10 @@
import { Me, Pause, PlayOne, Power, Refresh } from '@icon-park/vue-next';
const search = ref('');
const show = ref(false);
const { onUpdate, start, pause, shutdown, restart } = useProgramStore();
const { onUpdate, offUpdate, start, pause, shutdown, restart } =
useProgramStore();
const { running } = storeToRefs(useProgramStore());
const items = [
@@ -35,10 +37,19 @@ const items = [
id: 5,
label: 'Profile',
icon: Me,
handle: () => {
show.value = true;
},
},
];
onBeforeMount(() => onUpdate());
onBeforeMount(() => {
onUpdate();
});
onUnmounted(() => {
offUpdate();
});
</script>
<template>
@@ -50,7 +61,7 @@ onBeforeMount(() => onUpdate());
<div ml-auto>
<ab-status-bar :items="items" :running="running"></ab-status-bar>
</div>
<ab-change-account v-model:show="show"></ab-change-account>
</div>
</template>
<style lang="scss" scope></style>