-
Notifications
You must be signed in to change notification settings - Fork 994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #37454 - Allow js to be loaded late for react_component #10167
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
import PropTypes from 'prop-types'; | ||
import { useState, useEffect } from 'react'; | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import store from '../redux'; | ||
import componentRegistry from '../components/componentRegistry'; | ||
|
||
// Mounts a component after all plugins have been imported to make sure that all plugins are available to the component | ||
export const AwaitedMount = ({ component, data, flattenData }) => { | ||
const [mounted, setMounted] = useState(false); | ||
const [mountedComponent, setMountedComponent] = useState(null); | ||
const [allPluginsImported, setAllPluginsImported] = useState( | ||
window.allJsLoaded | ||
); | ||
async function mountComponent() { | ||
const mountComponent = useCallback(async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this an async function? I don't see any await |
||
if (componentRegistry.registry[component]) { | ||
setMounted(true); | ||
setMountedComponent( | ||
|
@@ -20,32 +17,22 @@ export const AwaitedMount = ({ component, data, flattenData }) => { | |
flattenData, | ||
}) | ||
); | ||
} else if (allPluginsImported) { | ||
const awaitedComponent = componentRegistry.markup(component, { | ||
data, | ||
store, | ||
flattenData, | ||
}); | ||
setMounted(true); | ||
setMountedComponent(awaitedComponent); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.debug( | ||
`Component not found: ${component}. The script for the component might not have been loaded yet.` | ||
); | ||
} | ||
} | ||
const updateAllPluginsImported = e => { | ||
setAllPluginsImported(true); | ||
}; | ||
useEffect(() => { | ||
document.addEventListener('loadJS', updateAllPluginsImported); | ||
return () => window.removeEventListener('loadJS', updateAllPluginsImported); | ||
}, []); | ||
}, [component, data, flattenData]); | ||
useEffect(() => { | ||
if (!mounted) mountComponent(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [allPluginsImported]); | ||
document.addEventListener('loadPlugin', mountComponent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how this is listening for all plugins' JS to be loaded and not just a single random plugin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its listening to each plugin load, and then checking if the component is registered. if it is it loads it. |
||
return () => { | ||
window.removeEventListener('loadPlugin', mountComponent); | ||
}; | ||
}, [mountComponent]); | ||
useEffect(() => { | ||
// Update the component if the data (props) change | ||
if (allPluginsImported) mountComponent(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [data]); | ||
mountComponent(); | ||
}, [mountComponent]); | ||
return mounted ? mountedComponent : null; | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really what it does? I see it's listening for the 'loadPlugin' event now, instead of 'loadJs'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loadPlugin might happen after loadjs. example: user clicks a button, a tab appears, plugin has Deface::Override that calls loadPlugin