web-map/src/stores/follow-view.store.ts

96 lines
2.0 KiB
TypeScript
Raw Normal View History

/**
* Store
* 使 Pinia
*/
import { defineStore } from 'pinia';
import { computed,ref } from 'vue';
export interface FollowViewState {
isFollowing: boolean;
robotId: string;
robotName: string;
timer: NodeJS.Timeout | null;
}
export const useFollowViewStore = defineStore('followView', () => {
// 状态
const state = ref<FollowViewState>({
isFollowing: false,
robotId: '',
robotName: '',
timer: null,
});
// 计算属性
const isFollowing = computed(() => state.value.isFollowing);
const robotId = computed(() => state.value.robotId);
const robotName = computed(() => state.value.robotName);
/**
*
* @param robotId ID
* @param robotName
* @param editor
*/
const startFollow = (robotId: string, robotName: string, editor: any) => {
// 如果已经在跟随其他机器人,先停止
if (state.value.isFollowing && state.value.robotId !== robotId) {
stopFollow();
}
// 更新状态
state.value.isFollowing = true;
state.value.robotId = robotId;
state.value.robotName = robotName;
// 立即执行一次聚焦
editor.gotoById(robotId);
// 设置定时器每10毫秒执行一次
state.value.timer = setInterval(() => {
if (state.value.isFollowing) {
editor.gotoById(robotId);
}
}, 10);
console.log('开始视角跟随:', robotId, robotName);
};
/**
*
*/
const stopFollow = () => {
state.value.isFollowing = false;
state.value.robotId = '';
state.value.robotName = '';
if (state.value.timer) {
clearInterval(state.value.timer);
state.value.timer = null;
}
console.log('停止视角跟随');
};
/**
*
*/
const reset = () => {
stopFollow();
};
return {
// 状态
state,
// 计算属性
isFollowing,
robotId,
robotName,
// 方法
startFollow,
stopFollow,
reset,
};
});