mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-04 22:38:43 +00:00
Filter stream updates by Share ID
This commit is contained in:
+27
-15
@@ -2,29 +2,37 @@ import { EventEmitter } from 'events';
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
|
|
||||||
const UNSUB_DELAY = 60000; //ms
|
// DEBUG
|
||||||
|
const DEBUG = {
|
||||||
|
showEvents : true,
|
||||||
|
showSubscriberCount : true
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delays
|
||||||
|
const UNSUB_DELAY = 60 * 1000; //ms
|
||||||
|
const REPORT_DELAY = 5 * 1000; //ms
|
||||||
|
|
||||||
|
|
||||||
const Stream = new EventEmitter;
|
const Stream = new EventEmitter;
|
||||||
|
|
||||||
// Create array of stream subscribers
|
// Create array of stream subscribers
|
||||||
let subscribers = [];
|
let subscribers = [];
|
||||||
|
|
||||||
const debounceUnsub = _.debounce((id)=>{ unsubscribe(id); }, UNSUB_DELAY, { leading: false, trailing: true });
|
|
||||||
|
|
||||||
// Listener functions
|
// Listener functions
|
||||||
const subscribe = function(shareId, res){
|
const subscribe = function(shareId, res){
|
||||||
|
const id = nanoid(24); // NanoID is assumed globally unique;
|
||||||
const subscriber = {
|
const subscriber = {
|
||||||
id : nanoid(), // NanoID is assumed globally unique
|
id,
|
||||||
shareId,
|
shareId,
|
||||||
stream : res,
|
stream : res,
|
||||||
time : new Date
|
time : new Date,
|
||||||
|
unsubTimer : setTimeout(()=>{ unsubscribe(id); }, UNSUB_DELAY)
|
||||||
};
|
};
|
||||||
|
|
||||||
subscribers.push(subscriber);
|
subscribers.push(subscriber);
|
||||||
|
|
||||||
Stream.emit('sendUpdate', 'subscribe', { id: subscriber.id, shareId: subscriber.shareId, time: subscriber.time });
|
Stream.emit('sendUpdate', 'subscribe', { id: subscriber.id, shareId: subscriber.shareId, time: subscriber.time });
|
||||||
|
|
||||||
debounceUnsub(subscriber.id);
|
|
||||||
return subscriber.id;
|
return subscriber.id;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -40,22 +48,26 @@ const unsubscribe = function(id){
|
|||||||
|
|
||||||
// Create global sendUpdate listener
|
// Create global sendUpdate listener
|
||||||
Stream.on('sendUpdate', (event, data)=>{
|
Stream.on('sendUpdate', (event, data)=>{
|
||||||
console.log('Event:', event, '\nData:', data);
|
if(DEBUG.showEvents) console.log('Event:', event, '\nData:', data);
|
||||||
subscribers
|
subscribers
|
||||||
.filter((sub)=>{return data.shareId == sub.shareId; })
|
.filter((sub)=>{return data.shareId == sub.shareId; })
|
||||||
.forEach((sub)=>{
|
.forEach((sub)=>{
|
||||||
sub?.stream?.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
if(event == 'brewUpdated'){
|
||||||
|
// Reset the unsubscription timer
|
||||||
|
clearTimeout(sub.unsubTimer);
|
||||||
|
sub.unsubTimer = setTimeout(()=>{ unsubscribe(sub.id); }, UNSUB_DELAY);
|
||||||
|
}
|
||||||
|
|
||||||
// invoke debounced unsub to delay execution
|
sub?.stream?.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
||||||
debounceUnsub(sub.id);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// DEBUG: Report subscriber count periodically
|
// DEBUG: Report subscriber count periodically
|
||||||
const REPORT_DELAY = 1000; //ms
|
if(DEBUG.showSubscriberCount){
|
||||||
const reportIntervalId = setInterval(()=>{
|
const reportIntervalId = setInterval(()=>{
|
||||||
console.log('Subscriber count:', subscribers.length);
|
console.log('Subscriber count:', subscribers.length, ' @ ', new Date);
|
||||||
}, REPORT_DELAY);
|
}, REPORT_DELAY);
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
emit : function(event) {return Stream.emit(event, ...([...arguments].slice(1)));}, // Arguments doesn't work for arrow functions
|
emit : function(event) {return Stream.emit(event, ...([...arguments].slice(1)));}, // Arguments doesn't work for arrow functions
|
||||||
|
|||||||
Reference in New Issue
Block a user