import { QueryTreeNode } from '../query-tree/query-tree-node' ; import { ResultTreeNode } from './result-tree-node' ; import { ResultTreeHandler } from './result-tree-handler' ; import { rgraphql } from 'rgraphql' ; export class PathCursor { public resultHandlers : ResultTreeHandler [ ] = [ ] ; public outOfBounds : boolean | undefined ; constructor ( public qnode , public rnode ) { } public apply ( val ) { if ( this . outOfBounds ) { return ; } let rtn ; let isQnode = ! ! val . queryNodeId ; let isArray = ! ! val . arrayIndex ; let isValue = ! ! val . value ; if ( isQnode ) { let valQnID = val . queryNodeId || 0 ; let nqn = this . qnode . lookupChildByID ( valQnID ) ; if ( ! nqn ) { this . outOfBounds = true ; return ; } this . qnode = nqn ; for ( let child of this . rnode . children ) { if ( child . value . queryNodeId === valQnID ) { rtn = child ; break ; } } } else if ( isArray ) { let valArrIdx = val . arrayIndex || 0 ; for ( let child of this . rnode . children ) { if ( child . value . arrayIndex === valArrIdx ) { rtn = child ; break ; } } } else { rtn = this . rnode ; } if ( ! rtn ) { rtn = new ResultTreeNode ( val ) ; this . rnode . children . push ( rtn ) ; } else if ( isValue ) { rtn . value = val ; } let nextHandlers = [ ] ; for ( let handler of this . resultHandlers ) { if ( ! handler ) { continue ; } let nextHandler = handler ( val ) ; if ( nextHandler ) { nextHandlers . push ( nextHandler ) ; } } this . resultHandlers = nextHandlers ; this . rnode = rtn ; } public clone ( ) { let n = new PathCursor ( this . qnode , this . rnode ) ; n . resultHandlers = this . resultHandlers . slice ( 0 ) ; if ( this . outOfBounds ) { n . outOfBounds = true ; } return n ; } }