Refactoring

This commit is contained in:
Sebastien Riviere
2026-02-18 10:57:08 +01:00
parent 4d8dcd241c
commit 776bbcd723
23 changed files with 191 additions and 265 deletions

View File

@@ -1,5 +1,5 @@
// Constants
import { SERVER_URL } from "../constants";
import { SERVER_URL } from "../../constants";
export const uploadTeamImage = async (id, imageUri) => {
if (!imageUri || !id) return;

View File

@@ -1,7 +1,7 @@
// Socket
import { io } from "socket.io-client";
// Constants
import { SOCKET_URL } from "../constants";
import { SOCKET_URL } from "../../constants";
export const socket = io(SOCKET_URL, {
path: "/back/socket.io"

View File

@@ -0,0 +1,55 @@
// Services
import { socket } from "./connection";
const customEmit = (event, ...args) => {
if (!socket?.connected) return false;
console.log("Emit", event);
socket.emit(event, ...args);
return true;
};
const customEmitCallback = (event, ...args) => {
return new Promise((resolve, reject) => {
if (!socket?.connected) return reject(new Error("Socket not connected"));
console.log("Emit", event);
const timeout = setTimeout(() => {
console.warn("Server timeout");
reject(new Error("Timeout"));
}, 3000);
socket.emit(event, ...args, (response) => {
clearTimeout(timeout);
resolve(response.length > 1 ? response : response[0]);
});
});
};
export const emitLogin = (password) => {
return customEmitCallback("login", password);
};
export const emitLogout = () => {
return customEmit("logout");
};
export const emitSendPosition = () => {
return customEmit("send_position");
};
export const emitUpdatePosition = (location) => {
return customEmit("update_position", location);
};
export const emitCapture = (captureCode) => {
return customEmitCallback("capture", captureCode);
};
export const emitBattery = (level) => {
return customEmit("battery_update", level);
};
export const emitDeviceInfo = (infos) => {
return customEmit("device_info", infos);
};

View File

@@ -1,44 +0,0 @@
// Services
import { socket } from "./socket";
export const emitLogin = (password, callback) => {
if (!socket?.connected) return;
console.log("Try to log in with :", password);
socket.emit("login", password, callback);
};
export const emitLogout = () => {
if (!socket?.connected) return;
console.log("Logout.");
socket.emit("logout");
};
export const emitSendPosition = () => {
if (!socket?.connected) return;
console.log("Reveal position.");
socket.emit("send_position");
};
export const emitUpdatePosition = (location) => {
if (!socket?.connected) return;
console.log("Update position :", location);
socket.emit("update_position", location);
};
export const emitCapture = (captureCode, callback) => {
if (!socket?.connected) return;
console.log("Try to capture :", captureCode);
socket.emit("capture", captureCode, callback);
};
export const emitBattery = (level) => {
if (!socket?.connected) return;
console.log("Send battery level.");
socket.emit('battery_update', level);
};
export const emitDeviceInfo = (infos) => {
if (!socket?.connected) return;
console.log("Send device infos.");
socket.emit('device_info', infos);
};

View File

@@ -2,9 +2,9 @@
import { defineTask, isTaskRegisteredAsync } from "expo-task-manager";
import * as Location from 'expo-location';
// Services
import { emitUpdatePosition } from "./socketEmitter";
import { emitUpdatePosition } from "../socket/emitters";
// Constants
import { TASKS, LOCATION_PARAMETERS } from "../constants";
import { TASKS, LOCATION_PARAMETERS } from "../../constants";
// Task