性能提升(Angular)

1.Use map to cache validator (validator should be the same of each formgroup in formarray)

  formControlInitByCaps(formControl: AbstractControl, caps: MetaModelEntityModel, forceInit?: boolean, useScale?: boolean) {
      if (caps.cfg || forceInit === true) {
        let validators;
        if (this.attrValidatorFn.has(caps.self)) {
          if (!this.feqAttrValidatorFn.has(caps.self)) {
            //cache the attribute that be used twice, the map will be much more smaller
            this.feqAttrValidatorFn.set(caps.self,this.attrValidatorFn.get(caps.self));
          }
          validators = this.feqAttrValidatorFn.get(caps.self);
        } else {
          validators = this.createValidatorsByCaps(caps, formControl, false, forceInit, useScale);
          /** union type attr which already exists becomes required  */
          if (formControl.value && caps.isUnion()) { validators.push(Validators.required); }
          this.attrValidatorFn.set(caps.self, validators);
        }
        formControl.setValidators(validators);
        formControl.enable({ onlySelf: false, emitEvent: false });

2. Formcontrol's onlySelf , true only update itself, not its parent

       formControl.enable({ onlySelf:false,emitEvent:false});

---> formControl.enable({ onlySelf:true,emitEvent:false});

Change to true, time consume of each control is drop to 0.0xxms from 500ms

3. Update each formcontrol will fire the formArray's distinct validator, only need to run one time when all formcontrol are stable

  private formArrayInit(formArray: FormArray, caps: MetaModelEntityModel, forceInit?: boolean, useScale?: boolean) {
    formArray.setValidators(createDistinctElementsValidator());

    for (const element of formArray.controls) {
      if (element instanceof FormControl) {
        // is a FormControl
        console.log(element, formArray);
        this.formArrayControlInit(caps, element, true, useScale);
      } else if (element instanceof FormGroup) {
        // is a FormGroup
        if (caps.attrs !== undefined) {
          const formGroup: FormGroup = <FormGroup>element;
          this.formGroupInitByCapsImpl(formGroup, caps, forceInit, useScale);
        }
      } else if (element instanceof FormArray) {
        // FIXME: dostolski add handling of FormArray inside formArray
      }
    }
  }

猜你喜欢

转载自blog.csdn.net/yizheng_zeng/article/details/132219556