From fbb3bfc044c97e36849808d1eaea79ea206d54e3 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Fri, 28 May 2021 17:39:32 +0200 Subject: [PATCH 1/4] Add similar promise variables to read() and close() as seen in query() --- packages/pg-cursor/index.js | 40 +++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/pg-cursor/index.js b/packages/pg-cursor/index.js index 8e8552be8..ce967a909 100644 --- a/packages/pg-cursor/index.js +++ b/packages/pg-cursor/index.js @@ -17,6 +17,7 @@ class Cursor extends EventEmitter { this._queue = [] this.state = 'initialized' this._result = new Result(this._conf.rowMode, this._conf.types) + this._Promise = this._conf.Promise || global.Promise this._cb = null this._rows = null this._portal = null @@ -198,6 +199,14 @@ class Cursor extends EventEmitter { } close(cb) { + var promise + + if (!cb) { + promise = new this._Promise((resolve, reject) => { + cb = (err) => (err ? reject(err) : resolve()) + }) + } + if (!this.connection || this.state === 'done') { if (cb) { return setImmediate(cb) @@ -213,23 +222,34 @@ class Cursor extends EventEmitter { cb() }) } + + // Return the promise (or undefined) + return promise } read(rows, cb) { - if (this.state === 'idle' || this.state === 'submitted') { - return this._getRows(rows, cb) - } - if (this.state === 'busy' || this.state === 'initialized') { - return this._queue.push([rows, cb]) - } - if (this.state === 'error') { - return setImmediate(() => cb(this._error)) + var promise + + if (!cb) { + promise = new this._Promise((resolve, reject) => { + cb = (err, rows) => (err ? reject(err) : resolve(rows)) + }) } - if (this.state === 'done') { - return setImmediate(() => cb(null, [])) + + if (this.state === 'idle' || this.state === 'submitted') { + this._getRows(rows, cb) + } else if (this.state === 'busy' || this.state === 'initialized') { + this._queue.push([rows, cb]) + } else if (this.state === 'error') { + setImmediate(() => cb(this._error)) + } else if (this.state === 'done') { + setImmediate(() => cb(null, [])) } else { throw new Error('Unknown state: ' + this.state) } + + // Return the promise (or undefined) + return promise } } From 21e36b15cd94f50be4109817887a8c07846743e8 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Tue, 22 Jun 2021 23:43:34 +0200 Subject: [PATCH 2/4] Add testing for promise specific usage --- packages/pg-cursor/test/promises.js | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 packages/pg-cursor/test/promises.js diff --git a/packages/pg-cursor/test/promises.js b/packages/pg-cursor/test/promises.js new file mode 100644 index 000000000..664f4d3ed --- /dev/null +++ b/packages/pg-cursor/test/promises.js @@ -0,0 +1,60 @@ +const assert = require('assert') +const Cursor = require('../') +const pg = require('pg') + +const text = 'SELECT generate_series as num FROM generate_series(0, 5)' + +describe('cursor using promises', function () { + beforeEach(function (done) { + const client = (this.client = new pg.Client()) + client.connect(done) + + this.pgCursor = function (text, values) { + return client.query(new Cursor(text, values || [])) + } + }) + + afterEach(function () { + this.client.end() + }) + + it('resolve with result', function (done) { + const cursor = this.pgCursor(text) + cursor + .read(6) + .then((res) => assert.strictEqual(res.length, 6)) + .error((err) => assert.ifError(err)) + .finally(() => done()) + }) + + it('reject with error', function (done) { + const cursor = this.pgCursor('select asdfasdf') + cursor.read(1).error((err) => { + assert(err) + done() + }) + }) + + it('read multiple times', async function (done) { + const cursor = this.pgCursor(text) + let res + + try { + res = await cursor.read(2) + assert.strictEqual(res.length, 2) + + res = await cursor.read(3) + assert.strictEqual(res.length, 3) + + res = await cursor.read(1) + assert.strictEqual(res.length, 1) + + res = await cursor.read(1) + assert.strictEqual(res.length, 0) + } catch (err) { + assert.ifError(err) + } finally { + done() + } + }) +}) From b2e3b7788963be546dade513ec84c2ad4be30606 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Wed, 23 Jun 2021 18:30:52 +0200 Subject: [PATCH 3/4] Simplify tests as no real callbacks are involved Removes usage of `done()` since we can end the test when we exit the function Co-Authored-By: Charmander <~@charmander.me> --- packages/pg-cursor/test/promises.js | 33 +++++++++++------------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/packages/pg-cursor/test/promises.js b/packages/pg-cursor/test/promises.js index 664f4d3ed..7b36dab8f 100644 --- a/packages/pg-cursor/test/promises.js +++ b/packages/pg-cursor/test/promises.js @@ -18,13 +18,10 @@ describe('cursor using promises', function () { this.client.end() }) - it('resolve with result', function (done) { + it('resolve with result', async function () { const cursor = this.pgCursor(text) - cursor - .read(6) - .then((res) => assert.strictEqual(res.length, 6)) - .error((err) => assert.ifError(err)) - .finally(() => done()) + const res = await cursor.read(6) + assert.strictEqual(res.length, 6) }) it('reject with error', function (done) { @@ -35,26 +32,20 @@ describe('cursor using promises', function () { }) }) - it('read multiple times', async function (done) { + it('read multiple times', async function () { const cursor = this.pgCursor(text) let res - try { - res = await cursor.read(2) - assert.strictEqual(res.length, 2) + res = await cursor.read(2) + assert.strictEqual(res.length, 2) - res = await cursor.read(3) - assert.strictEqual(res.length, 3) + res = await cursor.read(3) + assert.strictEqual(res.length, 3) - res = await cursor.read(1) - assert.strictEqual(res.length, 1) + res = await cursor.read(1) + assert.strictEqual(res.length, 1) - res = await cursor.read(1) - assert.strictEqual(res.length, 0) - } catch (err) { - assert.ifError(err) - } finally { - done() - } + res = await cursor.read(1) + assert.strictEqual(res.length, 0) }) }) From 631bfccd83595508fd1c48998376fa6177339aaf Mon Sep 17 00:00:00 2001 From: Bluenix Date: Wed, 23 Jun 2021 22:45:32 +0200 Subject: [PATCH 4/4] Switch to let over var --- packages/pg-cursor/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pg-cursor/index.js b/packages/pg-cursor/index.js index ce967a909..b77fd5977 100644 --- a/packages/pg-cursor/index.js +++ b/packages/pg-cursor/index.js @@ -199,7 +199,7 @@ class Cursor extends EventEmitter { } close(cb) { - var promise + let promise if (!cb) { promise = new this._Promise((resolve, reject) => { @@ -228,7 +228,7 @@ class Cursor extends EventEmitter { } read(rows, cb) { - var promise + let promise if (!cb) { promise = new this._Promise((resolve, reject) => {