mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-04 22:38:43 +00:00
Add unsubscribe call on SharePage unload
This commit is contained in:
@@ -54,7 +54,15 @@ const SharePage = (props)=>{
|
|||||||
fetchThemeBundle(undefined, setThemeBundle, currentBrew.renderer, currentBrew.theme);
|
fetchThemeBundle(undefined, setThemeBundle, currentBrew.renderer, currentBrew.theme);
|
||||||
|
|
||||||
// listen for changes in the brew version
|
// listen for changes in the brew version
|
||||||
const eventSource = new EventSource(`/stream/${props.brew.shareId}`);
|
const eventSource = new EventSource(`/api/stream/${props.brew.shareId}`);
|
||||||
|
|
||||||
|
let subId = '';
|
||||||
|
eventSource.addEventListener('subscribe', (evt)=>{
|
||||||
|
const messageData = JSON.parse(evt.data);
|
||||||
|
|
||||||
|
subId = messageData.id;
|
||||||
|
});
|
||||||
|
|
||||||
eventSource.addEventListener('brewUpdated', (evt)=>{
|
eventSource.addEventListener('brewUpdated', (evt)=>{
|
||||||
const messageData = JSON.parse(evt.data);
|
const messageData = JSON.parse(evt.data);
|
||||||
|
|
||||||
@@ -64,7 +72,11 @@ const SharePage = (props)=>{
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return ()=>{
|
window.addEventListener('beforeunload', async ()=>{
|
||||||
|
await request.get(`/api/stream/unsubscribe/${subId}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
return async ()=>{
|
||||||
document.removeEventListener('keydown', handleControlKeys);
|
document.removeEventListener('keydown', handleControlKeys);
|
||||||
eventSource.close();
|
eventSource.close();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user