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

Class FieldMetadataState

packages/forms/signals/src/field/metadata.ts:24–90  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22 * Tracks custom metadata associated with a `FieldNode`.
23 */
24export class FieldMetadataState {
25 /** A map of all `MetadataKey` that have been defined for this field. */
26 private readonly metadata = new Map<MetadataKey<unknown, unknown, unknown>, unknown>();
27
28 constructor(private readonly node: FieldNode) {}
29
30 /**
31 * Force eager creation of managed keys,
32 * as managed keys have a `create` function that needs to run during construction.
33 */
34 runMetadataCreateLifecycle(): void {
35 if (!this.node.logicNode.logic.hasMetadataKeys()) {
36 return;
37 }
38
39 const wasInParams = ɵisInParamsFunction();
40 if (wasInParams) ɵsetInParamsFunction(false);
41 try {
42 untracked(() => {
43 for (const key of this.node.logicNode.logic.getMetadataKeys()) {
44 if (key.create) {
45 // Accessing structure.injector creates the field injector, so only do it for managed keys.
46 runInInjectionContext(this.node.structure.injector, () => {
47 const logic = this.node.logicNode.logic.getMetadata(key);
48 const result = key.create!(
49 this.node,
50 computed(() => logic.compute(this.node.context)),
51 );
52 this.metadata.set(key, result);
53 });
54 }
55 }
56 });
57 } finally {
58 if (wasInParams) ɵsetInParamsFunction(true);
59 }
60 }
61
62 /** Gets the value of an `MetadataKey` for the field. */
63 get<T>(key: MetadataKey<T, unknown, unknown>): T | undefined {
64 // We create non-managed metadata lazily, the first time they are accessed.
65 if (this.has(key)) {
66 if (!this.metadata.has(key)) {
67 if (key.create) {
68 throw new RuntimeError(
69 RuntimeErrorCode.MANAGED_METADATA_LAZY_CREATION,
70 ngDevMode && 'Managed metadata cannot be created lazily',
71 );
72 }
73 const logic = this.node.logicNode.logic.getMetadata(key);
74 this.metadata.set(
75 key,
76 computed(() => logic.compute(this.node.context)),
77 );
78 }
79 }
80 return this.metadata.get(key) as T | undefined;
81 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected