Fixed boundToMercureHub HOC so that it clears updates intervals when unmounted

This commit is contained in:
Alejandro Celaya
2020-09-12 11:55:49 +02:00
parent fa074f91be
commit cfb165d240
2 changed files with 13 additions and 5 deletions

View File

@@ -22,12 +22,18 @@ export function boundToMercureHub<T = {}>(
useEffect(() => {
const onMessage = (visit: CreateVisit) => interval ? pendingUpdates.add(visit) : createNewVisits([ visit ]);
interval && setInterval(() => {
bindToMercureTopic(mercureInfo, getTopicForProps(props), onMessage, loadMercureInfo);
if (!interval) {
return undefined;
}
const timer = setInterval(() => {
createNewVisits([ ...pendingUpdates ]);
pendingUpdates.clear();
}, interval * 1000 * 60);
bindToMercureTopic(mercureInfo, getTopicForProps(props), onMessage, loadMercureInfo);
return () => clearInterval(timer);
}, [ mercureInfo ]);
return <WrappedComponent {...props} />;