/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/forms/signals/src/field/metadata.ts
89 строк
3 KB
Matthieu Riegler
fix(forms): allow multiple async validators
09 июл 2026, 19:55
09 июл 2026, 19:55
5cb8c73
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import { computed, runInInjectionContext, ɵRuntimeError as RuntimeError, untracked, ɵisInParamsFunction, ɵsetInParamsFunction, } from '@angular/core'; import {MetadataKey} from '../api/rules/metadata'; import {RuntimeErrorCode} from '../errors'; import type {FieldNode} from './node'; /** * Tracks custom metadata associated with a `FieldNode`. */ export class FieldMetadataState { /** A map of all `MetadataKey` that have been defined for this field. */ private readonly metadata = new Map<MetadataKey<unknown, unknown, unknown>, unknown>(); constructor(private readonly node: FieldNode) {} /** * Force eager creation of managed keys, * as managed keys have a `create` function that needs to run during construction. */ runMetadataCreateLifecycle(): void { if (!this.node.logicNode.logic.hasMetadataKeys()) { return; } const wasInParams = ɵisInParamsFunction(); if (wasInParams) ɵsetInParamsFunction(false); try { untracked(() => runInInjectionContext(this.node.structure.injector, () => { for (const key of this.node.logicNode.logic.getMetadataKeys()) { if (key.create) { const logic = this.node.logicNode.logic.getMetadata(key); const result = key.create!( this.node, computed(() => logic.compute(this.node.context)), ); this.metadata.set(key, result); } } }), ); } finally { if (wasInParams) ɵsetInParamsFunction(true); } } /** Gets the value of an `MetadataKey` for the field. */ get<T>(key: MetadataKey<T, unknown, unknown>): T | undefined { // We create non-managed metadata lazily, the first time they are accessed. if (this.has(key)) { if (!this.metadata.has(key)) { if (key.create) { throw new RuntimeError( RuntimeErrorCode.MANAGED_METADATA_LAZY_CREATION, ngDevMode && 'Managed metadata cannot be created lazily', ); } const logic = this.node.logicNode.logic.getMetadata(key); this.metadata.set( key, computed(() => logic.compute(this.node.context)), ); } } return this.metadata.get(key) as T | undefined; } /** Checks whether the current metadata state has the given metadata key. */ has(key: MetadataKey<any, any, any>): boolean { // Metadata keys get added to the map lazily, on first access, // so we can't rely on checking presence in the metadata map. // Instead we check if there is any logic for the given metadata key. return this.node.logicNode.logic.hasMetadata(key); } }