reconnectTimes: -1, // infinite retries by default
};
+/**
+ * @typedef {import('pg-promise')} PGPromise
+ */
+
/**
* Create a robust connection which listens to a notification channel.
*/
class PostgresListener {
+ static PingPayload = 'ping';
+
+ /**
+ * @param {*} logger logger
+ * @param {PGPromise.Database} db db
+ * @param {*} options options
+ */
constructor(logger, db, options) {
this.logger = logger;
this.db = db;
this.nextPingTimeout = setTimeout(async () => {
try {
if (this.connection) {
- await this.connection.none('NOTIFY $(channel:name), $(payload)', { channel: this.options.channel, payload: 'ping' });
+ await this.connection.none('NOTIFY $(channel:name), $(payload)', { channel: this.options.channel, payload: this.constructor.PingPayload });
}
} catch (e) {
this.logger.error(_scope, 'failed', { error: e });
*/
async _onNotification(data) {
const _scope = _fileScope('_onNotification');
- this.logger.debug(_scope, 'called', data);
const { channel, payload } = data;
// Ignore our own messages
- if (payload === 'ping') {
+ if (payload === this.constructor.PingPayload) {
return;
}
+ this.logger.debug(_scope, 'called', data);
await this.options.dataCallback(payload, channel);
}