mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-04 18:18:46 +00:00
Shit to res.close listener
This commit is contained in:
@@ -54,39 +54,29 @@ const SharePage = (props)=>{
|
||||
document.addEventListener('keydown', handleControlKeys);
|
||||
fetchThemeBundle(undefined, setThemeBundle, currentBrew.renderer, currentBrew.theme);
|
||||
|
||||
// generate unique fingerprint
|
||||
const clientFingerprint = nanoid(24);
|
||||
let eventSource;
|
||||
// Start a stream if the brew has been updated in the last month
|
||||
if(Date.parse(props.brew?.updatedAt) > (new Date).setDate((new Date).getDate() - 30)){
|
||||
// listen for changes in the brew version
|
||||
eventSource = new EventSource(`/api/stream/${props.brew.shareId}`);
|
||||
|
||||
// listen for changes in the brew version
|
||||
const eventSource = new EventSource(`/api/stream/${props.brew.shareId}?client=${clientFingerprint}`);
|
||||
eventSource.addEventListener('brewUpdated', (evt)=>{
|
||||
const messageData = JSON.parse(evt.data);
|
||||
|
||||
let subId = '';
|
||||
eventSource.addEventListener('subscribe', (evt)=>{
|
||||
const messageData = JSON.parse(evt.data);
|
||||
if(messageData.version != currentBrew.version) {
|
||||
console.log('should fetch brew');
|
||||
fetchUpdatedBrew();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if(messageData.client == clientFingerprint){
|
||||
subId = messageData.id;
|
||||
}
|
||||
});
|
||||
|
||||
eventSource.addEventListener('brewUpdated', (evt)=>{
|
||||
const messageData = JSON.parse(evt.data);
|
||||
|
||||
if(messageData.version != currentBrew.version) {
|
||||
console.log('should fetch brew');
|
||||
fetchUpdatedBrew();
|
||||
}
|
||||
});
|
||||
|
||||
async function unsub(){
|
||||
await request.get(`/api/stream/unsubscribe/${subId}`);
|
||||
function unsub(){
|
||||
eventSource?.close();
|
||||
};
|
||||
window.addEventListener('beforeunload', unsub);
|
||||
|
||||
return async ()=>{
|
||||
document.removeEventListener('keydown', handleControlKeys);
|
||||
window.removeEventListener('beforeunload', unsub);
|
||||
eventSource.close();
|
||||
unsub();
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
||||
@@ -11,9 +11,7 @@ const DEBUG = {
|
||||
};
|
||||
|
||||
// Delays
|
||||
const UNSUB_DELAY = 60 * 1000; //ms
|
||||
const REFRESH_DELAY = 60 * 1000; //ms
|
||||
const REPORT_DELAY = 5 * 1000; //ms
|
||||
const REPORT_DELAY = config.get('REPORT_DELAY') || 30 * 60 * 1000; //ms
|
||||
|
||||
|
||||
const Stream = new EventEmitter;
|
||||
@@ -22,19 +20,17 @@ const Stream = new EventEmitter;
|
||||
let subscribers = [];
|
||||
|
||||
// Listener functions
|
||||
const subscribe = function(shareId, res, clientFingerprint){
|
||||
const subscribe = function(shareId, res){
|
||||
const id = nanoid(24); // NanoID is assumed globally unique;
|
||||
const subscriber = {
|
||||
id,
|
||||
shareId,
|
||||
stream : res,
|
||||
time : new Date,
|
||||
unsubTimer : setTimeout(()=>{ unsubscribe(id); }, UNSUB_DELAY)
|
||||
stream : res
|
||||
};
|
||||
|
||||
subscribers.push(subscriber);
|
||||
|
||||
Stream.emit('sendUpdate', 'subscribe', { id: subscriber.id, shareId: subscriber.shareId, client: clientFingerprint, time: subscriber.time });
|
||||
Stream.emit('sendUpdate', 'subscribe', { id: subscriber.id, shareId: subscriber.shareId });
|
||||
|
||||
return subscriber.id;
|
||||
};
|
||||
@@ -44,7 +40,8 @@ const unsubscribe = function(id){
|
||||
.filter((sub)=>{return sub.id == id;})
|
||||
.forEach((sub)=>{
|
||||
clearTimeout(sub.unsubTimer);
|
||||
Stream.emit('sendUpdate', 'unsubscribe', { id: sub.id, shareId: sub.shareId, time: new Date });
|
||||
Stream.emit('sendUpdate', 'unsubscribe', { id: sub.id, shareId: sub.shareId });
|
||||
sub?.stream?.status(200).end();
|
||||
});
|
||||
|
||||
subscribers = subscribers.filter((sub)=>{ return sub.id != id; });
|
||||
@@ -59,12 +56,6 @@ Stream.on('sendUpdate', (event, data)=>{
|
||||
return data.shareId == sub.shareId;
|
||||
})
|
||||
.forEach((sub)=>{
|
||||
if(event == 'brewUpdated'){
|
||||
// Reset the unsubscription timer
|
||||
clearTimeout(sub.unsubTimer);
|
||||
sub.unsubTimer = setTimeout(()=>{ unsubscribe(sub.id); }, REFRESH_DELAY);
|
||||
}
|
||||
|
||||
sub?.stream?.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,13 +12,12 @@ app.get('/api/stream/:id', (req, res)=>{
|
||||
'Connection' : 'keep-alive',
|
||||
'Content-Encoding' : 'none'
|
||||
});
|
||||
;
|
||||
Stream.subscribe(req.params.id, res, req.query?.client);
|
||||
});
|
||||
|
||||
app.get('/api/stream/unsubscribe/:id', (req, res)=>{
|
||||
Stream.unsubscribe(req.params?.id);
|
||||
res.status(200).send();
|
||||
const subId = Stream.subscribe(req.params.id, res);
|
||||
|
||||
res.on('close', ()=>{
|
||||
Stream.unsubscribe(subId);
|
||||
});
|
||||
});
|
||||
|
||||
export default app;
|
||||
Reference in New Issue
Block a user