Add app states

This commit is contained in:
Bojan Mojsilovic 2024-03-13 12:30:10 +01:00
parent 44f67d2551
commit 3175a76cf3
2 changed files with 27 additions and 2 deletions

View File

@ -10,10 +10,12 @@ import {
export type AppContextStore = {
isInactive: boolean,
appState: 'sleep' | 'waking' | 'woke',
}
const initialData = {
const initialData: AppContextStore = {
isInactive: false,
appState: 'woke',
};
export const AppContext = createContext<AppContextStore>();
@ -38,10 +40,33 @@ export const AppProvider = (props: { children: JSXElement }) => {
onMount(() => {
document.addEventListener('mousemove', monitorActivity);
document.addEventListener('scroll', monitorActivity);
document.addEventListener('keydown', monitorActivity);
});
onCleanup(() => {
document.removeEventListener('mousemove', monitorActivity);
document.removeEventListener('scroll', monitorActivity);
document.removeEventListener('keydown', monitorActivity);
});
let wakingTimeout = 0;
createEffect(() => {
if (store.isInactive) {
updateStore('appState', () => 'sleep');
clearTimeout(wakingTimeout);
}
else {
// Set this state in order to make sure that we reload page
// when user requests future notes because we didn't fetch them yet
updateStore('appState', () => 'waking');
// Give time for future notes fetching to fire before changing state
wakingTimeout = setTimeout(() => {
updateStore('appState', () => 'woke');
}, 36_000);
}
});
// STORES ---------------------------------------

View File

@ -110,7 +110,7 @@ const Home: Component = () => {
});
const loadNewContent = () => {
if (newNotesCount() > 100) {
if (newNotesCount() > 100 || app?.appState === 'waking') {
location.reload();
return;
}