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

detect offline client #129

Merged
merged 1 commit into from
Apr 2, 2024

Conversation

Blazing-Mike
Copy link
Contributor

Context (Problem, Motivation, Solution)

Link related issues!
i ran into git conflict so i had to pull. open another PR
#126 #35

Describe Your Changes

Checklist

  • I have performed a self-review of my code
  • I ran make check and fixed resulting issues
  • I ran the relevant tests and they all pass
  • I wrote tests for my new features, or added regression tests for the bug I fixed

Testing

I implemented a modal to display when our app is offline. so how did i test this, i tested this by throttling the network in my devtools and also disconneCTING my device from wifi and this modal appears on the index.tsx and play.tsx page. when i toggle back or reconnect, the modal disaappears

CC: @norswap

@@ -37,6 +38,8 @@ const Home: FablePage = ({ isHydrated }) => {
const isRightNetwork = !notConnected && chainSupported
const isWrongNetwork = !notConnected && !chainSupported

useOfflineCheck();

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I forgot this one is the last review, could you add this to _app.tsx instead, so it applies to all pages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

});
return response.ok;
} catch (error) {
throw error; // Rethrow for retries
Copy link
Member

Choose a reason for hiding this comment

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

This is redundant, you could remove the whole try-catch statement and it would work the same.

}, retryDelay);
} else {
setIsOffline(true);
toast.error("App is offline. Please check your internet connection.");
Copy link
Member

Choose a reason for hiding this comment

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

Add an option object here with { dismissible: false, duration: Infinity } which prevents the toast from closing.

Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we need this at all — can't we just handle the "online" and "offline" events? On "online" do what you do in handleOnline, on "offline" do setIsOffline and the toast. What's the reasoning here? Is there some documentation around the behaviour and reliability of these events?

packages/webapp/src/hooks/useOfflineCheck.ts Outdated Show resolved Hide resolved
.catch(() => {
setIsOffline(true);
toast.error("App is offline. Please check your internet connection.");
});
Copy link
Member

Choose a reason for hiding this comment

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

The toast is triggered many times, which we should fix.

I'm wondering if this isn't the problem, as this is rerun every time useOfflineCheck is rerun, which is every time that the page is rendered.

Another possible source of the issue is that the "offline" event is fired multiple times? Is that the case?

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 think i figured why the toast is rendering multiple times, it is being called both in the performNetworkCheck() function and the handleOffline.

Copy link
Member

Choose a reason for hiding this comment

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

Did you try the codebase and shut down your wifi to see if this works correctly?

function useOfflineCheck(options: any = {}) {
const {
// Optional configuration properties:
threshold = 1000, // Minimum interval between offline checks (ms)
Copy link
Member

Choose a reason for hiding this comment

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

This is never used.

performNetworkCheck()
.then(() => handleOnline()) // online
.catch(() => {
setRetries(retries + 1); // Retry if error occurs
Copy link
Member

Choose a reason for hiding this comment

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

I'm quite dubious on the retry logic here, as we don't actually enqueue any retry! The only way retries can be attempted is if the "offline" event fire multiple times. Is that the intention here? But if so, the retryDelay variable is a little bit misleading, as it simply adds to the delay between multiple firings of "offline".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for the retry logic, we only want to show our app is offline if `retries === maxRetries. so the reason for that.

@Blazing-Mike
Copy link
Contributor Author

@norswap please i need a review on this PR.

@norswap
Copy link
Member

norswap commented Mar 5, 2024

Hey, time is a bit tight right now, but I'll get to it soon. Don't worry, this'll be considered for ODHack.

@Blazing-Mike
Copy link
Contributor Author

I understand. Thank you.

}
};

window.addEventListener("online", handleOffline);
Copy link
Member

Choose a reason for hiding this comment

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

It seems unecessary to recheck the network state when receiving "online".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is checked to dismiss the toast.

});
}, retryDelay);
}
};
Copy link
Member

Choose a reason for hiding this comment

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

The retry logic still doesn't make sense.

  1. handleOffline is called
  2. performNetworkCheck is called
  3. in case of failure (catch) the retry count is incremented by 1
  4. ??? — nothing in the code makes performNetworkCheck or handleOffline be called again

Copy link
Member

Choose a reason for hiding this comment

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

Do we really need retry logic in the first place? We could just assume offline after a single failure.

The risk of assuming after single failure is that it's a fluke and the offline event and subsequent online events are never sent, in which case we stay in offline mode forever.

Do we even need performNetworkCheck at all? Can't we only rely on online and offline?

It's possible that these are not always reliable and it's recommend to perform explicit checks, but I'd like to hear your reasoning about this + some documentation showing that indeed online / offline is not to be relied on.

Copy link
Contributor Author

@Blazing-Mike Blazing-Mike Mar 6, 2024

Choose a reason for hiding this comment

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

Hmmmm, it might be complicating things. The first option I opted for was using the online and offline , but seeing some documentation that suggests online/offline might not be relied on that's why I opted for doing a network check and retrying the request if it fails the first time.

In my opinion, the unreliability of theonline / offline event might be rare and we can tackle it with listening for both online and offline. I implemented a version that uses the online / offline and they both work. I will push the changes for review.

Cc: @norswap

.catch(() => {
setIsOffline(true);
toast.error("App is offline. Please check your internet connection.");
});
Copy link
Member

Choose a reason for hiding this comment

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

Did you try the codebase and shut down your wifi to see if this works correctly?

@norswap
Copy link
Member

norswap commented Mar 12, 2024

Hey, any updates on this?

@Blazing-Mike
Copy link
Contributor Author

Yess. I have fixed the implementation to just rely on offline and online events. And it works when I switched off WiFi and turned it on.

@Blazing-Mike
Copy link
Contributor Author

cc @norswap

@norswap
Copy link
Member

norswap commented Mar 15, 2024

Sorry, I missed that extra commit. Looks good, could you rebase on top of master and make sure make check passes? (You'll want to run make format first.)

@Blazing-Mike
Copy link
Contributor Author

Sorry, I missed that extra commit. Looks good, could you rebase on top of master and make sure make check passes? (You'll want to run make format first.)

Done ✅. make check passes.

Cc: @norswap

@Blazing-Mike
Copy link
Contributor Author

Hi norswap.Just reminding you of this PR, make check have passed.

@norswap
Copy link
Member

norswap commented Mar 20, 2024

Hey, I see you merged instead of rebasing, do you think you could get that fixed? If not, it's okay, just let me know and I'll perform the rebase myself :)

@Blazing-Mike
Copy link
Contributor Author

Blazing-Mike commented Mar 20, 2024

I am running into some issues with it. could you help please? thank you.

@Blazing-Mike
Copy link
Contributor Author

it's me again 🙂...just wanna check if the or is merged. Have a productive week ahead.

Cc: @norswap

@norswap
Copy link
Member

norswap commented Mar 26, 2024

Hey, I'm unfortunately super buy and hadn't time to deal with this, but I'll get it sorted!

Copy link
Member

@norswap norswap left a comment

Choose a reason for hiding this comment

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

Let's go! 🚀

@norswap norswap merged commit a037159 into 0xFableOrg:master Apr 2, 2024
1 check passed
@Blazing-Mike
Copy link
Contributor Author

finally 😃!!I feel so elated looking forward to contributing more to this project.

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