You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/07/15 23:31:05 UTC

[tinkerpop] 01/02: Added hasNext() for gremlin-javascript

This is an automated email from the ASF dual-hosted git repository.

spmallette pushed a commit to branch TINKERPOP-1921
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit c1a42724f2f2d087773cc72bc880f0bd3879e2eb
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Mon Jul 15 12:20:46 2019 -0400

    Added hasNext() for gremlin-javascript
---
 gremlin-javascript/glv/TraversalSource.template    | 12 ++++++++
 .../gremlin-javascript/lib/process/traversal.js    | 12 ++++++++
 .../test/integration/traversal-test.js             |  9 ++++--
 .../gremlin-javascript/test/unit/traversal-test.js | 36 ++++++++++++++++++++++
 4 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/gremlin-javascript/glv/TraversalSource.template b/gremlin-javascript/glv/TraversalSource.template
index afacce8..772b5a1 100644
--- a/gremlin-javascript/glv/TraversalSource.template
+++ b/gremlin-javascript/glv/TraversalSource.template
@@ -66,6 +66,18 @@ class Traversal {
   };
 
   /**
+   * Determines if there are any more items to iterate from the traversal.
+   * @returns {Promise.<boolean>}
+   */
+   hasNext() {
+     return this._applyStrategies().then(() => {
+       return this.traversers && this.traversers.length > 0 &&
+              this._traversersIteratorIndex < this.traversers.length &&
+              this.traversers[this._traversersIteratorIndex].bulk > 0;
+     });
+   }
+
+  /**
    * Iterates all Traverser instances in the traversal.
    * @returns {Promise}
    */
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js
index bb763a2..a5172e3 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js
@@ -66,6 +66,18 @@ class Traversal {
   };
 
   /**
+   * Determines if there are any more items to iterate from the traversal.
+   * @returns {Promise.<boolean>}
+   */
+   hasNext() {
+     return this._applyStrategies().then(() => {
+       return this.traversers && this.traversers.length > 0 &&
+              this._traversersIteratorIndex < this.traversers.length &&
+              this.traversers[this._traversersIteratorIndex].bulk > 0;
+     });
+   }
+
+  /**
    * Iterates all Traverser instances in the traversal.
    * @returns {Promise}
    */
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js
index e413f21..e68b615 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js
@@ -83,9 +83,12 @@ describe('Traversal', function () {
     it('should submit the traversal and return an iterator', function () {
       var g = traversal().withRemote(connection);
       var t = g.V().count();
-      return t.next()
-        .then(function (item) {
-          assert.ok(item);
+      return t.hasNext()
+        .then(function (more) {
+          assert.ok(more);
+          assert.strictEqual(more, true);
+          return t.next();
+        }).then(function (item) {
           assert.strictEqual(item.done, false);
           assert.strictEqual(typeof item.value, 'number');
           return t.next();
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 7a5d5b3..9e82f9a 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
@@ -59,6 +59,42 @@ describe('Traversal', function () {
     });
   });
 
+  describe('#hasNext()', function() {
+    it('should apply strategies and determine if there is anything left to iterate in the traversal')
+    const strategyMock = {
+      apply: function (traversal) {
+        traversal.traversers = [ new t.Traverser(1, 1), new t.Traverser(2, 1) ];
+        return Promise.resolve();
+      }
+    };
+    const strategies = new TraversalStrategies();
+    strategies.addStrategy(strategyMock);
+    const traversal = new t.Traversal(null, strategies, null);
+    return traversal.hasNext()
+        .then(function (more) {
+          assert.strictEqual(more, true);
+          return traversal.next();
+        })
+        .then(function (item) {
+          assert.strictEqual(item.value, 1);
+          assert.strictEqual(item.done, false);
+          return traversal.next();
+        })
+        .then(function (item) {
+          assert.strictEqual(item.value, 2);
+          assert.strictEqual(item.done, false);
+          return traversal.next();
+        })
+        .then(function (item) {
+          assert.strictEqual(item.value, null);
+          assert.strictEqual(item.done, true);
+          return traversal.hasNext();
+        })
+        .then(function (more) {
+          assert.strictEqual(more, false);
+        });
+  });
+
   describe('#next()', function () {
     it('should apply the strategies and return a Promise with the iterator item', function () {
       const strategyMock = {