44 * you may not use this file except in compliance with the Elastic License.
55 */
66
7+ /* eslint-disable no-shadow */
8+
79import { uniquePidForProcess , uniqueParentPidForProcess , orderByTime } from '../process_event' ;
810import { IndexedProcessTree } from '../../types' ;
911import { ResolverEvent } from '../../../../common/endpoint/types' ;
@@ -27,12 +29,12 @@ export function factory(
2729 const uniqueParentPid = uniqueParentPidForProcess ( process ) ;
2830 // if its defined and not ''
2931 if ( uniqueParentPid ) {
30- let siblings = idToChildren . get ( uniqueParentPid ) ;
31- if ( ! siblings ) {
32- siblings = [ ] ;
33- idToChildren . set ( uniqueParentPid , siblings ) ;
32+ let childrenWithTheSameParent = idToChildren . get ( uniqueParentPid ) ;
33+ if ( ! childrenWithTheSameParent ) {
34+ childrenWithTheSameParent = [ ] ;
35+ idToChildren . set ( uniqueParentPid , childrenWithTheSameParent ) ;
3436 }
35- siblings . push ( process ) ;
37+ childrenWithTheSameParent . push ( process ) ;
3638 }
3739 }
3840
@@ -50,9 +52,8 @@ export function factory(
5052/**
5153 * Returns an array with any children `ProcessEvent`s of the passed in `process`
5254 */
53- export function children ( tree : IndexedProcessTree , process : ResolverEvent ) : ResolverEvent [ ] {
54- const id = uniquePidForProcess ( process ) ;
55- const currentProcessSiblings = tree . idToChildren . get ( id ) ;
55+ export function children ( tree : IndexedProcessTree , parentID : string | undefined ) : ResolverEvent [ ] {
56+ const currentProcessSiblings = tree . idToChildren . get ( parentID ) ;
5657 return currentProcessSiblings === undefined ? [ ] : currentProcessSiblings ;
5758}
5859
@@ -81,26 +82,21 @@ export function parent(
8182/**
8283 * Returns the following sibling
8384 */
84- export function nextSibling (
85- tree : IndexedProcessTree ,
86- sibling : ResolverEvent
87- ) : ResolverEvent | undefined {
88- const parentNode = parent ( tree , sibling ) ;
89- if ( parentNode ) {
90- // The siblings of `sibling` are the children of its parent.
91- const siblings = children ( tree , parentNode ) ;
85+ export function siblings ( tree : IndexedProcessTree , node : ResolverEvent ) : ResolverEvent [ ] {
86+ // this can be undefined, since a node may have no parent.
87+ const parentID : string | undefined = uniqueParentPidForProcess ( node ) ;
9288
93- // Find the sibling
94- const index = siblings . indexOf ( sibling ) ;
89+ // nodes with the same parent ID.
90+ // if `node` has no parent ID, this is nodes with no parent ID.
91+ const childrenWithTheSameParent : undefined | ResolverEvent [ ] = tree . idToChildren . get ( parentID ) ;
9592
96- // if the sibling wasn't found, or if it was the last element in the array, return undefined
97- if ( index === - 1 || index === siblings . length - 1 ) {
98- return undefined ;
99- }
100-
101- // return the next sibling
102- return siblings [ index + 1 ] ;
93+ // this shouldn't happen if the node was in `tree`.
94+ if ( ! childrenWithTheSameParent ) {
95+ return [ ] ;
10396 }
97+
98+ // Return all children with the same parent as `node`, except `node` itself.
99+ return [ ...childrenWithTheSameParent . filter ( ( child ) => child !== node ) ] ;
104100}
105101
106102/**
@@ -133,6 +129,8 @@ export function root(tree: IndexedProcessTree) {
133129export function * levelOrder ( tree : IndexedProcessTree ) {
134130 const rootNode = root ( tree ) ;
135131 if ( rootNode !== null ) {
136- yield * baseLevelOrder ( rootNode , children . bind ( null , tree ) ) ;
132+ yield * baseLevelOrder ( rootNode , ( parentNode : ResolverEvent ) : ResolverEvent [ ] =>
133+ children ( tree , uniquePidForProcess ( parentNode ) )
134+ ) ;
137135 }
138136}
0 commit comments