Home / Class/ LRUCache Class — react Architecture

LRUCache Class — react Architecture

Architecture documentation for the LRUCache class in RunReactCompiler.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  ec73283c_3989_df3b_537d_60a9a8207e43["LRUCache"]
  0e801e17_9dc9_7a83_ce0f_d28b56e090f5["RunReactCompiler.ts"]
  ec73283c_3989_df3b_537d_60a9a8207e43 -->|defined in| 0e801e17_9dc9_7a83_ce0f_d28b56e090f5
  f6593b16_1284_a16d_6084_6080c92965c4["constructor()"]
  ec73283c_3989_df3b_537d_60a9a8207e43 -->|method| f6593b16_1284_a16d_6084_6080c92965c4
  0007f882_7308_290b_3dd1_9555b9f99d4f["get()"]
  ec73283c_3989_df3b_537d_60a9a8207e43 -->|method| 0007f882_7308_290b_3dd1_9555b9f99d4f
  14b7faa8_3b52_8cd7_c4d1_996ed950dc7f["push()"]
  ec73283c_3989_df3b_537d_60a9a8207e43 -->|method| 14b7faa8_3b52_8cd7_c4d1_996ed950dc7f

Relationship Graph

Source Code

packages/eslint-plugin-react-hooks/src/shared/RunReactCompiler.ts lines 265–299

class LRUCache<K, T> {
  // newest at headIdx, then headIdx + 1, ..., tailIdx
  #values: Array<[K, T | Error] | [typeof SENTINEL, void]>;
  #headIdx: number = 0;

  constructor(size: number) {
    this.#values = new Array(size).fill(SENTINEL);
  }

  // gets a value and sets it as "recently used"
  get(key: K): T | null {
    const idx = this.#values.findIndex(entry => entry[0] === key);
    // If found, move to front
    if (idx === this.#headIdx) {
      return this.#values[this.#headIdx][1] as T;
    } else if (idx < 0) {
      return null;
    }

    const entry: [K, T] = this.#values[idx] as [K, T];

    const len = this.#values.length;
    for (let i = 0; i < Math.min(idx, len - 1); i++) {
      this.#values[(this.#headIdx + i + 1) % len] =
        this.#values[(this.#headIdx + i) % len];
    }
    this.#values[this.#headIdx] = entry;
    return entry[1];
  }
  push(key: K, value: T): void {
    this.#headIdx =
      (this.#headIdx - 1 + this.#values.length) % this.#values.length;
    this.#values[this.#headIdx] = [key, value];
  }
}

Domain

Frequently Asked Questions

What is the LRUCache class?
LRUCache is a class in the react codebase, defined in packages/eslint-plugin-react-hooks/src/shared/RunReactCompiler.ts.
Where is LRUCache defined?
LRUCache is defined in packages/eslint-plugin-react-hooks/src/shared/RunReactCompiler.ts at line 265.

Analyze Your Own Codebase

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

Try Supermodel Free