Compare commits

..

No commits in common. "3b5955ff31bdc5e78059e36f4b1ff56d3b8e92a5" and "9624ff93f0f03e2023b3be8c292612d2dc8be160" have entirely different histories.

3 changed files with 26 additions and 59 deletions

View File

@ -3,7 +3,10 @@ package fr.titionfire.ffsaf.ws.recv;
import fr.titionfire.ffsaf.data.model.CategoryModel; import fr.titionfire.ffsaf.data.model.CategoryModel;
import fr.titionfire.ffsaf.data.model.MatchModel; import fr.titionfire.ffsaf.data.model.MatchModel;
import fr.titionfire.ffsaf.data.model.TreeModel; import fr.titionfire.ffsaf.data.model.TreeModel;
import fr.titionfire.ffsaf.data.repository.*; import fr.titionfire.ffsaf.data.repository.CategoryRepository;
import fr.titionfire.ffsaf.data.repository.CompetitionRepository;
import fr.titionfire.ffsaf.data.repository.MatchRepository;
import fr.titionfire.ffsaf.data.repository.TreeRepository;
import fr.titionfire.ffsaf.domain.entity.MatchEntity; import fr.titionfire.ffsaf.domain.entity.MatchEntity;
import fr.titionfire.ffsaf.domain.entity.TreeEntity; import fr.titionfire.ffsaf.domain.entity.TreeEntity;
import fr.titionfire.ffsaf.rest.exception.DForbiddenException; import fr.titionfire.ffsaf.rest.exception.DForbiddenException;
@ -43,9 +46,6 @@ public class RCategorie {
@Inject @Inject
TreeRepository treeRepository; TreeRepository treeRepository;
@Inject
CardboardRepository cardboardRepository;
private Uni<CategoryModel> getById(long id, WebSocketConnection connection) { private Uni<CategoryModel> getById(long id, WebSocketConnection connection) {
return categoryRepository.findById(id) return categoryRepository.findById(id)
.invoke(Unchecked.consumer(o -> { .invoke(Unchecked.consumer(o -> {
@ -210,7 +210,6 @@ public class RCategorie {
public Uni<Void> deleteCategory(WebSocketConnection connection, Long id) { public Uni<Void> deleteCategory(WebSocketConnection connection, Long id) {
return getById(id, connection) return getById(id, connection)
.call(cat -> Panache.withTransaction(() -> treeRepository.delete("category = ?1", cat.getId()) .call(cat -> Panache.withTransaction(() -> treeRepository.delete("category = ?1", cat.getId())
.call(__ -> cardboardRepository.delete("match.category = ?1", cat))
.call(__ -> matchRepository.delete("category = ?1", cat)))) .call(__ -> matchRepository.delete("category = ?1", cat))))
.chain(cat -> Panache.withTransaction(() -> categoryRepository.delete(cat))) .chain(cat -> Panache.withTransaction(() -> categoryRepository.delete(cat)))
.invoke(__ -> SSCategorie.sendDelCategory(connection, id)) .invoke(__ -> SSCategorie.sendDelCategory(connection, id))

View File

@ -44,9 +44,6 @@ public class RMatch {
@Inject @Inject
CompetitionGuestRepository competitionGuestRepository; CompetitionGuestRepository competitionGuestRepository;
@Inject
CardboardRepository cardboardRepository;
private Uni<MatchModel> getById(long id, WebSocketConnection connection) { private Uni<MatchModel> getById(long id, WebSocketConnection connection) {
return matchRepository.findById(id) return matchRepository.findById(id)
.invoke(Unchecked.consumer(o -> { .invoke(Unchecked.consumer(o -> {
@ -280,10 +277,7 @@ public class RMatch {
@WSReceiver(code = "deleteMatch", permission = PermLevel.ADMIN) @WSReceiver(code = "deleteMatch", permission = PermLevel.ADMIN)
public Uni<Void> deleteMatch(WebSocketConnection connection, Long idMatch) { public Uni<Void> deleteMatch(WebSocketConnection connection, Long idMatch) {
return getById(idMatch, connection) return getById(idMatch, connection)
.map(__ -> idMatch) .chain(matchModel -> Panache.withTransaction(() -> matchRepository.delete(matchModel)))
.chain(l -> Panache.withTransaction(() ->
cardboardRepository.delete("match.id = ?1", l)
.chain(__ -> matchRepository.delete("id = ?1", l))))
.invoke(__ -> SSMatch.sendDeleteMatch(connection, idMatch)) .invoke(__ -> SSMatch.sendDeleteMatch(connection, idMatch))
.replaceWithVoid(); .replaceWithVoid();
} }

View File

@ -1,4 +1,6 @@
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() {
@ -43,7 +45,6 @@ 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([])
@ -58,33 +59,10 @@ 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)){
@ -94,6 +72,24 @@ 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)
@ -136,7 +132,6 @@ 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()
} }
@ -144,14 +139,13 @@ export function WSProvider({url, onmessage, children}) {
} }
}, []) }, [])
const send2 = (uuid, code, type, data, resolve = () => { const send = (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");
@ -178,26 +172,6 @@ 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}