-
Notifications
You must be signed in to change notification settings - Fork 96
V3 #72
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
base: master
Are you sure you want to change the base?
Conversation
started TS added git ignore clean up removed node_modules and ignored
…removed devDep :|
|
|
||
| if ( | ||
| (typeof pollingConfig === 'object' && pollingConfig.enabled === true) || | ||
| (needsPolling === true && typeof pollingConfig === 'object') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case pollingObject.enabled === false, this would return an incorrect object
{
enabled: false,
url: 'https://ipv4.icanhazip.com/',
timeout: 5000,
interval: 5000,
}
Change to (needsPolling === true && typeof pollingConfig === 'object' && pollingConfig.enabled === undefined)
| }; | ||
|
|
||
| export const useOnlineEffect: UseOnlineEffectType = ( | ||
| callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better than using a callback would be use useState.
const [isOnline, setIsOnline] = useState(true);
const goOnline = () => {
setIsOnline(true);
};
const goOffline = () => {
setIsOnline(false);
};
...
setInterval(() => {
ping(pingConfig).then(setIsOnline);
}, interval);
...
return isOnline;A user can then just use it like:
const isOnline = useOnlineEffect();
useEffect(() => {
if (isOnline) {
console.debug("we're online");
} else {
console.warn("we're offline");
}
}, [isOnline])There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If callback is still wanted, you could add
if (callback) {
useEffect(() => {
callback(isOnline);
}, [isOnline])
}to have both options.
src/index.tsx
Outdated
| if ((mustPoll || enabled) && 'url' in pingConfig) { | ||
| const { url, timeout, interval } = pingConfig; | ||
|
|
||
| setInterval(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assign to intervalId
| window.removeEventListener('online', goOnline); | ||
| window.removeEventListener('offline', goOffline); | ||
|
|
||
| if (mustPoll || enabled) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use if (intervalId) instead. It will only be set in this case.
src/index.tsx
Outdated
| let intervalId: number | undefined; | ||
|
|
||
| // if we are polling for online status, set up the setInterval. | ||
| if ((mustPoll || enabled) && 'url' in pingConfig) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't getPollingConfigs have taken care to set enabled correctly.
Here you are overriding the choice of the user.
IMO enabled should suffice.
src/index.tsx
Outdated
| ping({ | ||
| url, | ||
| timeout, | ||
| }).then(online => (online ? goOnline() : goOffline())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be simplified to .then(callback)
|
Thank you so much for your work on this. 🙌 |
* Created hashed object that we can store to the window to make sure that duplicates are not being ran. * Added the unmount delete from the window.
Implementation of useOnlineEffect. No tests yet to confirm on the correct path lol.