MCPcopy Create free account
hub / github.com/angular/angular / computeChildrenMap

Function computeChildrenMap

packages/forms/signals/src/field/structure.ts:298–414  ·  view source on GitHub ↗
(
    value: unknown,
    prevData: ChildrenData | undefined,
    forceMaterialize: boolean,
  )

Source from the content-addressed store, hash-verified

296 }
297
298 private computeChildrenMap(
299 value: unknown,
300 prevData: ChildrenData | undefined,
301 forceMaterialize: boolean,
302 ): ChildrenData | undefined {
303 if (!isObject(value)) {
304 // Non-object values have no children. This short-circuit path makes `childrenMap` fast
305 // for primitive-valued fields.
306 return undefined;
307 }
308
309 // Determine if we actually need to materialize children right now.
310 // If not forced, and NO child has any logic rules, we can safely return `undefined`
311 // to keep instantiation lazy. However, if `prevData` is already defined, we MUST
312 // NOT return `undefined` or we will orphan the already instantiated children.
313 if (!forceMaterialize && prevData === undefined) {
314 // Check if any child of this field has logic rules. This check only needs to run once per
315 // structure since the presence of schema logic rules is static across value changes.
316 if (!(this._anyChildHasLogic ??= this.logic.anyChildHasLogic())) {
317 return undefined;
318 }
319 }
320
321 // Previous `ChildrenData` (immutable). This is also where we first initialize our map if
322 // needed.
323 prevData ??= {
324 byPropertyKey: new Map(),
325 };
326
327 // The next `ChildrenData` object to be returned. Initialized lazily when we know there's
328 // been a structural change to the model.
329 let materializedChildren: MutableChildrenData | undefined;
330
331 const parentIsArray = isArray(value);
332
333 // Remove fields that have disappeared since the last time this map was computed.
334 if (prevData !== undefined) {
335 if (parentIsArray) {
336 materializedChildren = maybeRemoveStaleArrayFields(prevData, value, this.identitySymbol);
337 } else {
338 materializedChildren = maybeRemoveStaleObjectFields(prevData, value);
339 }
340 }
341
342 // Now, go through the values and add any new ones.
343 for (const key of Object.keys(value)) {
344 let trackingKey: TrackingKey | undefined = undefined;
345 const childValue = value[key] as unknown;
346
347 // Fields explicitly set to `undefined` are treated as if they don't exist.
348 // This ensures that `{value: undefined}` and `{}` have the same behavior for their `value`
349 // field.
350 if (childValue === undefined) {
351 // The value might have _become_ `undefined`, so we need to delete it here.
352 if (prevData.byPropertyKey.has(key)) {
353 materializedChildren ??= {...(prevData as MutableChildrenData)};
354 materializedChildren.byPropertyKey.delete(key);
355 }

Callers

nothing calls this directly

Calls 10

isObjectFunction · 0.90
isArrayFunction · 0.90
anyChildHasLogicMethod · 0.65
keysMethod · 0.65
hasMethod · 0.65
setMethod · 0.65
getMethod · 0.65
deleteMethod · 0.45

Tested by

no test coverage detected