Files
sim-location-frontend/src/stores/tunneld.ts
T
2026-06-10 11:00:10 -04:00

72 lines
2.4 KiB
TypeScript

import { defineStore, acceptHMRUpdate } from 'pinia';
import type { TunneldCommands } from 'components/models';
import { useSocketStore } from 'stores/socket';
import { socket } from 'boot/socketio';
export const useTunneldStore = defineStore('tunneldStore', {
state: () => {
return {
tunnelConnected: false as boolean,
tunneldWatcher: false as boolean,
};
},
getters: {},
actions: {
digestUpdate(data: boolean) {
this.tunnelConnected = data;
},
tunneldControl(command: TunneldCommands, udid: string = '') {
const socketStore = useSocketStore();
const debugLog = socketStore.debugLog;
let functionReturn: { status: string; message?: string | undefined } = { status: '', message: '' };
if (debugLog) {
console.log('tunneldStore: got command: ', command);
}
let args: {command: TunneldCommands, udid?: string} = {command: command};
if (udid != '') {
args = { command: command, udid: udid };
}
socket.emit(
'tunneld_control', args, (response) => {
console.log(response.command_status, response.command);
if (response.command_status == 'ERROR') {
functionReturn = { status: 'error', message: response.message?.toString() };
throw new Error(response.message);
} else {
functionReturn = { status: 'OK', message: response.message?.toString() };
}
},
);
return functionReturn;
},
async tunneldControlAsync(command: TunneldCommands, udid: string = ''): Promise<{ status: string; message: string }> {
const socketStore = useSocketStore();
const debugLog = socketStore.debugLog;
if (debugLog) {
console.log('tunneldStore: got command: ', command);
}
let args: { command: TunneldCommands; udid?: string } = { command: command };
if (udid !== '') {
args = { command: command, udid: udid };
}
return await new Promise((resolve) => {
socket.emit('tunneld_control', args, (response) => {
if (debugLog) {
console.log(response.command_status, response.command);
}
resolve({
status: response.command_status === 'ERROR' ? 'error' : 'OK',
message: response.message || '',
});
});
});
}
}
});
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useTunneldStore, import.meta.hot));
}