Add cleanup function to registering the message listener

This cleanup function would never be called in normal operation, but if
we do decide to add a dependency to this `useEffect`, this will ensure
that only one listener is registered at a time.
This commit is contained in:
Koen Vlaswinkel
2022-10-11 16:26:42 +02:00
parent d18e3dd40e
commit ebba9949a8

View File

@@ -65,7 +65,7 @@ export function VariantAnalysis({
const [repoResults, setRepoResults] = useState<VariantAnalysisScannedRepositoryResult[]>(initialRepoResults);
useEffect(() => {
window.addEventListener('message', (evt: MessageEvent) => {
const listener = (evt: MessageEvent) => {
if (evt.origin === window.origin) {
const msg: ToVariantAnalysisMessage = evt.data;
if (msg.t === 'setVariantAnalysis') {
@@ -89,7 +89,12 @@ export function VariantAnalysis({
const origin = evt.origin.replace(/\n|\r/g, '');
console.error(`Invalid event origin ${origin}`);
}
});
};
window.addEventListener('message', listener);
return () => {
window.removeEventListener('message', listener);
};
}, []);
if (variantAnalysis?.actionsWorkflowRunId === undefined) {