Skip to content

Commit

Permalink
refactoring prameter fresh.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jun 5, 2016
1 parent 8b3e504 commit 90cc311
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 109 deletions.
20 changes: 1 addition & 19 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
58 changes: 7 additions & 51 deletions lib/connect.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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.");
Expand All @@ -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));
}
});
});
Expand All @@ -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.");
Expand All @@ -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));
}
});
});
Expand Down
4 changes: 2 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "4.4.4",
"version": "4.4.5",
"description": "PostgreSQL via promises",
"main": "lib/index.js",
"scripts": {
Expand Down
36 changes: 0 additions & 36 deletions test/arraySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});

});

0 comments on commit 90cc311

Please sign in to comment.