Home / Class/ DerivationCache Class — react Architecture

DerivationCache Class — react Architecture

Architecture documentation for the DerivationCache class in ValidateNoDerivedComputationsInEffects_exp.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  d898e8d4_bae5_bd1e_6da8_d32cb643c20a["DerivationCache"]
  a38a9d1f_969c_8056_aa80_6cb5fedba226["ValidateNoDerivedComputationsInEffects_exp.ts"]
  d898e8d4_bae5_bd1e_6da8_d32cb643c20a -->|defined in| a38a9d1f_969c_8056_aa80_6cb5fedba226
  2743f1aa_8811_26e3_a021_346fb59df244["takeSnapshot()"]
  d898e8d4_bae5_bd1e_6da8_d32cb643c20a -->|method| 2743f1aa_8811_26e3_a021_346fb59df244
  c50dbb50_939e_8d12_f49a_d3815a85e074["checkForChanges()"]
  d898e8d4_bae5_bd1e_6da8_d32cb643c20a -->|method| c50dbb50_939e_8d12_f49a_d3815a85e074
  85e01863_7f22_28ac_88d4_1bee2b6ae4a3["snapshot()"]
  d898e8d4_bae5_bd1e_6da8_d32cb643c20a -->|method| 85e01863_7f22_28ac_88d4_1bee2b6ae4a3
  d47181e1_cf8f_56cd_bdd7_4bce6f7d15d9["addDerivationEntry()"]
  d898e8d4_bae5_bd1e_6da8_d32cb643c20a -->|method| d47181e1_cf8f_56cd_bdd7_4bce6f7d15d9
  2141364e_ec22_da13_4057_580069c4b486["isDerivationEqual()"]
  d898e8d4_bae5_bd1e_6da8_d32cb643c20a -->|method| 2141364e_ec22_da13_4057_580069c4b486

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects_exp.ts lines 57–150

class DerivationCache {
  hasChanges: boolean = false;
  cache: Map<IdentifierId, DerivationMetadata> = new Map();
  private previousCache: Map<IdentifierId, DerivationMetadata> | null = null;

  takeSnapshot(): void {
    this.previousCache = new Map();
    for (const [key, value] of this.cache.entries()) {
      this.previousCache.set(key, {
        place: value.place,
        sourcesIds: new Set(value.sourcesIds),
        typeOfValue: value.typeOfValue,
        isStateSource: value.isStateSource,
      });
    }
  }

  checkForChanges(): void {
    if (this.previousCache === null) {
      this.hasChanges = true;
      return;
    }

    for (const [key, value] of this.cache.entries()) {
      const previousValue = this.previousCache.get(key);
      if (
        previousValue === undefined ||
        !this.isDerivationEqual(previousValue, value)
      ) {
        this.hasChanges = true;
        return;
      }
    }

    if (this.cache.size !== this.previousCache.size) {
      this.hasChanges = true;
      return;
    }

    this.hasChanges = false;
  }

  snapshot(): boolean {
    const hasChanges = this.hasChanges;
    this.hasChanges = false;
    return hasChanges;
  }

  addDerivationEntry(
    derivedVar: Place,
    sourcesIds: Set<IdentifierId>,
    typeOfValue: TypeOfValue,
    isStateSource: boolean,
  ): void {
    let finalIsSource = isStateSource;
    if (!finalIsSource) {
      for (const sourceId of sourcesIds) {
        const sourceMetadata = this.cache.get(sourceId);
        if (
          sourceMetadata?.isStateSource &&
          sourceMetadata.place.identifier.name?.kind !== 'named'
        ) {
          finalIsSource = true;
          break;
        }
      }
    }

    this.cache.set(derivedVar.identifier.id, {
      place: derivedVar,
      sourcesIds: sourcesIds,
      typeOfValue: typeOfValue ?? 'ignored',
      isStateSource: finalIsSource,
    });
  }

  private isDerivationEqual(
    a: DerivationMetadata,
    b: DerivationMetadata,
  ): boolean {
    if (a.typeOfValue !== b.typeOfValue) {

Domain

Frequently Asked Questions

What is the DerivationCache class?
DerivationCache is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects_exp.ts.
Where is DerivationCache defined?
DerivationCache is defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects_exp.ts at line 57.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free