Files
SolidScribe/server/helpers/Realtime.js
2026-11-03 11:20:03 -07:00

44 lines
1.3 KiB
JavaScript

//
// Socket.IO emit wrapper.
//
// Replaces the `if(typeof SocketIo != 'undefined')` guards that were scattered
// through the models. Those guards did not just avoid a crash - they silently
// disabled ALL scraping under Jest, which is why none of this code was ever
// testable. Here the no-socket case is a no-op rather than a skipped branch, so
// the surrounding logic runs identically in tests and in production.
//
let Realtime = module.exports = {}
let warned = false
const server = () => (typeof global.SocketIo !== 'undefined' && global.SocketIo) ? global.SocketIo : null
Realtime.isEnabled = () => server() !== null
const emit = (room, event, payload) => {
const io = server()
if (!io) {
if (!warned) {
warned = true
console.log('[realtime] no socket server bound; emits are no-ops')
}
return false
}
try {
if (payload === undefined) { io.to(room).emit(event) }
else { io.to(room).emit(event, payload) }
return true
} catch (error) {
console.log('[realtime] emit failed', event, error.message)
return false
}
}
Realtime.emitToUser = (userId, event, payload) => emit(`user:${userId}`, event, payload)
Realtime.emitToNote = (noteRawTextId, event, payload) => emit(`note:${noteRawTextId}`, event, payload)
Realtime.__resetWarning = () => { warned = false }