@@ -24,15 +24,15 @@ export abstract class NestedView<T extends NestedType> implements View<T> {
2424 public length : number ;
2525 public numChildren : number ;
2626 public childData : Data < any > [ ] ;
27- protected _childColumns : Vector < any > [ ] ;
27+ protected _children : Vector < any > [ ] ;
2828 constructor ( data : Data < T > , children ?: Vector < any > [ ] ) {
2929 this . length = data . length ;
3030 this . childData = data . childData ;
3131 this . numChildren = data . childData . length ;
32- this . _childColumns = children || new Array ( this . numChildren ) ;
32+ this . _children = children || new Array ( this . numChildren ) ;
3333 }
3434 public clone ( data : Data < T > ) : this {
35- return new ( < any > this . constructor ) ( data , this . _childColumns ) as this;
35+ return new ( < any > this . constructor ) ( data , this . _children ) as this;
3636 }
3737 public isValid ( ) : boolean {
3838 return true ;
@@ -52,9 +52,11 @@ export abstract class NestedView<T extends NestedType> implements View<T> {
5252 }
5353 protected abstract getNested ( self : NestedView < T > , index : number ) : T [ 'TValue' ] ;
5454 protected abstract setNested ( self : NestedView < T > , index : number , value : T [ 'TValue' ] ) : void ;
55- public getChildAt < R extends DataType = DataType > ( index : number ) {
56- return this . _childColumns [ index ] || (
57- this . _childColumns [ index ] = Vector . create < R > ( this . childData [ index ] ) ) ;
55+ public getChildAt < R extends DataType = DataType > ( index : number ) : Vector < R > | null {
56+ return index < 0 || index >= this . numChildren
57+ ? null
58+ : ( this . _children [ index ] as Vector < R > ) ||
59+ ( this . _children [ index ] = Vector . create < R > ( this . childData [ index ] ) ) ;
5860 }
5961 public * [ Symbol . iterator ] ( ) : IterableIterator < T [ 'TValue' ] > {
6062 const get = this . getNested ;
@@ -120,14 +122,22 @@ export class DenseUnionView extends UnionView<DenseUnion> {
120122
121123export class StructView extends NestedView < Struct > {
122124 protected getNested ( self : StructView , index : number ) {
123- return new RowView ( self as any , self . _childColumns , index ) ;
125+ return new RowView ( self as any , self . _children , index ) ;
124126 }
125127 protected setNested ( self : StructView , index : number , value : any ) : void {
126- let idx = - 1 , len = self . numChildren ;
128+ let idx = - 1 , len = self . numChildren , child : Vector | null ;
127129 if ( ! ( value instanceof NestedView || value instanceof Vector ) ) {
128- while ( ++ idx < len ) { self . getChildAt ( idx ) . set ( index , value [ idx ] ) ; }
130+ while ( ++ idx < len ) {
131+ if ( child = self . getChildAt ( idx ) ) {
132+ child . set ( index , value [ idx ] ) ;
133+ }
134+ }
129135 } else {
130- while ( ++ idx < len ) { self . getChildAt ( idx ) . set ( index , value . get ( idx ) ) ; }
136+ while ( ++ idx < len ) {
137+ if ( child = self . getChildAt ( idx ) ) {
138+ child . set ( index , value . get ( idx ) ) ;
139+ }
140+ }
131141 }
132142 }
133143}
@@ -140,14 +150,22 @@ export class MapView extends NestedView<Map_> {
140150 ( xs [ x . name ] = i ) && xs || xs , Object . create ( null ) ) ;
141151 }
142152 protected getNested ( self : MapView , index : number ) {
143- return new MapRowView ( self as any , self . _childColumns , index ) ;
153+ return new MapRowView ( self as any , self . _children , index ) ;
144154 }
145155 protected setNested ( self : MapView , index : number , value : { [ k : string ] : any } ) : void {
146- const typeIds = self . typeIds as any ;
156+ let typeIds = self . typeIds as any , child : Vector | null ;
147157 if ( ! ( value instanceof NestedView || value instanceof Vector ) ) {
148- for ( const key in typeIds ) { self . getChildAt ( typeIds [ key ] ) . set ( index , value [ key ] ) ; }
158+ for ( const key in typeIds ) {
159+ if ( child = self . getChildAt ( typeIds [ key ] ) ) {
160+ child . set ( index , value [ key ] ) ;
161+ }
162+ }
149163 } else {
150- for ( const key in typeIds ) { self . getChildAt ( typeIds [ key ] ) . set ( index , value . get ( key as any ) ) ; }
164+ for ( const key in typeIds ) {
165+ if ( child = self . getChildAt ( typeIds [ key ] ) ) {
166+ child . set ( index , value . get ( key as any ) ) ;
167+ }
168+ }
151169 }
152170 }
153171}
@@ -160,7 +178,7 @@ export class RowView extends UnionView<SparseUnion> {
160178 this . length = data . numChildren ;
161179 }
162180 public clone ( data : Data < SparseUnion > & NestedView < any > ) : this {
163- return new ( < any > this . constructor ) ( data , this . _childColumns , this . rowIndex ) as this;
181+ return new ( < any > this . constructor ) ( data , this . _children , this . rowIndex ) as this;
164182 }
165183 protected getChildValue ( self : RowView , index : number , _typeIds : any , _valueOffsets ?: any ) : any | null {
166184 const child = self . getChildAt ( index ) ;
0 commit comments