mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-08-19 02:06:26 +00:00
Merge pull request #1344 from Stremio/fix/background-autoplay-focus
Some checks are pending
Build / build (push) Waiting to run
Some checks are pending
Build / build (push) Waiting to run
fix: keep active model subscriptions while window is unfocused
This commit is contained in:
commit
2272aab854
2 changed files with 10 additions and 6 deletions
|
|
@ -6,11 +6,11 @@ const { deepEqual } = require('fast-equals');
|
|||
const intersection = require('lodash.intersection');
|
||||
const { useCore } = require('stremio/core');
|
||||
const { useCoreSuspender } = require('stremio/common/CoreSuspender');
|
||||
const { default: useRouteFocused } = require('stremio/common/useRouteFocused');
|
||||
const { useRouteActive } = require('stremio/common/useRouteFocused');
|
||||
|
||||
const useModelState = ({ action, ...args }) => {
|
||||
const core = useCore();
|
||||
const routeFocused = useRouteFocused();
|
||||
const routeActive = useRouteActive();
|
||||
const mountedRef = React.useRef(false);
|
||||
const [model, timeout, map, deps] = React.useMemo(() => {
|
||||
return [args.model, args.timeout, args.map, args.deps];
|
||||
|
|
@ -53,7 +53,7 @@ const useModelState = ({ action, ...args }) => {
|
|||
}
|
||||
};
|
||||
const onNewStateThrottled = throttle(onNewState, timeout);
|
||||
if (routeFocused) {
|
||||
if (routeActive) {
|
||||
core.on('state', onNewStateThrottled);
|
||||
if (mountedRef.current) {
|
||||
onNewState([model]);
|
||||
|
|
@ -63,7 +63,7 @@ const useModelState = ({ action, ...args }) => {
|
|||
onNewStateThrottled.cancel();
|
||||
core.off('state', onNewStateThrottled);
|
||||
};
|
||||
}, [routeFocused]);
|
||||
}, [routeActive]);
|
||||
React.useEffect(() => {
|
||||
mountedRef.current = true;
|
||||
}, []);
|
||||
|
|
|
|||
|
|
@ -4,8 +4,12 @@ import React from 'react';
|
|||
|
||||
const RouteFocusedContext = React.createContext(true);
|
||||
|
||||
export const useRouteActive = () => {
|
||||
return React.useContext(RouteFocusedContext);
|
||||
};
|
||||
|
||||
const useRouteFocused = () => {
|
||||
const routeFocused = React.useContext(RouteFocusedContext);
|
||||
const routeActive = useRouteActive();
|
||||
const [isFocused, setIsFocused] = React.useState(document.hasFocus());
|
||||
|
||||
React.useEffect(() => {
|
||||
|
|
@ -24,7 +28,7 @@ const useRouteFocused = () => {
|
|||
};
|
||||
}, []);
|
||||
|
||||
return routeFocused && isFocused;
|
||||
return routeActive && isFocused;
|
||||
};
|
||||
|
||||
export const RouteFocusedProvider = RouteFocusedContext.Provider;
|
||||
|
|
|
|||
Loading…
Reference in a new issue