Skip to content

Commit a83a6c1

Browse files
committed
Changed how PGClient handles notifications
1 parent fb69ff9 commit a83a6c1

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

async_postgres.lua

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ end
165165
---@field private retryAttempted number number of attempts to reconnect to the database
166166
---@field private conn PGconn native connection object (do not use it directly, otherwise be careful not to store it anywhere else, otherwise closing connection will be impossible)
167167
---@field private queries { push: fun(self, q: PGQuery), prepend: fun(self, q: PGQuery), pop: (fun(self): PGQuery), size: fun(self): number } list of queries
168-
---@field private notifyCallback function? callback for NOTIFY messages
169168
---@field package acquired boolean
170169
---@field package pool PGPool?
171170
local Client = {}
@@ -235,7 +234,10 @@ function Client:connect(callback)
235234
if ok then
236235
---@cast conn PGconn
237236
self.conn = conn
238-
pcall(self.conn.setNotifyCallback, self.conn, self.notifyCallback)
237+
self.conn:setNotifyCallback(function(channel, payload, backendPID)
238+
self:onNotify(channel, payload, backendPID)
239+
end)
240+
239241
xpcall(callback, ErrorNoHaltWithStack, ok)
240242
self:processQueue()
241243
else
@@ -481,15 +483,6 @@ function Client:pendingQueries()
481483
return self.queries:size()
482484
end
483485

484-
--- Sets a callback for NOTIFY messages
485-
---@param callback fun(channel: string, payload: string, backendPID: number)
486-
function Client:setNotifyCallback(callback)
487-
self.notifyCallback = callback
488-
if self.conn then
489-
self.conn:setNotifyCallback(callback)
490-
end
491-
end
492-
493486
--- Returns the database name of the connection.
494487
---@return string
495488
function Client:db()
@@ -657,6 +650,15 @@ function Client:release()
657650
pool:processQueue() -- after client was release, we need to process pool queue
658651
end
659652

653+
--- This function is called when NOTIFY message is received
654+
---
655+
--- You can set it to your own function to handle NOTIFY messages
656+
---@param channel string
657+
---@param payload string
658+
---@param backendPID number
659+
function Client:onNotify(channel, payload, backendPID)
660+
end
661+
660662
--- Creates a new client with given connection url
661663
--- ```lua
662664
--- local client = async_postgres.Client("postgresql://user:password@localhost:5432/database")

0 commit comments

Comments
 (0)