Skip to content

Commit 71f30fa

Browse files
authored
Merge pull request brianc#29 from brianc/no-sync-callbacks
Cleanup
2 parents 5a0af8c + bbefeb8 commit 71f30fa

File tree

11 files changed

+214
-151
lines changed

11 files changed

+214
-151
lines changed

.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "standard",
3+
"env": {
4+
"mocha": true
5+
},
6+
"rules": {
7+
"no-new-func": "off"
8+
}
9+
}

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ language: node_js
22
dist: trusty
33
sudo: false
44
node_js:
5+
- "4.2"
56
- "6"
7+
- "8"
68
env:
79
- PGUSER=postgres
810
services:
911
- postgresql
1012
addons:
1113
postgresql: "9.6"
1214
before_script:
13-
- psql -c 'create database travis;' -U postgres | true
15+
- psql -c 'create database travis;' -U postgres | true

index.js

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
var Result = require('./pg').Result
2-
var prepare = require('./pg').prepareValue
3-
var EventEmitter = require('events').EventEmitter;
4-
var util = require('util');
1+
'use strict'
2+
const Result = require('./pg').Result
3+
const prepare = require('./pg').prepareValue
4+
const EventEmitter = require('events').EventEmitter
5+
const util = require('util')
56

67
function Cursor (text, values) {
7-
EventEmitter.call(this);
8+
EventEmitter.call(this)
89

910
this.text = text
1011
this.values = values ? values.map(prepare) : null
@@ -18,11 +19,10 @@ function Cursor (text, values) {
1819

1920
util.inherits(Cursor, EventEmitter)
2021

21-
Cursor.prototype.submit = function(connection) {
22+
Cursor.prototype.submit = function (connection) {
2223
this.connection = connection
2324

24-
var con = connection
25-
var self = this
25+
const con = connection
2626

2727
con.parse({
2828
text: this.text
@@ -34,146 +34,143 @@ Cursor.prototype.submit = function(connection) {
3434

3535
con.describe({
3636
type: 'P',
37-
name: '' //use unamed portal
37+
name: '' // use unamed portal
3838
}, true)
3939

4040
con.flush()
4141

42+
const ifNoData = () => {
43+
this.state = 'idle'
44+
this._shiftQueue()
45+
}
46+
4247
con.once('noData', ifNoData)
4348
con.once('rowDescription', function () {
44-
con.removeListener('noData', ifNoData);
45-
});
46-
47-
function ifNoData () {
48-
self.state = 'idle'
49-
self._shiftQueue();
50-
}
49+
con.removeListener('noData', ifNoData)
50+
})
5151
}
5252

5353
Cursor.prototype._shiftQueue = function () {
54-
if(this._queue.length) {
54+
if (this._queue.length) {
5555
this._getRows.apply(this, this._queue.shift())
5656
}
5757
}
5858

59-
Cursor.prototype.handleRowDescription = function(msg) {
59+
Cursor.prototype.handleRowDescription = function (msg) {
6060
this._result.addFields(msg.fields)
6161
this.state = 'idle'
62-
this._shiftQueue();
62+
this._shiftQueue()
6363
}
6464

65-
Cursor.prototype.handleDataRow = function(msg) {
66-
var row = this._result.parseRow(msg.fields)
65+
Cursor.prototype.handleDataRow = function (msg) {
66+
const row = this._result.parseRow(msg.fields)
6767
this.emit('row', row, this._result)
6868
this._rows.push(row)
6969
}
7070

71-
Cursor.prototype._sendRows = function() {
71+
Cursor.prototype._sendRows = function () {
7272
this.state = 'idle'
73-
setImmediate(function() {
74-
var cb = this._cb
75-
//remove callback before calling it
76-
//because likely a new one will be added
77-
//within the call to this callback
73+
setImmediate(() => {
74+
const cb = this._cb
75+
// remove callback before calling it
76+
// because likely a new one will be added
77+
// within the call to this callback
7878
this._cb = null
79-
if(cb) {
79+
if (cb) {
8080
this._result.rows = this._rows
8181
cb(null, this._rows, this._result)
8282
}
8383
this._rows = []
84-
}.bind(this))
84+
})
8585
}
8686

87-
Cursor.prototype.handleCommandComplete = function() {
87+
Cursor.prototype.handleCommandComplete = function () {
8888
this.connection.sync()
8989
}
9090

91-
Cursor.prototype.handlePortalSuspended = function() {
91+
Cursor.prototype.handlePortalSuspended = function () {
9292
this._sendRows()
9393
}
9494

95-
Cursor.prototype.handleReadyForQuery = function() {
95+
Cursor.prototype.handleReadyForQuery = function () {
9696
this._sendRows()
9797
this.emit('end', this._result)
9898
this.state = 'done'
9999
}
100100

101-
Cursor.prototype.handleEmptyQuery = function(con) {
102-
if (con.sync) {
103-
con.sync()
104-
}
105-
};
101+
Cursor.prototype.handleEmptyQuery = function () {
102+
this.connection.sync()
103+
}
106104

107-
Cursor.prototype.handleError = function(msg) {
105+
Cursor.prototype.handleError = function (msg) {
108106
this.state = 'error'
109107
this._error = msg
110-
//satisfy any waiting callback
111-
if(this._cb) {
108+
// satisfy any waiting callback
109+
if (this._cb) {
112110
this._cb(msg)
113111
}
114-
//dispatch error to all waiting callbacks
115-
for(var i = 0; i < this._queue.length; i++) {
112+
// dispatch error to all waiting callbacks
113+
for (var i = 0; i < this._queue.length; i++) {
116114
this._queue.pop()[1](msg)
117115
}
118116

119-
if (this.eventNames().indexOf('error') >= 0) {
120-
//only dispatch error events if we have a listener
117+
if (this.listenerCount('error') > 0) {
118+
// only dispatch error events if we have a listener
121119
this.emit('error', msg)
122120
}
123-
//call sync to keep this connection from hanging
121+
// call sync to keep this connection from hanging
124122
this.connection.sync()
125123
}
126124

127-
Cursor.prototype._getRows = function(rows, cb) {
125+
Cursor.prototype._getRows = function (rows, cb) {
128126
this.state = 'busy'
129127
this._cb = cb
130128
this._rows = []
131-
var msg = {
129+
const msg = {
132130
portal: '',
133131
rows: rows
134132
}
135133
this.connection.execute(msg, true)
136134
this.connection.flush()
137135
}
138136

139-
Cursor.prototype.end = function(cb) {
140-
if(this.state != 'initialized') {
137+
Cursor.prototype.end = function (cb) {
138+
if (this.state !== 'initialized') {
141139
this.connection.sync()
142140
}
143-
this.connection.end()
144141
this.connection.stream.once('end', cb)
142+
console.log('calling end on connection')
143+
this.connection.end()
145144
}
146145

147-
Cursor.prototype.close = function(cb) {
148-
if (this.state == 'done') {
146+
Cursor.prototype.close = function (cb) {
147+
if (this.state === 'done') {
149148
return setImmediate(cb)
150149
}
151150
this.connection.close({type: 'P'})
152151
this.connection.sync()
153152
this.state = 'done'
154-
if(cb) {
155-
this.connection.once('closeComplete', function() {
153+
if (cb) {
154+
this.connection.once('closeComplete', function () {
156155
cb()
157156
})
158157
}
159158
}
160159

161-
Cursor.prototype.read = function(rows, cb) {
162-
var self = this
163-
if(this.state == 'idle') {
160+
Cursor.prototype.read = function (rows, cb) {
161+
if (this.state === 'idle') {
164162
return this._getRows(rows, cb)
165163
}
166-
if(this.state == 'busy' || this.state == 'initialized') {
164+
if (this.state === 'busy' || this.state === 'initialized') {
167165
return this._queue.push([rows, cb])
168166
}
169-
if(this.state == 'error') {
170-
return cb(this._error)
171-
}
172-
if(this.state == 'done') {
173-
return cb(null, [])
167+
if (this.state === 'error') {
168+
return setImmediate(() => cb(this._error))
174169
}
175-
else {
176-
throw new Error("Unknown state: " + this.state)
170+
if (this.state === 'done') {
171+
return setImmediate(() => cb(null, []))
172+
} else {
173+
throw new Error('Unknown state: ' + this.state)
177174
}
178175
}
179176

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
"test": "test"
88
},
99
"scripts": {
10-
"test": "mocha test/"
10+
"test": " mocha && eslint ."
1111
},
1212
"author": "Brian M. Carlson",
1313
"license": "MIT",
1414
"devDependencies": {
15-
"pg": "~6.0.0",
16-
"mocha": "~1.17.1"
15+
"eslint": "^4.4.0",
16+
"eslint-config-standard": "^10.2.1",
17+
"eslint-plugin-import": "^2.7.0",
18+
"eslint-plugin-node": "^5.1.1",
19+
"eslint-plugin-promise": "^3.5.0",
20+
"eslint-plugin-standard": "^3.0.1",
21+
"mocha": "^3.5.0",
22+
"pg": "~6.0.0"
1723
},
1824
"dependencies": {}
1925
}

pg.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//support both pg & pg.js
2-
//this will eventually go away when i break native bindings
3-
//out into their own module
1+
// support both pg & pg.js
2+
// this will eventually go away when i break native bindings
3+
// out into their own module
44
try {
55
module.exports.Result = require('pg/lib/result.js')
66
module.exports.prepareValue = require('pg/lib/utils.js').prepareValue
7-
} catch(e) {
7+
} catch (e) {
88
module.exports.Result = require('pg.js/lib/result.js')
99
module.exports.prepareValue = require('pg.js/lib/utils.js').prepareValue
1010
}

test/close.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ var Cursor = require('../')
33
var pg = require('pg')
44

55
var text = 'SELECT generate_series as num FROM generate_series(0, 50)'
6-
describe('close', function() {
7-
beforeEach(function(done) {
6+
describe('close', function () {
7+
beforeEach(function (done) {
88
var client = this.client = new pg.Client()
99
client.connect(done)
1010
client.on('drain', client.end.bind(client))
1111
})
1212

13-
it('closes cursor early', function(done) {
13+
it('closes cursor early', function (done) {
1414
var cursor = new Cursor(text)
1515
this.client.query(cursor)
1616
this.client.query('SELECT NOW()', done)
17-
cursor.read(25, function(err, res) {
17+
cursor.read(25, function (err, res) {
1818
assert.ifError(err)
1919
cursor.close()
2020
})
2121
})
2222

23-
it('works with callback style', function(done) {
23+
it('works with callback style', function (done) {
2424
var cursor = new Cursor(text)
2525
var client = this.client
2626
client.query(cursor)
27-
cursor.read(25, function(err, res) {
27+
cursor.read(25, function (err, res) {
2828
assert.ifError(err)
29-
cursor.close(function(err) {
29+
cursor.close(function (err) {
3030
assert.ifError(err)
3131
client.query('SELECT NOW()', done)
3232
})

0 commit comments

Comments
 (0)