<commit_before>import { DocumentNode } from 'graphql';
import {
  DataProxy,
  DataProxyReadQueryOptions,
  DataProxyReadFragmentOptions,
  DataProxyWriteQueryOptions,
  DataProxyWriteFragmentOptions,
} from './proxy';
import { addTypenameToDocument } from '../queries/queryTransform';
import {
  getFragmentQueryDocument,
  getOperationName,
} from '../queries/getFromAST';

export type CacheWrite = {
  dataId: string;
  result: any;
  document: DocumentNode;
  variables?: Object;
};

export abstract class Cache implements DataProxy {
  private addTypename: boolean;
  constructor(addTypename: boolean) {
    this.addTypename = addTypename;
  }

  public abstract reset(): Promise<void>;

  public abstract diffQuery(query: {
    query: DocumentNode;
    variables: any;
    returnPartialData?: boolean;
    previousResult?: any;
    optimistic: boolean;
  }): any;

  public abstract read(query: {
    query: DocumentNode;
    variables: any;
    rootId?: string;
    previousResult?: any;
    optimistic: boolean;
  }): any;

  public readQuery<QueryType>(
    options: DataProxyReadQueryOptions,
    optimistic: boolean = false,
  ): QueryType {
    let query = options.query;
    if (this.addTypename) {
      query = addTypenameToDocument(query);
    }

    return this.read({
      query,
      variables: options.variables,
      optimistic,
    });
  }

  public readFragment<FragmentType>(
    options: DataProxyReadFragmentOptions,
    optimistic: boolean = false,
  ): FragmentType | null {
    let document = getFragmentQueryDocument(
      options.fragment,
      options.fragmentName,
    );
    if (this.addTypename) {
      document = addTypenameToDocument(document);
    }

    return this.read({
      query: document,
      variables: options.variables,
      rootId: options.id,
      optimistic,
    });
  }

  public abstract writeResult(write: CacheWrite): void;

  public writeQuery(options: DataProxyWriteQueryOptions): void {
    let query = options.query;
    if (this.addTypename) {
      query = addTypenameToDocument(query);
    }

    this.writeResult({
      dataId: 'ROOT_QUERY',
      result: options.data,
      document: query,
      variables: options.variables,
    });
  }

  public writeFragment(options: DataProxyWriteFragmentOptions): void {
    let document = getFragmentQueryDocument(
      options.fragment,
      options.fragmentName,
    );
    if (this.addTypename) {
      document = addTypenameToDocument(document);
    }

    this.writeResult({
      dataId: options.id,
      result: options.data,
      document,
      variables: options.variables,
    });
  }

  public abstract removeOptimistic(id: string): void;

  public abstract performTransaction(transaction: (c: Cache) => void): void;
  public abstract recordOptimisticTransaction(
    transaction: (c: Cache) => void,
    id: string,
  ): void;

  public nonOptimisticProxy(): DataProxy {
    const parent = this;
    return {
      readQuery<QueryType>(options: DataProxyReadQueryOptions): QueryType {
        return parent.readQuery(options, true);
      },
      readFragment<FragmentType>(
        options: DataProxyReadFragmentOptions,
      ): FragmentType | null {
        return parent.readFragment(options, true);
      },
      writeQuery(options: DataProxyWriteQueryOptions): void {
        parent.writeQuery(options);
      },
      writeFragment(options: DataProxyWriteFragmentOptions): void {
        parent.writeFragment(options);
      },
    };
  }
}<commit_msg>Remove now unused nonOptimisticProxy<commit_after>import { DocumentNode } from 'graphql';
import {
  DataProxy,
  DataProxyReadQueryOptions,
  DataProxyReadFragmentOptions,
  DataProxyWriteQueryOptions,
  DataProxyWriteFragmentOptions,
} from './proxy';
import { addTypenameToDocument } from '../queries/queryTransform';
import {
  getFragmentQueryDocument,
  getOperationName,
} from '../queries/getFromAST';

export type CacheWrite = {
  dataId: string;
  result: any;
  document: DocumentNode;
  variables?: Object;
};

export abstract class Cache implements DataProxy {
  private addTypename: boolean;
  constructor(addTypename: boolean) {
    this.addTypename = addTypename;
  }

  public abstract reset(): Promise<void>;

  public abstract diffQuery(query: {
    query: DocumentNode;
    variables: any;
    returnPartialData?: boolean;
    previousResult?: any;
    optimistic: boolean;
  }): any;

  public abstract read(query: {
    query: DocumentNode;
    variables: any;
    rootId?: string;
    previousResult?: any;
    optimistic: boolean;
  }): any;

  public readQuery<QueryType>(
    options: DataProxyReadQueryOptions,
    optimistic: boolean = false,
  ): QueryType {
    let query = options.query;
    if (this.addTypename) {
      query = addTypenameToDocument(query);
    }

    return this.read({
      query,
      variables: options.variables,
      optimistic,
    });
  }

  public readFragment<FragmentType>(
    options: DataProxyReadFragmentOptions,
    optimistic: boolean = false,
  ): FragmentType | null {
    let document = getFragmentQueryDocument(
      options.fragment,
      options.fragmentName,
    );
    if (this.addTypename) {
      document = addTypenameToDocument(document);
    }

    return this.read({
      query: document,
      variables: options.variables,
      rootId: options.id,
      optimistic,
    });
  }

  public abstract writeResult(write: CacheWrite): void;

  public writeQuery(options: DataProxyWriteQueryOptions): void {
    let query = options.query;
    if (this.addTypename) {
      query = addTypenameToDocument(query);
    }

    this.writeResult({
      dataId: 'ROOT_QUERY',
      result: options.data,
      document: query,
      variables: options.variables,
    });
  }

  public writeFragment(options: DataProxyWriteFragmentOptions): void {
    let document = getFragmentQueryDocument(
      options.fragment,
      options.fragmentName,
    );
    if (this.addTypename) {
      document = addTypenameToDocument(document);
    }

    this.writeResult({
      dataId: options.id,
      result: options.data,
      document,
      variables: options.variables,
    });
  }

  public abstract removeOptimistic(id: string): void;

  public abstract performTransaction(transaction: (c: Cache) => void): void;
  public abstract recordOptimisticTransaction(
    transaction: (c: Cache) => void,
    id: string,
  ): void;
}
{'fraction_non_alphanumeric': 0.10259618392242728, 'fraction_numerical': 0.0, 'mean_word_length': 3.3063973063973062, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '6391702', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1798, 'n_words': 418}