mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-21 03:22:11 +00:00
delay render of nested suspenders
This commit is contained in:
parent
b20e197baf
commit
7632d39e1d
1 changed files with 15 additions and 7 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
const React = require('react');
|
const React = require('react');
|
||||||
const { useServices } = require('stremio/services');
|
const { useServices } = require('stremio/services');
|
||||||
|
|
||||||
const CoreSuspenderContext = React.createContext();
|
const CoreSuspenderContext = React.createContext(null);
|
||||||
|
|
||||||
CoreSuspenderContext.displayName = 'CoreSuspenderContext';
|
CoreSuspenderContext.displayName = 'CoreSuspenderContext';
|
||||||
|
|
||||||
|
|
@ -31,9 +31,15 @@ function wrapPromise(promise) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const useCoreSuspender = () => {
|
||||||
|
return React.useContext(CoreSuspenderContext);
|
||||||
|
};
|
||||||
|
|
||||||
const withCoreSuspender = (Component, Fallback = () => { }) => {
|
const withCoreSuspender = (Component, Fallback = () => { }) => {
|
||||||
return function withCoreSuspender(props) {
|
return function withCoreSuspender(props) {
|
||||||
const { core } = useServices();
|
const { core } = useServices();
|
||||||
|
const parentSuspender = useCoreSuspender();
|
||||||
|
const [render, setRender] = React.useState(parentSuspender === null);
|
||||||
const statesRef = React.useRef({});
|
const statesRef = React.useRef({});
|
||||||
const streamsRef = React.useRef({});
|
const streamsRef = React.useRef({});
|
||||||
const getState = React.useCallback((model) => {
|
const getState = React.useCallback((model) => {
|
||||||
|
|
@ -50,18 +56,20 @@ const withCoreSuspender = (Component, Fallback = () => { }) => {
|
||||||
|
|
||||||
return streamsRef.current[stream].read();
|
return streamsRef.current[stream].read();
|
||||||
}, []);
|
}, []);
|
||||||
return (
|
React.useLayoutEffect(() => {
|
||||||
|
if (!render) {
|
||||||
|
setRender(true);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
return render ?
|
||||||
<React.Suspense fallback={<Fallback {...props} />}>
|
<React.Suspense fallback={<Fallback {...props} />}>
|
||||||
<CoreSuspenderContext.Provider value={{ getState, decodeStream }}>
|
<CoreSuspenderContext.Provider value={{ getState, decodeStream }}>
|
||||||
<Component {...props} />
|
<Component {...props} />
|
||||||
</CoreSuspenderContext.Provider>
|
</CoreSuspenderContext.Provider>
|
||||||
</React.Suspense>
|
</React.Suspense>
|
||||||
);
|
:
|
||||||
|
null;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const useCoreSuspender = () => {
|
|
||||||
return React.useContext(CoreSuspenderContext);
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = { withCoreSuspender, useCoreSuspender };
|
module.exports = { withCoreSuspender, useCoreSuspender };
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue