From 2a0261c7eb4d84eb824d2698e31d69a40e2e1fd8 Mon Sep 17 00:00:00 2001 From: Manfred Cheung Date: Tue, 29 Jun 2021 19:20:45 -0400 Subject: [PATCH] Replaced path with pathRef and added path getter to gremlin state --- src/query/query.ts | 3 ++- src/utils/utils.ts | 31 +++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/query/query.ts b/src/query/query.ts index ef66ed2..7cca246 100644 --- a/src/query/query.ts +++ b/src/query/query.ts @@ -11,7 +11,8 @@ export type Step = [ export interface GremlinState { as?: Map, - path?: IVertexPath, + path?: Array, + pathRef?: IVertexPath, isResult?: boolean, isSuspended?: boolean, } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index a05b77c..7e7e7b3 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -16,11 +16,30 @@ export interface IUtils { export function hydrate(bgraph: IBGraph): void { bgraph.cloneGremlinState = function(state: GremlinState) { const newState: Partial = {}; + Object.defineProperty(newState, 'path', { + get: function (): Array { + if (!this.pathRef) { + return []; + } + + const path = []; + let currentPath = this.pathRef; + while (true) { + path.unshift(currentPath.vertex); + if(currentPath.parent) { + currentPath = currentPath.parent; + } else { + return path; + } + } + }, + }); + if (state?.as) { newState.as = state.as; } - if (state?.path) { - newState.path = state.path; // Point new state to head of path + if (state?.pathRef) { + newState.pathRef = state.pathRef; // Point new state to head of path } return newState as GremlinState; }; @@ -31,15 +50,15 @@ export function hydrate(bgraph: IBGraph): void { bgraph.gotoVertex = function(gremlin: Gremlin, vertex: IVertex): Gremlin { const state = bgraph.cloneGremlinState(gremlin.state); - if (state.path) { + if (state.pathRef) { // Add current vertex to Gremlin's path const newPath = { vertex: gremlin.vertex, - parent: state.path, + parent: state.pathRef, }; - state.path = newPath; + state.pathRef = newPath; } else { - state.path = { + state.pathRef = { vertex: gremlin.vertex, }; }