Comparing version 1.3.1 to 1.3.2-connected
@@ -9,2 +9,3 @@ import TypedEventEmitter from "typed-emitter"; | ||
interface PgListenEvents { | ||
connected: () => void; | ||
error: (error: Error) => void; | ||
@@ -63,3 +64,3 @@ notification: (notification: PgParsedNotification) => void; | ||
listenTo(channelName: string): Promise<pg.QueryResult> | undefined; | ||
notify(channelName: string, payload: any): Promise<pg.QueryResult>; | ||
notify(channelName: string, payload?: any): Promise<pg.QueryResult>; | ||
unlisten(channelName: string): Promise<pg.QueryResult> | undefined; | ||
@@ -66,0 +67,0 @@ unlistenAll(): Promise<pg.QueryResult>; |
@@ -62,3 +62,3 @@ "use strict"; | ||
var delay = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); }; | ||
function connect(connectionConfig, options) { | ||
function connect(connectionConfig, emitter, options) { | ||
var _this = this; | ||
@@ -70,2 +70,3 @@ connectionLogger("Creating PostgreSQL client for notification streaming"); | ||
var dbClient = new Client(effectiveConnectionConfig); | ||
dbClient.once("connect", function () { return emitter.emit("connected"); }); | ||
var reconnect = function (onAttempt) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -101,2 +102,3 @@ var startTime, _loop_1, attempt, state_1; | ||
connectionLogger("PostgreSQL reconnection succeeded"); | ||
emitter.emit("connected"); | ||
return [2 /*return*/, { value: newClient_1 }]; | ||
@@ -144,3 +146,3 @@ case 3: | ||
try { | ||
payload = notification.payload !== undefined ? parse(notification.payload) : undefined; | ||
payload = notification.payload ? parse(notification.payload) : undefined; | ||
} | ||
@@ -203,3 +205,3 @@ catch (error) { | ||
}); | ||
var _d = connect(connectionConfig, options), initialDBClient = _d.dbClient, reconnect = _d.reconnect; | ||
var _d = connect(connectionConfig, emitter, options), initialDBClient = _d.dbClient, reconnect = _d.reconnect; | ||
var closing = false; | ||
@@ -300,4 +302,9 @@ var dbClient = initialDBClient; | ||
notificationLogger("Sending PostgreSQL notification to \"" + channelName + "\":", payload); | ||
var serialized = serialize(payload); | ||
return dbClient.query("NOTIFY " + pg_format_1.default.ident(channelName) + ", " + pg_format_1.default.literal(serialized)); | ||
if (payload !== undefined) { | ||
var serialized = serialize(payload); | ||
return dbClient.query("NOTIFY " + pg_format_1.default.ident(channelName) + ", " + pg_format_1.default.literal(serialized)); | ||
} | ||
else { | ||
return dbClient.query("NOTIFY " + pg_format_1.default.ident(channelName)); | ||
} | ||
}, | ||
@@ -304,0 +311,0 @@ unlisten: function (channelName) { |
{ | ||
"name": "pg-listen", | ||
"version": "1.3.1", | ||
"version": "1.3.2-connected", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "PostgreSQL LISTEN & NOTIFY that finally works.", |
@@ -79,2 +79,6 @@ # pg-listen - Postgres LISTEN & NOTIFY that works | ||
#### `instance.events.on("connected", listener: () => void)` | ||
The `connected` event is emitted once after initially establishing the connection and later once after every successful reconnect. Reconnects happen automatically when `pg-listen` detects that the connection closed or became unresponsive. | ||
#### `instance.events.on("error", listener: (error: Error) => void)` | ||
@@ -81,0 +85,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
27754
432
123
2