dev #94

Merged
Thibaut merged 4 commits from dev into master 2026-01-03 20:50:18 +00:00
Showing only changes of commit e8ee5811a0 - Show all commits

View File

@ -1,6 +1,4 @@
import {createContext, useContext, useEffect, useId, useReducer, useRef, useState} from "react"; import {createContext, useContext, useEffect, useId, useReducer, useRef, useState} from "react";
import {apiAxios} from "../utils/Tools.js";
import {toast} from "react-toastify";
import {useAuth} from "./useAuth.jsx"; import {useAuth} from "./useAuth.jsx";
function uuidv4() { function uuidv4() {
@ -45,6 +43,7 @@ export function WSProvider({url, onmessage, children}) {
const id = useId(); const id = useId();
const {is_authenticated} = useAuth() const {is_authenticated} = useAuth()
const [isReady, setIsReady] = useState(false) const [isReady, setIsReady] = useState(false)
const [doReconnect, setDoReconnect] = useState(false)
const [state, dispatch] = useReducer(reducer, {listener: []}) const [state, dispatch] = useReducer(reducer, {listener: []})
const ws = useRef(null) const ws = useRef(null)
const listenersRef = useRef([]) const listenersRef = useRef([])
@ -59,10 +58,33 @@ export function WSProvider({url, onmessage, children}) {
listenersRef.current = state.listener listenersRef.current = state.listener
}, [state.listener]) }, [state.listener])
useEffect(() => {
if (!doReconnect && !is_authenticated && isReady)
return;
const timer = setInterval(() => {
if (isReady || !doReconnect || !is_authenticated)
return;
console.log("WSProvider: reconnecting to", url);
try {
const newSocket = new WebSocket(url)
newSocket.onopen = ws.current.onopen
newSocket.onclose = ws.current.onclose
newSocket.onmessage = ws.current.onmessage
ws.current = newSocket
}catch (e) {
}
}, 5000);
return () => clearInterval(timer);
}, [isReady, doReconnect, is_authenticated]);
useEffect(() => { useEffect(() => {
if (!mountCounter[id]) if (!mountCounter[id])
mountCounter[id] = 0 mountCounter[id] = 0
mountCounter[id] += 1 mountCounter[id] += 1
setDoReconnect(true)
console.log(`WSProvider ${id} mounted ${mountCounter[id]} time(s)`); console.log(`WSProvider ${id} mounted ${mountCounter[id]} time(s)`);
if (mountCounter[id] === 1 && (ws.current === null || ws.current.readyState >= WebSocket.CLOSING)){ if (mountCounter[id] === 1 && (ws.current === null || ws.current.readyState >= WebSocket.CLOSING)){
@ -72,24 +94,6 @@ export function WSProvider({url, onmessage, children}) {
socket.onopen = () => setIsReady(true) socket.onopen = () => setIsReady(true)
socket.onclose = () => { socket.onclose = () => {
setIsReady(false) setIsReady(false)
if (mountCounter[id] > 0) {
setTimeout(() => {
//if (is_authenticated){
console.log("WSProvider: reconnecting to", url);
try {
const newSocket = new WebSocket(url)
ws.current = newSocket
newSocket.onopen = socket.onopen
newSocket.onclose = socket.onclose
newSocket.onmessage = socket.onmessage
}catch (e) {
}
//}else{
// console.log("WSProvider: not reconnecting, user is not authenticated");
//}
}, 5000)
}
} }
socket.onmessage = (event) => { socket.onmessage = (event) => {
const msg = JSON.parse(event.data) const msg = JSON.parse(event.data)
@ -132,6 +136,7 @@ export function WSProvider({url, onmessage, children}) {
setTimeout(() => { setTimeout(() => {
console.log(`WSProvider ${id} checking for close, ${mountCounter[id]} instance(s) remain`); console.log(`WSProvider ${id} checking for close, ${mountCounter[id]} instance(s) remain`);
if (mountCounter[id] === 0) { if (mountCounter[id] === 0) {
setDoReconnect(false)
console.log("WSProvider: closing connection to", url); console.log("WSProvider: closing connection to", url);
ws.current.close() ws.current.close()
} }
@ -139,13 +144,14 @@ export function WSProvider({url, onmessage, children}) {
} }
}, []) }, [])
const send = (uuid, code, type, data, resolve = () => { const send2 = (uuid, code, type, data, resolve = () => {
}, reject = () => { }, reject = () => {
}) => { }) => {
if (!isReadyRef.current) { if (!isReadyRef.current) {
reject("WebSocket is not connected"); reject("WebSocket is not connected");
return; return;
} }
if (type === "REQUEST") { if (type === "REQUEST") {
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
reject("timeout"); reject("timeout");
@ -172,6 +178,26 @@ export function WSProvider({url, onmessage, children}) {
data: data data: data
})) }))
} }
const send = (uuid, code, type, data, resolve = () => {
}, reject = () => {
}) => {
if (isReadyRef.current) {
send2(uuid, code, type, data, resolve, reject);
}else {
let counter = 0;
const waitInterval = setInterval(() => {
if (isReadyRef.current) {
clearInterval(waitInterval);
send2(uuid, code, type, data, resolve, reject);
}
counter += 1;
if (counter >= 300) { // 30 seconds timeout
clearInterval(waitInterval);
reject("WebSocket is not connected");
}
}, 100);
}
}
const ret = {isReady, dispatch, send, wait_length: callbackRef} const ret = {isReady, dispatch, send, wait_length: callbackRef}