[Refactoring] useBackgroundMode: Use signals
This commit is contained in:
parent
aaf19a4405
commit
02ada70d43
@ -1,26 +1,21 @@
|
||||
import { useEffect } from '../../lib/teact/teact';
|
||||
import { useEffect } from '@teact';
|
||||
|
||||
import { createCallbackManager } from '../../util/callbacks';
|
||||
import { createSignal } from '../../util/signals';
|
||||
import useLastCallback from '../useLastCallback';
|
||||
|
||||
const blurCallbacks = createCallbackManager();
|
||||
const focusCallbacks = createCallbackManager();
|
||||
const [getIsInBackgroundLocal, setIsInBackground] = createSignal(!document.hasFocus());
|
||||
export const getIsInBackground = getIsInBackgroundLocal;
|
||||
|
||||
let isFocused = document.hasFocus();
|
||||
function handleBlur() {
|
||||
setIsInBackground(true);
|
||||
}
|
||||
|
||||
window.addEventListener('blur', () => {
|
||||
if (!isFocused) {
|
||||
return;
|
||||
}
|
||||
function handleFocus() {
|
||||
setIsInBackground(false);
|
||||
}
|
||||
|
||||
isFocused = false;
|
||||
blurCallbacks.runCallbacks();
|
||||
});
|
||||
|
||||
window.addEventListener('focus', () => {
|
||||
isFocused = true;
|
||||
focusCallbacks.runCallbacks();
|
||||
});
|
||||
window.addEventListener('blur', handleBlur);
|
||||
window.addEventListener('focus', handleFocus);
|
||||
|
||||
export default function useBackgroundMode(
|
||||
onBlur?: AnyToVoidFunction,
|
||||
@ -35,20 +30,20 @@ export default function useBackgroundMode(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!isFocused) {
|
||||
if (getIsInBackground()) {
|
||||
lastOnBlur();
|
||||
}
|
||||
|
||||
blurCallbacks.addCallback(lastOnBlur);
|
||||
focusCallbacks.addCallback(lastOnFocus);
|
||||
|
||||
return () => {
|
||||
focusCallbacks.removeCallback(lastOnFocus);
|
||||
blurCallbacks.removeCallback(lastOnBlur);
|
||||
};
|
||||
return getIsInBackground.subscribe(() => {
|
||||
if (getIsInBackground()) {
|
||||
lastOnBlur();
|
||||
} else {
|
||||
lastOnFocus();
|
||||
}
|
||||
});
|
||||
}, [isDisabled, lastOnBlur, lastOnFocus]);
|
||||
}
|
||||
|
||||
export function isBackgroundModeActive() {
|
||||
return !isFocused;
|
||||
return getIsInBackground();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user