@@ -56,7 +56,6 @@ export function getCommitTree({
5656 const commitTrees = ( ( rootToCommitTreeMap . get (
5757 rootID ,
5858 ) : any ) : Array < CommitTree > ) ;
59-
6059 if ( commitIndex < commitTrees . length ) {
6160 return commitTrees [ commitIndex ] ;
6261 }
@@ -72,52 +71,46 @@ export function getCommitTree({
7271 }
7372
7473 const { operations} = dataForRoot ;
74+ if ( operations . length <= commitIndex ) {
75+ throw Error (
76+ `getCommitTree(): Invalid commit "${ commitIndex } " for root "${ rootID } ". There are only "${ operations . length } " commits.` ,
77+ ) ;
78+ }
7579
76- // Commits are generated sequentially and cached.
77- // If this is the very first commit, start with the cached snapshot and apply the first mutation.
78- // Otherwise load (or generate) the previous commit and append a mutation to it.
79- if ( commitIndex === 0 ) {
80- const nodes = new Map ( ) ;
81-
82- // Construct the initial tree.
83- recursivelyInitializeTree ( rootID , 0 , nodes , dataForRoot ) ;
80+ let commitTree : CommitTree = ( ( null : any ) : CommitTree ) ;
81+ for ( let index = commitTrees . length ; index <= commitIndex ; index ++ ) {
82+ // Commits are generated sequentially and cached.
83+ // If this is the very first commit, start with the cached snapshot and apply the first mutation.
84+ // Otherwise load (or generate) the previous commit and append a mutation to it.
85+ if ( index === 0 ) {
86+ const nodes = new Map ( ) ;
8487
85- // Mutate the tree
86- if ( operations != null && commitIndex < operations . length ) {
87- const commitTree = updateTree ( { nodes, rootID} , operations [ commitIndex ] ) ;
88+ // Construct the initial tree.
89+ recursivelyInitializeTree ( rootID , 0 , nodes , dataForRoot ) ;
8890
89- if ( __DEBUG__ ) {
90- __printTree ( commitTree ) ;
91- }
91+ // Mutate the tree
92+ if ( operations != null && index < operations . length ) {
93+ commitTree = updateTree ( { nodes , rootID } , operations [ index ] ) ;
9294
93- commitTrees . push ( commitTree ) ;
94- return commitTree ;
95- }
96- } else {
97- const previousCommitTree = getCommitTree ( {
98- commitIndex : commitIndex - 1 ,
99- profilerStore,
100- rootID,
101- } ) ;
95+ if ( __DEBUG__ ) {
96+ __printTree ( commitTree ) ;
97+ }
10298
103- if ( operations != null && commitIndex < operations . length ) {
104- const commitTree = updateTree (
105- previousCommitTree ,
106- operations [ commitIndex ] ,
107- ) ;
99+ commitTrees . push ( commitTree ) ;
100+ }
101+ } else {
102+ const previousCommitTree = commitTrees [ index - 1 ] ;
103+ commitTree = updateTree ( previousCommitTree , operations [ index ] ) ;
108104
109105 if ( __DEBUG__ ) {
110106 __printTree ( commitTree ) ;
111107 }
112108
113109 commitTrees . push ( commitTree ) ;
114- return commitTree ;
115110 }
116111 }
117112
118- throw Error (
119- `getCommitTree(): Unable to reconstruct tree for root "${ rootID } " and commit "${ commitIndex } "` ,
120- ) ;
113+ return commitTree ;
121114}
122115
123116function recursivelyInitializeTree (
0 commit comments