<commit_before>import { useCallback, useRef, useEffect, useMemo } from "react";
import ResizeObserverPolyfill from "resize-observer-polyfill";

// these are copied from the ResizeObserverPolyfill type definitions since the type definition
// file doesn't seem to be importing the polyfill which causes compilation errors in other packages.
// I don't really know how to fix it.

interface DOMRectReadOnly {
  readonly x: number;
  readonly y: number;
  readonly width: number;
  readonly height: number;
  readonly top: number;
  readonly right: number;
  readonly bottom: number;
  readonly left: number;
}

interface ResizeObserverCallback {
  (entries: ResizeObserverEntry[], observer: ResizeObserver): void;
}

interface ResizeObserverEntry {
  readonly target: Element;
  readonly contentRect: DOMRectReadOnly;
}

interface ResizeObserver {
  observe(target: Element): void;
  unobserve(target: Element): void;
  disconnect(): void;
}

/**
 * A function that will return the resize observer target element. This
 * should return an HTMLElement or null.
 */
export type GetResizeObserverTarget = () => HTMLElement | null;

/**
 * A resize observer target finder. This can either be a `document.querySeletor` string,
 * an `HTMLElement`, a function that returns an `HTMLElement`, or `null`.
 *
 * Setting this to `null` will result in a "lazy Observer". The observer will not start until it has
 * been updated to be a string or an HTMLElement.
 */
export type ResizeObserverTargetFinder =
  | string
  | HTMLElement
  | GetResizeObserverTarget
  | null;

export interface ElementSize {
  height: number;
  width: number;
  scrollHeight: number;
  scrollWidth: number;
}

export interface ResizeObserverChangeEvent extends ElementSize {
  element: HTMLElement;
}

export type ResizeObserverChangeEventHandler = (
  event: ResizeObserverChangeEvent
) => void;

/**
 * A utility function to get the current resize observer element.
 *
 * @private
 */
export function getResizeObserverTarget(target: ResizeObserverTargetFinder) {
  if (target === null) {
    return target;
  }

  switch (typeof target) {
    case "function":
      return (target as GetResizeObserverTarget)();
    case "string":
      return document.querySelector<HTMLElement>(target);
    default:
      return target as HTMLElement;
  }
}

/**
 * Checks if there has been a size change.
 *
 * @private
 */
export function isSizeChange(
  prevSize: ElementSize | null,
  nextSize: ElementSize,
  disableHeight: boolean,
  disableWidth: boolean
) {
  return (
    !prevSize ||
    (!disableWidth &&
      (prevSize.width !== nextSize.width ||
        prevSize.scrollWidth !== nextSize.scrollWidth)) ||
    (!disableHeight &&
      (prevSize.height !== nextSize.height ||
        prevSize.scrollHeight !== nextSize.scrollHeight))
  );
}

interface MeasureOptions {
  onResize: ResizeObserverChangeEventHandler;
  disableHeight?: boolean;
  disableWidth?: boolean;
  defaultSize?: ElementSize | null;
}

/**
 * Creates a memoized measure function to apply to a ResizeObserver.
 *
 * @private
 */
export function useMeasure({
  disableHeight = false,
  disableWidth = false,
  onResize,
  defaultSize = null,
}: MeasureOptions) {
  const prevSize = useRef<ElementSize | null>(defaultSize);
  const options = useRef({
    disableHeight,
    disableWidth,
    onResize,
  });
  useEffect(() => {
    options.current = {
      disableHeight,
      disableWidth,
      onResize,
    };
  });

  return useCallback<ResizeObserverCallback>(entries => {
    const { onResize, disableHeight, disableWidth } = options.current;
    for (const entry of entries) {
      if (!entry) {
        return;
      }

      const { height, width } = entry.contentRect;
      const { scrollHeight, scrollWidth } = entry.target;
      const nextSize = { height, width, scrollWidth, scrollHeight };
      if (
        isSizeChange(prevSize.current, nextSize, disableHeight, disableWidth)
      ) {<commit_msg>Fixed resize observer tests<commit_after>import { useCallback, useRef, useEffect, useMemo } from "react";
import ResizeObserverPolyfill from "resize-observer-polyfill";

// these are copied from the ResizeObserverPolyfill type definitions since the type definition
// file doesn't seem to be importing the polyfill which causes compilation errors in other packages.
// I don't really know how to fix it.

interface DOMRectReadOnly {
  readonly x: number;
  readonly y: number;
  readonly width: number;
  readonly height: number;
  readonly top: number;
  readonly right: number;
  readonly bottom: number;
  readonly left: number;
}

interface ResizeObserverCallback {
  (entries: ResizeObserverEntry[], observer: ResizeObserver): void;
}

interface ResizeObserverEntry {
  readonly target: Element;
  readonly contentRect: DOMRectReadOnly;
}

interface ResizeObserver {
  observe(target: Element): void;
  unobserve(target: Element): void;
  disconnect(): void;
}

/**
 * A function that will return the resize observer target element. This
 * should return an HTMLElement or null.
 */
export type GetResizeObserverTarget = () => HTMLElement | null;

/**
 * A resize observer target finder. This can either be a `document.querySeletor` string,
 * an `HTMLElement`, a function that returns an `HTMLElement`, or `null`.
 *
 * Setting this to `null` will result in a "lazy Observer". The observer will not start until it has
 * been updated to be a string or an HTMLElement.
 */
export type ResizeObserverTargetFinder =
  | string
  | HTMLElement
  | GetResizeObserverTarget
  | null;

export interface ElementSize {
  height: number;
  width: number;
  scrollHeight: number;
  scrollWidth: number;
}

export interface ResizeObserverChangeEvent extends ElementSize {
  element: HTMLElement;
}

export type ResizeObserverChangeEventHandler = (
  event: ResizeObserverChangeEvent
) => void;

/**
 * A utility function to get the current resize observer element.
 *
 * @private
 */
export function getResizeObserverTarget(target: ResizeObserverTargetFinder) {
  if (target === null) {
    return target;
  }

  switch (typeof target) {
    case "function":
      return (target as GetResizeObserverTarget)();
    case "string":
      return document.querySelector<HTMLElement>(target);
    default:
      return target as HTMLElement;
  }
}

/**
 * Checks if there has been a size change.
 *
 * @private
 */
export function isSizeChange(
  prevSize: ElementSize | null,
  nextSize: ElementSize,
  disableHeight: boolean,
  disableWidth: boolean
) {
  return (
    !prevSize ||
    (!disableWidth &&
      (prevSize.width !== nextSize.width ||
        prevSize.scrollWidth !== nextSize.scrollWidth)) ||
    (!disableHeight &&
      (prevSize.height !== nextSize.height ||
        prevSize.scrollHeight !== nextSize.scrollHeight))
  );
}

interface MeasureOptions {
  onResize: ResizeObserverChangeEventHandler;
  disableHeight?: boolean;
  disableWidth?: boolean;
  defaultSize?: ElementSize | null;
}

/**
 * Creates a memoized measure function to apply to a ResizeObserver.
 *
 * @private
 */
export function useMeasure({
  disableHeight = false,
  disableWidth = false,
  onResize,
  defaultSize = null,
}: MeasureOptions) {
  const prevSize = useRef<ElementSize | null>(defaultSize);
  const options = useRef({
    disableHeight,
    disableWidth,
    onResize,
  });
  useEffect(() => {
    options.current = {
      disableHeight,
      disableWidth,
      onResize,
    };
  });

  return useCallback((entries: ResizeObserverEntry[]) => {
    const { onResize, disableHeight, disableWidth } = options.current;
    for (const entry of entries) {
      if (!entry) {
        return;
      }

      const { height, width } = entry.contentRect;
      const { scrollHeight, scrollWidth } = entry.target;
      const nextSize = { height, width, scrollWidth, scrollHeight };
      if (
        isSizeChange(prevSize.current, nextSize, disableHeight, disableWidth)
      ) {
{'fraction_non_alphanumeric': 0.09733619492488323, 'fraction_numerical': 0.0, 'mean_word_length': 3.7924984875983063, 'pattern_counts': {'":': 4, '<': 8, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5537448', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2230, 'n_words': 708}