Created custom react hook that binds to mercure topic

This commit is contained in:
Alejandro Celaya
2020-06-06 09:29:43 +02:00
parent c46d5187c1
commit 52c56f7918
5 changed files with 14 additions and 21 deletions

View File

@@ -1,3 +1,4 @@
import { useEffect } from 'react';
import { EventSourcePolyfill as EventSource } from 'event-source-polyfill';
export const bindToMercureTopic = (mercureInfo, topic, onMessage, onTokenExpired) => () => {
@@ -21,3 +22,7 @@ export const bindToMercureTopic = (mercureInfo, topic, onMessage, onTokenExpired
return () => es.close();
};
export const useMercureTopicBinding = (mercureInfo, topic, onMessage, onTokenExpired) => {
useEffect(bindToMercureTopic(mercureInfo, topic, onMessage, onTokenExpired), [ mercureInfo ]);
};