Skip to content

Commit

Permalink
remove unnecessary stringify and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Nov 22, 2023
1 parent 08bb764 commit 69748c0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/event-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ function EventBus ({ logger = console } = {}) {
this.sub.on('message', (_, message) => {
const channel = message.slice(0, message.indexOf(':'))
if (this.events.listenerCount(channel)) {
const payload = JSON.parse(message.slice(channel.length + 1))
return this.emitLocalEvent({ channel, payload })
return this.emitLocalEvent({ channel, payload: message.slice(channel.length + 1) })
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const files = [
})

fastify.decorateReply('sse', function (payload, event) {
this.raw.write(`id: ${this.message_count++}\n${event ? `event: ${event}\n` : ''}data: ${JSON.stringify(payload)}\n\n`)
this.raw.write(`id: ${this.message_count++}\n${event ? `event: ${event}\n` : ''}data: ${typeof payload === 'object' ? JSON.stringify(payload) : payload}\n\n`)
})

// Setup interval to ping every 30 seconds to keep the connection alive
Expand Down

0 comments on commit 69748c0

Please sign in to comment.