mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-20 23:12:13 +00:00
remove onNewState and mapWithCtx from useModelState
This commit is contained in:
parent
43ecfbd6c2
commit
2dd5812d2f
1 changed files with 3 additions and 19 deletions
|
|
@ -6,7 +6,7 @@ const { useRouteFocused } = require('stremio-router');
|
||||||
const { useServices } = require('stremio/services');
|
const { useServices } = require('stremio/services');
|
||||||
const useDeepEqualState = require('stremio/common/useDeepEqualState');
|
const useDeepEqualState = require('stremio/common/useDeepEqualState');
|
||||||
|
|
||||||
const useModelState = ({ model, init, action, timeout, onNewState, map, mapWithCtx }) => {
|
const useModelState = ({ model, init, action, timeout, map }) => {
|
||||||
const modelRef = React.useRef(model);
|
const modelRef = React.useRef(model);
|
||||||
const mountedRef = React.useRef(false);
|
const mountedRef = React.useRef(false);
|
||||||
const { core } = useServices();
|
const { core } = useServices();
|
||||||
|
|
@ -22,23 +22,7 @@ const useModelState = ({ model, init, action, timeout, onNewState, map, mapWithC
|
||||||
}, []);
|
}, []);
|
||||||
React.useLayoutEffect(() => {
|
React.useLayoutEffect(() => {
|
||||||
const onNewStateThrottled = throttle(() => {
|
const onNewStateThrottled = throttle(() => {
|
||||||
const state = core.transport.getState(modelRef.current);
|
setState(map(core.transport.getState(modelRef.current)));
|
||||||
if (typeof onNewState === 'function') {
|
|
||||||
const action = onNewState(state);
|
|
||||||
if (action) {
|
|
||||||
core.transport.dispatch(action, modelRef.current);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof mapWithCtx === 'function') {
|
|
||||||
const ctx = core.transport.getState('ctx');
|
|
||||||
setState(mapWithCtx(state, ctx));
|
|
||||||
} else if (typeof map === 'function') {
|
|
||||||
setState(map(state));
|
|
||||||
} else {
|
|
||||||
setState(state);
|
|
||||||
}
|
|
||||||
}, timeout);
|
}, timeout);
|
||||||
if (routeFocused) {
|
if (routeFocused) {
|
||||||
core.transport.on('NewState', onNewStateThrottled);
|
core.transport.on('NewState', onNewStateThrottled);
|
||||||
|
|
@ -50,7 +34,7 @@ const useModelState = ({ model, init, action, timeout, onNewState, map, mapWithC
|
||||||
onNewStateThrottled.cancel();
|
onNewStateThrottled.cancel();
|
||||||
core.transport.off('NewState', onNewStateThrottled);
|
core.transport.off('NewState', onNewStateThrottled);
|
||||||
};
|
};
|
||||||
}, [routeFocused, timeout, onNewState, map, mapWithCtx]);
|
}, [routeFocused, timeout, map]);
|
||||||
React.useLayoutEffect(() => {
|
React.useLayoutEffect(() => {
|
||||||
mountedRef.current = true;
|
mountedRef.current = true;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue