@@ -24,15 +24,15 @@ export abstract class NestedView<T extends NestedType> implements View<T> {
24
24
public length : number ;
25
25
public numChildren : number ;
26
26
public childData : Data < any > [ ] ;
27
- protected _childColumns : Vector < any > [ ] ;
27
+ protected _children : Vector < any > [ ] ;
28
28
constructor ( data : Data < T > , children ?: Vector < any > [ ] ) {
29
29
this . length = data . length ;
30
30
this . childData = data . childData ;
31
31
this . numChildren = data . childData . length ;
32
- this . _childColumns = children || new Array ( this . numChildren ) ;
32
+ this . _children = children || new Array ( this . numChildren ) ;
33
33
}
34
34
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;
36
36
}
37
37
public isValid ( ) : boolean {
38
38
return true ;
@@ -52,9 +52,11 @@ export abstract class NestedView<T extends NestedType> implements View<T> {
52
52
}
53
53
protected abstract getNested ( self : NestedView < T > , index : number ) : T [ 'TValue' ] ;
54
54
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 ] ) ) ;
58
60
}
59
61
public * [ Symbol . iterator ] ( ) : IterableIterator < T [ 'TValue' ] > {
60
62
const get = this . getNested ;
@@ -120,14 +122,22 @@ export class DenseUnionView extends UnionView<DenseUnion> {
120
122
121
123
export class StructView extends NestedView < Struct > {
122
124
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 ) ;
124
126
}
125
127
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 ;
127
129
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
+ }
129
135
} 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
+ }
131
141
}
132
142
}
133
143
}
@@ -140,14 +150,22 @@ export class MapView extends NestedView<Map_> {
140
150
( xs [ x . name ] = i ) && xs || xs , Object . create ( null ) ) ;
141
151
}
142
152
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 ) ;
144
154
}
145
155
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 ;
147
157
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
+ }
149
163
} 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
+ }
151
169
}
152
170
}
153
171
}
@@ -160,7 +178,7 @@ export class RowView extends UnionView<SparseUnion> {
160
178
this . length = data . numChildren ;
161
179
}
162
180
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;
164
182
}
165
183
protected getChildValue ( self : RowView , index : number , _typeIds : any , _valueOffsets ?: any ) : any | null {
166
184
const child = self . getChildAt ( index ) ;
0 commit comments