Repository: tinkerpop Updated Branches: refs/heads/TINKERPOP-1489 f2f9729cd -> 5fda8188a
Remove utils.toPromise() Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/af718f7c Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/af718f7c Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/af718f7c Branch: refs/heads/TINKERPOP-1489 Commit: af718f7ca53111b371903471ae76841aa7e06861 Parents: f2f9729 Author: Jorge Bay Gondra <jorgebaygon...@gmail.com> Authored: Mon Jan 22 11:19:07 2018 +0100 Committer: Jorge Bay Gondra <jorgebaygon...@gmail.com> Committed: Mon Jan 22 11:19:07 2018 +0100 ---------------------------------------------------------------------- .../lib/driver/driver-remote-connection.js | 68 +++++++++----------- .../lib/driver/remote-connection.js | 9 ++- .../lib/process/traversal-strategy.js | 20 ++---- .../javascript/gremlin-javascript/lib/utils.js | 31 --------- .../test/unit/traversal-test.js | 13 ++-- 5 files changed, 44 insertions(+), 97 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/af718f7c/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js index ac3fb0f..5587fd6 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js @@ -59,16 +59,13 @@ class DriverRemoteConnection extends RemoteConnection { pfx: options.pfx, rejectUnauthorized: options.rejectUnauthorized }); - var self = this; - this._ws.on('open', function opened () { - self.isOpen = true; - if (self._openCallback) { - self._openCallback(); + this._ws.on('open', () => { + this.isOpen = true; + if (this._openCallback) { + this._openCallback(); } }); - this._ws.on('message', function incoming (data) { - self._handleMessage(data); - }); + this._ws.on('message', data => this._handleMessage(data)); // A map containing the request id and the handler this._responseHandlers = {}; this._reader = options.reader || new serializer.GraphSONReader(); @@ -86,39 +83,33 @@ class DriverRemoteConnection extends RemoteConnection { * Opens the connection, if its not already opened. * @returns {Promise} */ - open(promiseFactory) { + open() { if (this._closePromise) { - return this._openPromise = utils.toPromise(promiseFactory, function promiseHandler(callback) { - callback(new Error('Connection has been closed')); - }); + return this._openPromise = Promise.reject(new Error('Connection has been closed')); + } + if (this.isOpen) { + return Promise.resolve(); } if (this._openPromise) { return this._openPromise; } - const self = this; - return this._openPromise = utils.toPromise(promiseFactory, function promiseHandler(callback) { - if (self.isOpen) { - return callback(); - } - // It will be invoked when opened - self._openCallback = callback; + return this._openPromise = new Promise((resolve, reject) => { + // Set the callback that will be invoked once the WS is opened + this._openCallback = err => err ? reject(err) : resolve(); }); } /** @override */ - submit(bytecode, promiseFactory) { - const self = this; - return this.open().then(function () { - return utils.toPromise(promiseFactory, function promiseHandler(callback) { - const requestId = getUuid(); - self._responseHandlers[requestId] = { - callback: callback, - result: null - }; - const message = bufferFromString(self._header + JSON.stringify(self._getRequest(requestId, bytecode))); - self._ws.send(message); - }); - }); + submit(bytecode) { + return this.open().then(() => new Promise((resolve, reject) => { + const requestId = getUuid(); + this._responseHandlers[requestId] = { + callback: (err, result) => err ? reject(err) : resolve(result), + result: null + }; + const message = bufferFromString(this._header + JSON.stringify(this._getRequest(requestId, bytecode))); + this._ws.send(message); + })); } _getRequest(id, bytecode) { @@ -163,17 +154,16 @@ class DriverRemoteConnection extends RemoteConnection { * Closes the Connection. * @return {Promise} */ - close(promiseFactory) { + close() { if (this._closePromise) { return this._closePromise; } - const self = this; - return this._closePromise = utils.toPromise(promiseFactory, function promiseHandler(callback) { - self._ws.on('close', function () { - self.isOpen = false; - callback(); + this._closePromise = new Promise(resolve => { + this._ws.on('close', function () { + this.isOpen = false; + resolve(); }); - self._ws.close(); + this._ws.close(); }); } } http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/af718f7c/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js index 37f3d0e..2672ff4 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js @@ -33,10 +33,9 @@ class RemoteConnection { /** * @abstract * @param {Bytecode} bytecode - * @param {Function|undefined} promiseFactory * @returns {Promise} */ - submit(bytecode, promiseFactory) { + submit(bytecode) { throw new Error('submit() was not implemented'); }; } @@ -60,11 +59,11 @@ class RemoteStrategy extends TraversalStrategy { } /** @override */ - apply(traversal, promiseFactory) { + apply(traversal) { if (traversal.traversers) { - return utils.resolvedPromise(promiseFactory); + return Promise.resolve(); } - return this.connection.submit(traversal.getBytecode(), promiseFactory).then(function (remoteTraversal) { + return this.connection.submit(traversal.getBytecode()).then(function (remoteTraversal) { traversal.sideEffects = remoteTraversal.sideEffects; traversal.traversers = remoteTraversal.traversers; }); http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/af718f7c/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal-strategy.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal-strategy.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal-strategy.js index fbc3fba..d3d97b8 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal-strategy.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal-strategy.js @@ -28,22 +28,16 @@ class TraversalStrategies { /** * Creates a new instance of TraversalStrategies. * @param {TraversalStrategies} [parent] The parent strategies from where to clone the values from. - * @param {Function} [promiseFactory] The factory used to create the A+ Promise instances. Use it when you want to - * create Promise instances without using ECMAScript Promise constructor, ie: bluebird or Q promises. * @constructor */ - constructor(parent, promiseFactory) { + constructor(parent) { if (parent) { // Clone the strategies this.strategies = parent.strategies.slice(0); - this.promiseFactory = parent.promiseFactory; } else { this.strategies = []; } - if (promiseFactory) { - this.promiseFactory = promiseFactory; - } } /** @param {TraversalStrategy} strategy */ @@ -57,12 +51,9 @@ class TraversalStrategies { */ applyStrategies(traversal) { // Apply all strategies serially - const self = this; - return this.strategies.reduce(function reduceItem(promise, strategy) { - return promise.then(function () { - return strategy.apply(traversal, self.promiseFactory); - }); - }, utils.resolvedPromise(this.promiseFactory)); + return this.strategies.reduce((promise, strategy) => { + return promise.then(() => strategy.apply(traversal)); + }, Promise.resolve()); } } @@ -71,10 +62,9 @@ class TraversalStrategy { /** * @abstract * @param {Traversal} traversal - * @param {Function|undefined} promiseFactory * @returns {Promise} */ - apply(traversal, promiseFactory) { + apply(traversal) { } } http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/af718f7c/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/utils.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/utils.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/utils.js index bf87368..c864efd 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/utils.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/utils.js @@ -27,37 +27,6 @@ exports.parseArgs = function parseArgs() { return (arguments.length === 1 ? [ arguments[0] ] : Array.apply(null, arguments)); }; -/** - * @param {Function} handler - * @returns {Promise} - */ -function defaultPromiseFactory(handler) { - return new Promise(function executor(resolve, reject) { - handler(function handlerCallback(err, result) { - if (err) { - return reject(err); - } - resolve(result); - }); - }); -} - -/** - * Gets a resolved Promise instance. - * @param {Function} promiseFactory - * @returns {Promise} - */ -exports.resolvedPromise = function (promiseFactory) { - return toPromise(promiseFactory, function handler(cb) { - cb(); - }); -}; - -const toPromise = exports.toPromise = function toPromise(promiseFactory, handler) { - promiseFactory = promiseFactory || defaultPromiseFactory; - return promiseFactory(handler); -}; - exports.toLong = function toLong(value) { return new Long(value); }; http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/af718f7c/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js index 9310143..130a218 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js @@ -25,7 +25,6 @@ const assert = require('assert'); const expect = require('chai').expect; const graph = require('../../lib/structure/graph'); -const utils = require('../../lib/utils'); const t = require('../../lib/process/traversal'); const TraversalStrategies = require('../../lib/process/traversal-strategy').TraversalStrategies; @@ -64,7 +63,7 @@ describe('Traversal', function () { const strategyMock = { apply: function (traversal) { traversal.traversers = [ new t.Traverser(1, 1), new t.Traverser(2, 1) ]; - return utils.resolvedPromise(); + return Promise.resolve(); } }; const strategies = new TraversalStrategies(); @@ -92,7 +91,7 @@ describe('Traversal', function () { const strategyMock = { apply: function (traversal) { traversal.traversers = [ new t.Traverser(1, 2), new t.Traverser(2, 1) ]; - return utils.resolvedPromise(); + return Promise.resolve(); } }; const strategies = new TraversalStrategies(); @@ -128,7 +127,7 @@ describe('Traversal', function () { const strategyMock = { apply: function (traversal) { traversal.traversers = [ new t.Traverser('a', 1), new t.Traverser('b', 1) ]; - return utils.resolvedPromise(); + return Promise.resolve(); } }; const strategies = new TraversalStrategies(); @@ -144,7 +143,7 @@ describe('Traversal', function () { const strategyMock = { apply: function (traversal) { traversal.traversers = []; - return utils.resolvedPromise(); + return Promise.resolve(); } }; const strategies = new TraversalStrategies(); @@ -161,7 +160,7 @@ describe('Traversal', function () { apply: function (traversal) { traversal.traversers = [ new t.Traverser(1, 1), new t.Traverser(2, 3), new t.Traverser(3, 2), new t.Traverser(4, 1) ]; - return utils.resolvedPromise(); + return Promise.resolve(); } }; const strategies = new TraversalStrategies(); @@ -181,7 +180,7 @@ describe('Traversal', function () { apply: function (traversal) { applied = true; traversal.traversers = [ new t.Traverser('a', 1), new t.Traverser('b', 1) ]; - return utils.resolvedPromise(); + return Promise.resolve(); } }; const strategies = new TraversalStrategies();