@@ -127,7 +127,7 @@ export class EagerMapTreeNode<TSchema extends FlexTreeNodeSchema> implements Map
127
127
* Set this node's parentage (see {@link FlexTreeNode.parentField}).
128
128
* @remarks A node may only be adopted to a new parent one time, and only if it was not constructed with a parent.
129
129
*/
130
- public adopt ( parent : MapTreeField , index : number ) : void {
130
+ public adoptBy ( parent : MapTreeField , index : number ) : void {
131
131
assert (
132
132
this . location === undefined ,
133
133
0x98c /* Node may not be adopted if it already has a parent */ ,
@@ -160,19 +160,18 @@ export class EagerMapTreeNode<TSchema extends FlexTreeNodeSchema> implements Map
160
160
const field = this . mapTree . fields . get ( key ) ;
161
161
// Only return the field if it is not empty, in order to fulfill the contract of `tryGetField`.
162
162
if ( field !== undefined && field . length > 0 ) {
163
- return getOrCreateField ( this , key , field , this . schema . getFieldSchema ( key ) ) ;
163
+ return getOrCreateField ( this , key , this . schema . getFieldSchema ( key ) ) ;
164
164
}
165
165
}
166
166
167
167
public getBoxed ( key : string ) : FlexTreeField {
168
168
const fieldKey : FieldKey = brand ( key ) ;
169
- const field = this . mapTree . fields . get ( fieldKey ) ?? [ ] ;
170
- return getOrCreateField ( this , fieldKey , field , this . schema . getFieldSchema ( fieldKey ) ) ;
169
+ return getOrCreateField ( this , fieldKey , this . schema . getFieldSchema ( fieldKey ) ) ;
171
170
}
172
171
173
172
public boxedIterator ( ) : IterableIterator < FlexTreeField > {
174
- return mapIterable ( this . mapTree . fields . entries ( ) , ( [ key , field ] ) =>
175
- getOrCreateField ( this , key , field , this . schema . getFieldSchema ( key ) ) ,
173
+ return mapIterable ( this . mapTree . fields . entries ( ) , ( [ key ] ) =>
174
+ getOrCreateField ( this , key , this . schema . getFieldSchema ( key ) ) ,
176
175
) ;
177
176
}
178
177
@@ -190,7 +189,7 @@ export class EagerMapTreeNode<TSchema extends FlexTreeNodeSchema> implements Map
190
189
191
190
private walkTree ( ) : void {
192
191
for ( const [ key , mapTrees ] of this . mapTree . fields ) {
193
- const field = getOrCreateField ( this , key , mapTrees , this . schema . getFieldSchema ( key ) ) ;
192
+ const field = getOrCreateField ( this , key , this . schema . getFieldSchema ( key ) ) ;
194
193
for ( let index = 0 ; index < field . length ; index ++ ) {
195
194
const child = getOrCreateChild (
196
195
mapTrees [ index ] ?? oob ( ) ,
@@ -336,8 +335,13 @@ class EagerMapTreeLeafNode<TSchema extends LeafNodeSchema>
336
335
337
336
// #region Fields
338
337
338
+ /**
339
+ * A readonly {@link FlexTreeField} which wraps an array of {@link MapTrees}.
340
+ * @remarks Reading data from the MapTreeField will read the corresponding data from the {@link MapTree}s.
341
+ * Create a `MapTreeField` by calling {@link getOrCreateField}.
342
+ */
339
343
interface MapTreeField extends FlexTreeField {
340
- readonly mapTrees : readonly ExclusiveMapTree [ ] ;
344
+ readonly mapTrees : readonly MapTree [ ] ;
341
345
}
342
346
343
347
/**
@@ -372,27 +376,30 @@ class EagerMapTreeField<T extends FlexAllowedTypes> implements MapTreeField {
372
376
public constructor (
373
377
public readonly schema : FlexFieldSchema < FlexFieldKind , T > ,
374
378
public readonly key : FieldKey ,
375
- public readonly parent : MapTreeNode ,
376
- public readonly mapTrees : readonly ExclusiveMapTree [ ] ,
379
+ public readonly parent : EagerMapTreeNode < FlexTreeNodeSchema > ,
377
380
) {
378
381
const fieldKeyCache = getFieldKeyCache ( parent ) ;
379
382
assert ( ! fieldKeyCache . has ( key ) , 0x990 /* A field already exists for the given MapTrees */ ) ;
380
383
fieldKeyCache . set ( key , this ) ;
381
384
382
385
// When this field is created (which only happens one time, because it is cached), all the children become parented for the first time.
383
386
// "Adopt" each child by updating its parent information to point to this field.
384
- for ( const [ i , mapTree ] of mapTrees . entries ( ) ) {
387
+ for ( const [ i , mapTree ] of this . mapTrees . entries ( ) ) {
385
388
const mapTreeNodeChild = nodeCache . get ( mapTree ) ;
386
389
if ( mapTreeNodeChild !== undefined ) {
387
390
assert (
388
391
mapTreeNodeChild . parentField . parent === rootMapTreeField ,
389
392
0x991 /* Node is already parented under a different field */ ,
390
393
) ;
391
- mapTreeNodeChild . adopt ( this , i ) ;
394
+ mapTreeNodeChild . adoptBy ( this , i ) ;
392
395
}
393
396
}
394
397
}
395
398
399
+ public get mapTrees ( ) : readonly ExclusiveMapTree [ ] {
400
+ return this . parent . mapTree . fields . get ( this . key ) ?? [ ] ;
401
+ }
402
+
396
403
public get length ( ) : number {
397
404
return this . mapTrees . length ;
398
405
}
@@ -633,9 +640,8 @@ function createNode<TSchema extends FlexTreeNodeSchema>(
633
640
634
641
/** Creates a field with the given attributes, or returns a cached field if there is one */
635
642
function getOrCreateField (
636
- parent : MapTreeNode ,
643
+ parent : EagerMapTreeNode < FlexTreeNodeSchema > ,
637
644
key : FieldKey ,
638
- mapTrees : readonly ExclusiveMapTree [ ] ,
639
645
schema : FlexFieldSchema ,
640
646
) : EagerMapTreeField < FlexFieldSchema [ "allowedTypes" ] > {
641
647
const cached = getFieldKeyCache ( parent ) . get ( key ) ;
@@ -647,18 +653,18 @@ function getOrCreateField(
647
653
schema . kind . identifier === FieldKinds . required . identifier ||
648
654
schema . kind . identifier === FieldKinds . identifier . identifier
649
655
) {
650
- return new EagerMapTreeRequiredField ( schema , key , parent , mapTrees ) ;
656
+ return new EagerMapTreeRequiredField ( schema , key , parent ) ;
651
657
}
652
658
653
659
if ( schema . kind . identifier === FieldKinds . optional . identifier ) {
654
- return new EagerMapTreeOptionalField ( schema , key , parent , mapTrees ) ;
660
+ return new EagerMapTreeOptionalField ( schema , key , parent ) ;
655
661
}
656
662
657
663
if ( schema . kind . identifier === FieldKinds . sequence . identifier ) {
658
- return new EagerMapTreeSequenceField ( schema , key , parent , mapTrees ) ;
664
+ return new EagerMapTreeSequenceField ( schema , key , parent ) ;
659
665
}
660
666
661
- return new EagerMapTreeField ( schema , key , parent , mapTrees ) ;
667
+ return new EagerMapTreeField ( schema , key , parent ) ;
662
668
}
663
669
664
670
/** Unboxes non-polymorphic leaf nodes to their values, if applicable */
@@ -687,7 +693,7 @@ function unboxedField<TFieldSchema extends FlexFieldSchema>(
687
693
field : EagerMapTreeField < FlexAllowedTypes > ,
688
694
key : FieldKey ,
689
695
mapTree : ExclusiveMapTree ,
690
- parentNode : MapTreeNode ,
696
+ parentNode : EagerMapTreeNode < FlexTreeNodeSchema > ,
691
697
) : FlexTreeUnboxField < TFieldSchema > {
692
698
const fieldSchema = field . schema ;
693
699
const mapTrees =
@@ -710,12 +716,7 @@ function unboxedField<TFieldSchema extends FlexFieldSchema>(
710
716
) as FlexTreeUnboxField < TFieldSchema > ;
711
717
}
712
718
713
- return getOrCreateField (
714
- parentNode ,
715
- key ,
716
- mapTrees ,
717
- fieldSchema ,
718
- ) as FlexTreeUnboxField < TFieldSchema > ;
719
+ return getOrCreateField ( parentNode , key , fieldSchema ) as FlexTreeUnboxField < TFieldSchema > ;
719
720
}
720
721
721
722
// #endregion Caching and unboxing utilities
0 commit comments