disable cache

This commit is contained in:
SrGooglo 2025-02-18 04:22:56 +00:00
parent 37ab0184c2
commit c1feeb0221

View File

@ -1,70 +1,82 @@
import SSEChannel from "../SSEChannel" import SSEChannel from "../SSEChannel"
export default class SSEManager { export default class SSEManager {
channels = new Map() channels = new Map()
createChannel(channelId) { createChannel(channelId) {
const channel = new SSEChannel({ const channel = new SSEChannel({
id: channelId id: channelId,
}) })
this.channels.set(channelId, channel) this.channels.set(channelId, channel)
return channel return channel
} }
sendToChannel(channelId, ...args) { sendToChannel(channelId, ...args) {
const channel = this.channels.get(channelId) if (!this.channels.has(channelId)) {
this.createChannel(channelId)
}
if (!channel) { const channel = this.channels.get(channelId)
throw new Error("Channel not found")
}
channel.cache.push(args) if (!channel) {
channel.eventBus.emit("data", ...args) throw new Error("Channel not found")
} }
connectToChannelStream = (channelId, req, res) => { //channel.cache.push(args)
const channel = this.channels.get(channelId) channel.eventBus.emit("data", ...args)
}
if (!channel) { connectToChannelStream = (channelId, req, res, { initialData } = {}) => {
throw new OperationError(404, `Channel [${channelId}] not found`) let channel = this.channels.get(channelId)
}
channel.clients.add(req) if (!channel) {
channel = this.createChannel(channelId)
//throw new OperationError(404, `Channel [${channelId}] not found`)
}
res.setHeader("Content-Type", "text/event-stream") channel.clients.add(req)
res.setHeader("Cache-Control", "no-cache")
res.setHeader("Connection", "keep-alive")
res.status(200)
if (channel.cache.length > 0) { res.setHeader("Content-Type", "text/event-stream")
for (const oldData of channel.cache) { res.setHeader("Cache-Control", "no-cache")
this.writeJSONToResponse(res, oldData) res.setHeader("Connection", "keep-alive")
} res.status(200)
}
channel.eventBus.on("data", (data) => { // if (channel.cache.length > 0) {
this.writeJSONToResponse(res, data) // for (const oldData of channel.cache) {
}) // this.writeJSONToResponse(res, oldData)
// }
// }
req.on("close", () => { this.writeJSONToResponse(res, {
channel.clients.delete(req) event: "connected",
})
if (channel.clients.size === 0) { if (initialData) {
this.channels.delete(channelId) this.writeJSONToResponse(res, initialData)
} }
res.end() channel.eventBus.on("data", (data) => {
}) this.writeJSONToResponse(res, data)
} })
writeJSONToResponse = (res, data) => { req.on("close", () => {
console.log(`[SSE] Sending data >`, data) channel.clients.delete(req)
res.write("data: " + JSON.stringify(data) + "\n\n")
}
getChannel = (channelId) => { if (channel.clients.size === 0) {
return this.channels.get(channelId) this.channels.delete(channelId)
} }
res.end()
})
}
writeJSONToResponse = (res, data) => {
res.write("data: " + JSON.stringify(data) + "\n\n")
}
getChannel = (channelId) => {
return this.channels.get(channelId)
}
} }