新增任务队列功能,支持定时执行任务并在播放器视图中集成

This commit is contained in:
hello8693 2025-03-22 17:45:45 +08:00
parent 77a69e206b
commit 8ce00050e6
2 changed files with 52 additions and 32 deletions

View File

@ -0,0 +1,46 @@
import { ref } from 'vue';
import { v4 as uuidv4 } from 'uuid';
interface Task {
id: string;
executeTime: number;
taskFunction: () => void;
}
const taskQueue = ref<Task[]>([]);
const intervalId = ref<number | null>(null);
const addTask = (executeTime: number, taskFunction: () => void) => {
const task: Task = {
id: uuidv4(),
executeTime,
taskFunction,
};
taskQueue.value.push(task);
taskQueue.value.sort((a, b) => a.executeTime - b.executeTime);
};
const checkTasks = () => {
const now = new Date().getTime();
while (taskQueue.value.length > 0 && taskQueue.value[0].executeTime <= now) {
const task = taskQueue.value.shift();
if (task) {
task.taskFunction();
}
}
};
const startTaskQueue = () => {
if (!intervalId.value) {
intervalId.value = window.setInterval(checkTasks, 200);
}
};
const stopTaskQueue = () => {
if (intervalId.value) {
clearInterval(intervalId.value);
intervalId.value = null;
}
};
export { addTask, startTaskQueue, stopTaskQueue };

View File

@ -1,32 +1,12 @@
<script setup lang="jsx">
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { v4 as uuidv4 } from 'uuid';
import { addTask, startTaskQueue, stopTaskQueue } from '@renderer/core/taskQueue';
const { ipcRenderer } = window.electron;
const taskQueue = ref([]);
const intervalId = ref(null);
const configData = ref(null);
const addTask = (executeTime, taskFunction) => {
const task = {
id: uuidv4(),
executeTime,
taskFunction,
};
taskQueue.value.push(task);
taskQueue.value.sort((a, b) => a.executeTime - b.executeTime);
};
const checkTasks = () => {
const now = new Date().getTime();
while (taskQueue.value.length > 0 && taskQueue.value[0].executeTime <= now) {
const task = taskQueue.value.shift();
task.taskFunction();
}
};
const configData = ref<any>(null);
onMounted(() => {
intervalId.value = setInterval(checkTasks, 200);
startTaskQueue();
console.log('Sending load-config event');
ipcRenderer.on('load-config', (event, data) => {
@ -36,16 +16,10 @@ onMounted(() => {
});
onUnmounted(() => {
if (intervalId.value) {
clearInterval(intervalId.value);
}
stopTaskQueue();
});
</script>
<template>
<div v-if="configData">
<!-- 在这里显示配置文件内容 -->
<pre>{{ configData }}</pre>
</div>
</template>