Home / Function/ pushTreeContext() — react Function Reference

pushTreeContext() — react Function Reference

Architecture documentation for the pushTreeContext() function in ReactFizzTreeContext.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  16d6dc73_764e_5899_435e_7680626d3203["pushTreeContext()"]
  fe344404_ced0_26ed_f52b_cbbe258bc47a["ReactFizzTreeContext.js"]
  16d6dc73_764e_5899_435e_7680626d3203 -->|defined in| fe344404_ced0_26ed_f52b_cbbe258bc47a
  53e86f7d_ca7a_54c6_33e6_5e6f2fd561f0["getBitLength()"]
  16d6dc73_764e_5899_435e_7680626d3203 -->|calls| 53e86f7d_ca7a_54c6_33e6_5e6f2fd561f0
  style 16d6dc73_764e_5899_435e_7680626d3203 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-server/src/ReactFizzTreeContext.js lines 80–144

export function pushTreeContext(
  baseContext: TreeContext,
  totalChildren: number,
  index: number,
): TreeContext {
  const baseIdWithLeadingBit = baseContext.id;
  const baseOverflow = baseContext.overflow;

  // The leftmost 1 marks the end of the sequence, non-inclusive. It's not part
  // of the id; we use it to account for leading 0s.
  const baseLength = getBitLength(baseIdWithLeadingBit) - 1;
  const baseId = baseIdWithLeadingBit & ~(1 << baseLength);

  const slot = index + 1;
  const length = getBitLength(totalChildren) + baseLength;

  // 30 is the max length we can store without overflowing, taking into
  // consideration the leading 1 we use to mark the end of the sequence.
  if (length > 30) {
    // We overflowed the bitwise-safe range. Fall back to slower algorithm.
    // This branch assumes the length of the base id is greater than 5; it won't
    // work for smaller ids, because you need 5 bits per character.
    //
    // We encode the id in multiple steps: first the base id, then the
    // remaining digits.
    //
    // Each 5 bit sequence corresponds to a single base 32 character. So for
    // example, if the current id is 23 bits long, we can convert 20 of those
    // bits into a string of 4 characters, with 3 bits left over.
    //
    // First calculate how many bits in the base id represent a complete
    // sequence of characters.
    const numberOfOverflowBits = baseLength - (baseLength % 5);

    // Then create a bitmask that selects only those bits.
    const newOverflowBits = (1 << numberOfOverflowBits) - 1;

    // Select the bits, and convert them to a base 32 string.
    const newOverflow = (baseId & newOverflowBits).toString(32);

    // Now we can remove those bits from the base id.
    const restOfBaseId = baseId >> numberOfOverflowBits;
    const restOfBaseLength = baseLength - numberOfOverflowBits;

    // Finally, encode the rest of the bits using the normal algorithm. Because
    // we made more room, this time it won't overflow.
    const restOfLength = getBitLength(totalChildren) + restOfBaseLength;
    const restOfNewBits = slot << restOfBaseLength;
    const id = restOfNewBits | restOfBaseId;
    const overflow = newOverflow + baseOverflow;
    return {
      id: (1 << restOfLength) | id,
      overflow,
    };
  } else {
    // Normal path
    const newBits = slot << baseLength;
    const id = newBits | baseId;
    const overflow = baseOverflow;
    return {
      id: (1 << length) | id,
      overflow,
    };
  }
}

Domain

Subdomains

Frequently Asked Questions

What does pushTreeContext() do?
pushTreeContext() is a function in the react codebase, defined in packages/react-server/src/ReactFizzTreeContext.js.
Where is pushTreeContext() defined?
pushTreeContext() is defined in packages/react-server/src/ReactFizzTreeContext.js at line 80.
What does pushTreeContext() call?
pushTreeContext() calls 1 function(s): getBitLength.

Analyze Your Own Codebase

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

Try Supermodel Free