Skip to content

Commit 13178ac

Browse files
authored
Merge pull request #568 from sidorares/pr-567-tweaks
Make Promise Pool wrapper extend EventEmitter
2 parents 5f5bb7b + 6cd72ae commit 13178ac

File tree

4 files changed

+166
-62
lines changed

4 files changed

+166
-62
lines changed

promise.js

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
var core = require('./index.js');
2+
var EventEmitter = require('events').EventEmitter;
3+
var util = require('util');
4+
5+
function inheritEvents(source, target, events) {
6+
events
7+
.forEach(function (eventName) {
8+
source.on(eventName, function () {
9+
var args = [].slice.call(arguments);
10+
args.unshift(eventName);
11+
12+
target.emit.apply(target, args);
13+
});
14+
});
15+
}
216

317
function createConnection (opts) {
418
var coreConnection = core.createConnection(opts);
@@ -19,7 +33,10 @@ function createConnection (opts) {
1933
function PromiseConnection (connection, promiseImpl) {
2034
this.connection = connection;
2135
this.Promise = promiseImpl;
36+
37+
inheritEvents(connection, this, ['error', 'drain', 'connect', 'end', 'enqueue']);
2238
}
39+
util.inherits(PromiseConnection, EventEmitter);
2340

2441
PromiseConnection.prototype.release = function () {
2542
this.connection.release();
@@ -165,6 +182,62 @@ PromiseConnection.prototype.prepare = function () {
165182
'unprepare'
166183
]);
167184

185+
function PromisePool(pool, Promise) {
186+
this.pool = pool;
187+
this.Promise = Promise;
188+
189+
inheritEvents(pool, this, ['acquire', 'connection', 'enqueue', 'release']);
190+
}
191+
util.inherits(PromisePool, EventEmitter);
192+
193+
PromisePool.prototype.getConnection = function () {
194+
var corePool = this.pool;
195+
196+
return new this.Promise(function (resolve, reject) {
197+
corePool.getConnection(function (err, coreConnection) {
198+
if (err) {
199+
reject(err);
200+
} else {
201+
resolve(new PromiseConnection(coreConnection, Promise));
202+
}
203+
});
204+
});
205+
};
206+
207+
PromisePool.prototype.query = function (sql, args) {
208+
var corePool = this.pool;
209+
210+
return new this.Promise(function (resolve, reject) {
211+
var done = makeDoneCb(resolve, reject);
212+
if (args) {
213+
corePool.query(sql, args, done);
214+
} else {
215+
corePool.query(sql, done);
216+
}
217+
});
218+
};
219+
220+
PromisePool.prototype.execute = function (sql, values) {
221+
var corePool = this.pool;
222+
223+
return new Promise(function (resolve, reject) {
224+
corePool.execute(sql, values, makeDoneCb(resolve, reject));
225+
});
226+
};
227+
228+
PromisePool.prototype.end = function () {
229+
var corePool = this.pool;
230+
231+
return new Promise(function (resolve, reject) {
232+
corePool.end(function (err) {
233+
if (err) {
234+
reject(err);
235+
} else {
236+
resolve();
237+
}
238+
});
239+
});
240+
};
168241

169242
function createPool (opts) {
170243
var corePool = core.createPool(opts);
@@ -175,50 +248,7 @@ function createPool (opts) {
175248
' implementation as parameter, for example: { Promise: require(\'bluebird\') }');
176249
}
177250

178-
var promisePool = {
179-
getConnection: function () {
180-
return new Promise(function (resolve, reject) {
181-
corePool.getConnection(function (err, coreConnection) {
182-
if (err) {
183-
reject(err);
184-
} else {
185-
resolve(new PromiseConnection(coreConnection, Promise));
186-
}
187-
});
188-
});
189-
},
190-
191-
query: function (sql, args) {
192-
return new Promise(function (resolve, reject) {
193-
var done = makeDoneCb(resolve, reject);
194-
if (args) {
195-
corePool.query(sql, args, done);
196-
} else {
197-
corePool.query(sql, done);
198-
}
199-
});
200-
},
201-
202-
execute: function (sql, values) {
203-
return new Promise(function (resolve, reject) {
204-
corePool.execute(sql, values, makeDoneCb(resolve, reject));
205-
});
206-
},
207-
208-
end: function () {
209-
return new Promise(function (resolve, reject) {
210-
corePool.end(function (err) {
211-
if (err) {
212-
reject(err);
213-
} else {
214-
resolve();
215-
}
216-
});
217-
});
218-
}
219-
};
220-
221-
return promisePool;
251+
return new PromisePool(corePool, Promise);
222252
}
223253

224254
module.exports.createConnection = createConnection;

test/common.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,22 @@ module.exports.createConnection = function(args, callback) {
5656
// c.on('connect', function() {
5757
//
5858
// });
59-
setTimeout(
60-
function() {
61-
console.log('altering client...');
62-
c.oldQuery = c.query;
63-
c.query = function(sql, callback) {
64-
var rows = [];
65-
var q = c.oldQuery(sql);
66-
q.on('result', function(res) {
67-
res.on('row', function(row) {
68-
rows.push(row);
69-
});
70-
res.on('end', function() {
71-
callback(null, rows);
72-
});
59+
setTimeout(function() {
60+
console.log('altering client...');
61+
c.oldQuery = c.query;
62+
c.query = function(sql, callback) {
63+
var rows = [];
64+
var q = c.oldQuery(sql);
65+
q.on('result', function(res) {
66+
res.on('row', function(row) {
67+
rows.push(row);
7368
});
74-
};
75-
},
76-
1000
77-
);
69+
res.on('end', function() {
70+
callback(null, rows);
71+
});
72+
});
73+
};
74+
}, 1000);
7875
return c;
7976
}
8077

test/integration/test-promise-wrappers.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ assert.equal(mainExport, createConnection);
1818

1919
var doneCalled = false;
2020
var exceptionCaught = false;
21+
var doneEventsConnect = false;
2122

2223
var doneCalledPool = false;
2324
var exceptionCaughtPool = false;
25+
var doneEventsPool = false;
2426

2527
function testBasic() {
2628
var connResolved;
@@ -102,6 +104,38 @@ function testObjParams() {
102104
});
103105
}
104106

107+
function testEventsConnect() {
108+
var connPromise = createConnection(config).then(function(conn) {
109+
var events = 0;
110+
111+
conn
112+
.once('error', function(connection) {
113+
++events;
114+
})
115+
.once('drain', function(connection) {
116+
++events;
117+
})
118+
.once('connect', function() {
119+
++events;
120+
})
121+
.once('enqueue', function() {
122+
++events;
123+
})
124+
.once('end', function() {
125+
++events;
126+
127+
doneEventsConnect = events === 5;
128+
});
129+
130+
conn.connection.emit('error');
131+
conn.connection.emit('drain');
132+
conn.connection.emit('connect');
133+
conn.connection.emit('enqueue');
134+
conn.connection.emit('end');
135+
conn.end();
136+
});
137+
}
138+
105139
function testBasicPool() {
106140
var pool = createPool(config);
107141
pool
@@ -163,21 +197,51 @@ function testObjParamsPool() {
163197
});
164198
}
165199

200+
function testEventsPool() {
201+
var pool = createPool(config);
202+
var events = 0;
203+
204+
pool
205+
.once('acquire', function(connection) {
206+
++events;
207+
})
208+
.once('connection', function(connection) {
209+
++events;
210+
})
211+
.once('enqueue', function() {
212+
++events;
213+
})
214+
.once('release', function() {
215+
++events;
216+
217+
doneEventsPool = events === 4;
218+
});
219+
220+
pool.pool.emit('acquire');
221+
pool.pool.emit('connection');
222+
pool.pool.emit('enqueue');
223+
pool.pool.emit('release');
224+
}
225+
166226
testBasic();
167227
testErrors();
168228
testObjParams();
229+
testEventsConnect();
169230
testBasicPool();
170231
testErrorsPool();
171232
testObjParamsPool();
233+
testEventsPool();
172234

173235
process.on('exit', function() {
174236
if (skipTest) {
175237
return;
176238
}
177239
assert.equal(doneCalled, true);
178240
assert.equal(exceptionCaught, true);
241+
assert.equal(doneEventsConnect, true);
179242
assert.equal(doneCalledPool, true);
180243
assert.equal(exceptionCaughtPool, true);
244+
assert.equal(doneEventsPool, true);
181245
});
182246

183247
process.on('unhandledRejection', function(err) {

test/run.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,16 @@ if (process.env.FILTER) {
1212
process.env.TZ = 'UTC';
1313

1414
require('urun')(__dirname, options);
15+
16+
17+
process.on('exit', (code) => {
18+
console.log(`About to exit with code: ${code}`);
19+
});
20+
21+
process.on('unhandledRejection', (reason) => {
22+
console.log('unhandledRejection', reason);
23+
});
24+
25+
process.on('uncaughtException', (err) => {
26+
console.log('uncaughtException', err);
27+
});

0 commit comments

Comments
 (0)