Skip to content

Commit

Permalink
Update tp ap.js 1.2.0 and remove disconnection detector
Browse files Browse the repository at this point in the history
  • Loading branch information
aki665 committed Nov 9, 2024
1 parent 83d68f0 commit 34c0063
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 374 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@react-navigation/native": "^6.1.14",
"@react-navigation/native-stack": "^6.9.22",
"@shopify/flash-list": "1.6.4",
"archipelago.js": "^1.1.0",
"archipelago.js": "1.2.0",
"expo": "^51.0.18",
"expo-status-bar": "~1.12.1",
"react": "18.2.0",
Expand Down
46 changes: 6 additions & 40 deletions screens/Connected.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { createMaterialTopTabNavigator } from "@react-navigation/material-top-tabs";
import { MaterialTopTabNavigationHelpers } from "@react-navigation/material-top-tabs/lib/typescript/src/types";
import {
CLIENT_PACKET_TYPE,
PrintJSONPacket,
SERVER_PACKET_TYPE,
ServerPacket,
} from "archipelago.js";
import { PrintJSONPacket, SERVER_PACKET_TYPE } from "archipelago.js";
import React, { useContext, useEffect, useRef, useState } from "react";
import { Alert, BackHandler } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
Expand All @@ -19,8 +14,8 @@ function Placeholder() {
return <></>;
}

/**Send a sync to the server, if the connection hasn't been verified in this many seconds */
const ALLOWED_TIME_BETWEEN_PACKETS = 120;
/**Check connection status every this many seconds */
const ALLOWED_TIME_BETWEEN_PACKETS = 30;

const minTime = ALLOWED_TIME_BETWEEN_PACKETS * 1000;

Expand All @@ -33,8 +28,6 @@ export default function Connected({

const [messages, setMessages] = useState<messages>([]);
const insets = useSafeAreaInsets();
const lastConnectionRef = useRef(new Date().getTime());
const triedSyncRef = useRef(false);
const retryRef = useRef<ReturnType<typeof setInterval> | null>(null);

/**
Expand Down Expand Up @@ -93,7 +86,6 @@ export default function Connected({

const handleDisconnect = async () => {
client.removeListener(SERVER_PACKET_TYPE.PRINT_JSON, handleMessages);
client.removeListener("PacketReceived", handleConnectionStatus);
console.log("disconnecting...");
client.disconnect();
setMessages([]);
Expand All @@ -104,34 +96,10 @@ export default function Connected({
navigation.navigate("connect");
};

const handleConnectionStatus = () => {
lastConnectionRef.current = new Date().getTime();
triedSyncRef.current = false;
};

const checkConnected = (lastConnection: number) => {
if (lastConnection + minTime < new Date().getTime()) {
console.log(
"connection not verified in ",
ALLOWED_TIME_BETWEEN_PACKETS,
"seconds. Sending Sync package...",
);
triedSyncRef.current = true;
client.send({ cmd: CLIENT_PACKET_TYPE.SYNC });
}
};

const handleReconnection = async () => {
const info = connectionInfoRef?.current;
const checkConnection = triedSyncRef.current;
const lastConnection = lastConnectionRef.current;

console.log(
lastConnection + minTime < new Date().getTime(),
checkConnection,
);
checkConnected(lastConnection);
if (lastConnection + minTime < new Date().getTime() && checkConnection) {
console.log(client.status);
if (client.status === "Disconnected") {
try {
if (info) {
console.log("trying to connect with info", info);
Expand Down Expand Up @@ -163,7 +131,6 @@ export default function Connected({

useEffect(() => {
client.addListener(SERVER_PACKET_TYPE.PRINT_JSON, handleMessages);
client.addListener("PacketReceived", handleConnectionStatus);

const backAction = () => {
Alert.alert(
Expand Down Expand Up @@ -193,13 +160,12 @@ export default function Connected({

const retry = setInterval(() => {
handleReconnection();
}, 10000);
}, minTime);
retryRef.current = retry;

return () => {
console.log("Connected.tsx useEffect cleanup is running...");
client.removeListener(SERVER_PACKET_TYPE.PRINT_JSON, handleMessages);
client.removeListener("PacketReceived", handleConnectionStatus);
backHandler.remove();
clearInterval(retry);
};
Expand Down
Loading

0 comments on commit 34c0063

Please sign in to comment.