Skip to content

Commit

Permalink
Try closing the event controller in cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansick committed Apr 3, 2024
1 parent 37b46ba commit 48701fd
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ export const TimesSquareHtmlEventsContext = React.createContext();

export default function TimesSquareHtmlEventsProvider({ children }) {
const [htmlEvent, setHtmlEvent] = React.useState(null);
const [abortController, setAbortController] = useState(new AbortController());
const { htmlEventsUrl } = useTimesSquarePage();

React.useEffect(async () => {
const controller = new AbortController();

async function runEffect() {
await fetchEventSource(htmlEventsUrl, {
method: 'GET',
signal: abortController.signal,
onopen(res) {
if (res.ok && res.status === 200) {
console.log('Connection made ', res);
Expand All @@ -41,6 +45,12 @@ export default function TimesSquareHtmlEventsProvider({ children }) {
});
}
runEffect();

return () => {
// Clean up: close the event source connection
controller.abort();
setAbortController(new AbortController());
};
}, [htmlEventsUrl]);

const contextValue = {
Expand Down

0 comments on commit 48701fd

Please sign in to comment.