Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket issue #1115

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Socket issue #1115

wants to merge 4 commits into from

Conversation

BIhab
Copy link
Contributor

@BIhab BIhab commented May 2, 2023

No description provided.

Comment on lines 101 to 112
try {
const [devicesResponse, positionsResponse] = await Promise.all([fetch('/api/devices'), fetch('/api/positions')]);
if (devicesResponse.ok) {
dispatch(devicesActions.update(await devicesResponse.json()));
}
if (positionsResponse.ok) {
dispatch(sessionActions.updatePositions(await positionsResponse.json()));
}
connectSocket();
} catch (error) {
// ignore errors
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to duplicate this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove the duplication


useEffect(() => {
if (authenticated) {
setTimeout(keepAlive, 3000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this is called only once. Is that by design?

Copy link
Contributor Author

@BIhab BIhab May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the value for the useEffect change?

useEffect(() => {
    if (authenticated) {
      setTimeout(keepAlive, 3000);
    }
  }, [lastUpdate]);

lastUpdate change every time a data in sent via the socket.onmessage, and the code inside the useEffect is re-executed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see... but how do you cancel the old timeout?

Copy link
Contributor Author

@BIhab BIhab May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about doing that?

const [timeOutId, setTimeOutId] = useState();
...
useEffect(() => {
    if (authenticated) {
      clearTimeout(timeOutId);
      setTimeOutId(setTimeout(keepAlive, 3000));
    }
  }, [lastUpdate]);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should work.

The only minor nit is use "timeout" as a single word, not "timeOut".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I push the new changes, I'll give it a try on some devices and came back with the result

Comment on lines 39 to 43
const resetCounterKeepAlive = () => {
setTimeKeepAlive(new Date());
};

const isConnected = () => Math.abs(new Date() - timeKeepAlive) < 10000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can probably be inlined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

Comment on lines 167 to 171
ContentProps={{
sx: {
background: 'red',
},
}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move out all unrelated changes like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad

@@ -26,11 +29,19 @@ const SocketController = () => {
const [events, setEvents] = useState([]);
const [notifications, setNotifications] = useState([]);

const [timeKeepAlive, setTimeKeepAlive] = useState();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably rename this to something like lastUpdate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that

@BIhab
Copy link
Contributor Author

BIhab commented May 2, 2023

The main of the solution is it correct? I tried it on iOs and Android, didn't notice an issue on android, but on iOS the problem still persist of socket not updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants