mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-04 22:38:43 +00:00
Incremental commit
This commit is contained in:
+1
-1
@@ -203,7 +203,7 @@ export default async function createApp(vite) {
|
||||
'Content-Encoding' : 'none'
|
||||
});
|
||||
;
|
||||
Stream.subscribe(res);
|
||||
Stream.subscribe(req.params.id, res);
|
||||
});
|
||||
|
||||
|
||||
|
||||
+26
-12
@@ -12,21 +12,27 @@ let subscribers = [];
|
||||
const debounceUnsub = _.debounce((id)=>{ unsubscribe(id); }, UNSUB_DELAY, { leading: false, trailing: true });
|
||||
|
||||
// Listener functions
|
||||
const subscribe = function(res){
|
||||
const id = nanoid();
|
||||
subscribers.push({ id, 'stream': res });
|
||||
const subscribe = function(shareId, res){
|
||||
const subscriber = {
|
||||
id : nanoid(), // NanoID is assumed globally unique
|
||||
shareId,
|
||||
stream : res,
|
||||
time : new Date
|
||||
};
|
||||
|
||||
res.write(`event: subscribe\ndata: { id: ${id} }\n\n`);
|
||||
subscribers.push(subscriber);
|
||||
|
||||
debounceUnsub(id);
|
||||
return id;
|
||||
Stream.emit('sendUpdate', 'subscribe', { id: subscriber.id, shareId: subscriber.shareId, time: subscriber.time });
|
||||
|
||||
debounceUnsub(subscriber.id);
|
||||
return subscriber.id;
|
||||
};
|
||||
|
||||
const unsubscribe = function(id){
|
||||
subscribers
|
||||
.filter((sub)=>{return sub.id == id;})
|
||||
.forEach((sub)=>{
|
||||
sub.stream.write(`event: unsubscribe\ndata: { id: ${id} }\n\n`);
|
||||
Stream.emit('sendUpdate', 'unsubscribe', { id: sub.id, shareId: sub.shareId, time: new Date });
|
||||
});
|
||||
|
||||
subscribers = subscribers.filter((sub)=>{ return sub.id != id; });
|
||||
@@ -35,14 +41,22 @@ const unsubscribe = function(id){
|
||||
// Create global sendUpdate listener
|
||||
Stream.on('sendUpdate', (event, data)=>{
|
||||
console.log('Event:', event, '\nData:', data);
|
||||
subscribers.forEach((sub)=>{
|
||||
sub?.stream?.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
||||
subscribers
|
||||
.filter((sub)=>{return data.shareId == sub.shareId; })
|
||||
.forEach((sub)=>{
|
||||
sub?.stream?.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
||||
|
||||
// invoke debounced unsub to delay execution
|
||||
debounceUnsub(sub.id);
|
||||
});
|
||||
// invoke debounced unsub to delay execution
|
||||
debounceUnsub(sub.id);
|
||||
});
|
||||
});
|
||||
|
||||
// DEBUG: Report subscriber count periodically
|
||||
const REPORT_DELAY = 1000; //ms
|
||||
const reportIntervalId = setInterval(()=>{
|
||||
console.log('Subscriber count:', subscribers.length);
|
||||
}, REPORT_DELAY);
|
||||
|
||||
export default {
|
||||
emit : function(event) {return Stream.emit(event, ...([...arguments].slice(1)));}, // Arguments doesn't work for arrow functions
|
||||
on : (event, listener)=>{return Stream.on(event, listener);},
|
||||
|
||||
Reference in New Issue
Block a user