diff --git a/lib/array.js b/lib/array.js index b3e5f9d1..97367108 100644 --- a/lib/array.js +++ b/lib/array.js @@ -84,29 +84,11 @@ function countIf(arr, cb, obj) { return count; } -// Conditionally removes elements; -function removeIf(arr, cb, obj) { - if (obj) { - for (var i = arr.length; i--;) { - if (cb.call(obj, arr[i], i, arr)) { - arr.splice(i, 1); - } - } - } else { - for (var i = arr.length; i--;) { - if (cb(arr[i], i, arr)) { - arr.splice(i, 1); - } - } - } -} - module.exports = { map: map, filter: filter, forEach: forEach, - countIf: countIf, - removeIf: removeIf + countIf: countIf }; Object.freeze(module.exports); diff --git a/lib/connect.js b/lib/connect.js index 06b5b4f4..277f183e 100644 --- a/lib/connect.js +++ b/lib/connect.js @@ -1,56 +1,16 @@ 'use strict'; var $npm = { - pg: require('pg'), utils: require('./utils'), events: require('./events') }; -var $arr = require('../lib/array'); - -var liveClientKeys = {}; -var liveNativeClients = []; - -function getFreshStatus(config, client) { - // we do not test code specific to Native Bindings; - // istanbul ignore next - if (config.native) { - for (var i = 0; i < liveNativeClients.length; i++) { - var c = liveNativeClients[i]; - if (c.client === client) { - c.tick = Date.now(); - return false; - } - } - liveNativeClients.push({ - client: client, - tick: Date.now() - }); - return true; - } - var isFreshClient = !(client.secretKey in liveClientKeys); - liveClientKeys[client.secretKey] = Date.now(); - return isFreshClient; -} - -function clearFreshStatus(config) { - var now = Date.now(), delay = $npm.pg.defaults.poolIdleTimeout + 1000; - // we do not test code specific to Native Bindings; - // istanbul ignore if - if (config.native) { - $arr.removeIf(liveNativeClients, function (c) { - return now - c.tick > delay; - }); - } else { - $arr.map(Object.keys(liveClientKeys), function (key) { - // It is impossible to test expired connections automatically, - // therefore we are excluding it from the text coverage: - // istanbul ignore next; - if (now - liveClientKeys[key] > delay) { - delete liveClientKeys[key]; - } - }); +function isFreshClient(client) { + if (client.$used) { + return false; } + $npm.utils.addReadProp(client, '$used', true, true); + return true; } function poolConnect(ctx, config) { @@ -63,7 +23,6 @@ function poolConnect(ctx, config) { }); reject(err); } else { - var isFreshClient = getFreshStatus(config, client); var end = client.end; client.end = function () { throw new Error("Cannot invoke client.end() directly."); @@ -74,10 +33,9 @@ function poolConnect(ctx, config) { client.end = end; done(); $npm.events.disconnect(ctx, client); - clearFreshStatus(config); } }); - $npm.events.connect(ctx, client, isFreshClient); + $npm.events.connect(ctx, client, isFreshClient(client)); } }); }); @@ -94,7 +52,6 @@ function directConnect(ctx, config) { }); reject(err); } else { - var isFreshClient = getFreshStatus(config, client); var end = client.end; client.end = function () { throw new Error("Cannot invoke client.end() directly."); @@ -104,10 +61,9 @@ function directConnect(ctx, config) { done: function () { end.call(client); $npm.events.disconnect(ctx, client); - clearFreshStatus(config); } }); - $npm.events.connect(ctx, client, isFreshClient); + $npm.events.connect(ctx, client, isFreshClient(client)); } }); }); diff --git a/lib/events.js b/lib/events.js index 9c00ca24..f3d02ea7 100644 --- a/lib/events.js +++ b/lib/events.js @@ -26,8 +26,8 @@ var $events = { * @param {boolean} fresh * **Added in v.4.4.4** * - * It indicates when the connection has been freshly allocated: - * - `true` - the connection has been freshly allocated + * It indicates when it is a fresh connection: + * - `true` - the connection just has been allocated * - `false` - the connection has been used previously * * @example diff --git a/package.json b/package.json index 8d48aa67..f5ead46b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-promise", - "version": "4.4.4", + "version": "4.4.5", "description": "PostgreSQL via promises", "main": "lib/index.js", "scripts": { diff --git a/test/arraySpec.js b/test/arraySpec.js index 259c39c7..3c2f2cb0 100644 --- a/test/arraySpec.js +++ b/test/arraySpec.js @@ -152,39 +152,3 @@ describe("countIf", function () { }); }); - -describe("removeIf", function () { - - describe("with context", function () { - var ctx, arr, indexes = [], values = [1, 2, 3, 4, 5]; - $arr.removeIf(values, function (d, idx, a) { - arr = a; - ctx = this; - indexes.push(idx); - return d === 2 || d === 4; - }, values); - it("must remove correctly", function () { - expect(ctx).toBe(values); - expect(arr).toBe(values); - expect(values).toEqual([1, 3, 5]); - expect(indexes).toEqual([4, 3, 2, 1, 0]); - }); - }); - - describe("without context", function () { - var ctx, arr, indexes = [], values = [1, 2, 3, 4, 5]; - $arr.removeIf(values, function (d, idx, a) { - arr = a; - ctx = this; - indexes.push(idx); - return d === 2 || d === 4; - }); - it("must remove correctly", function () { - expect(ctx).toBeUndefined(); - expect(arr).toBe(values); - expect(values).toEqual([1, 3, 5]); - expect(indexes).toEqual([4, 3, 2, 1, 0]); - }); - }); - -});