disable cache

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

View File

@ -5,7 +5,7 @@ export default class SSEManager {
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)
@ -14,21 +14,26 @@ export default class SSEManager {
} }
sendToChannel(channelId, ...args) { sendToChannel(channelId, ...args) {
if (!this.channels.has(channelId)) {
this.createChannel(channelId)
}
const channel = this.channels.get(channelId) const channel = this.channels.get(channelId)
if (!channel) { if (!channel) {
throw new Error("Channel not found") throw new Error("Channel not found")
} }
channel.cache.push(args) //channel.cache.push(args)
channel.eventBus.emit("data", ...args) channel.eventBus.emit("data", ...args)
} }
connectToChannelStream = (channelId, req, res) => { connectToChannelStream = (channelId, req, res, { initialData } = {}) => {
const channel = this.channels.get(channelId) let channel = this.channels.get(channelId)
if (!channel) { if (!channel) {
throw new OperationError(404, `Channel [${channelId}] not found`) channel = this.createChannel(channelId)
//throw new OperationError(404, `Channel [${channelId}] not found`)
} }
channel.clients.add(req) channel.clients.add(req)
@ -38,10 +43,18 @@ export default class SSEManager {
res.setHeader("Connection", "keep-alive") res.setHeader("Connection", "keep-alive")
res.status(200) res.status(200)
if (channel.cache.length > 0) { // if (channel.cache.length > 0) {
for (const oldData of channel.cache) { // for (const oldData of channel.cache) {
this.writeJSONToResponse(res, oldData) // this.writeJSONToResponse(res, oldData)
} // }
// }
this.writeJSONToResponse(res, {
event: "connected",
})
if (initialData) {
this.writeJSONToResponse(res, initialData)
} }
channel.eventBus.on("data", (data) => { channel.eventBus.on("data", (data) => {
@ -60,7 +73,6 @@ export default class SSEManager {
} }
writeJSONToResponse = (res, data) => { writeJSONToResponse = (res, data) => {
console.log(`[SSE] Sending data >`, data)
res.write("data: " + JSON.stringify(data) + "\n\n") res.write("data: " + JSON.stringify(data) + "\n\n")
} }