<commit_before>import { Account } from '../../models/account'
import { PullRequest } from '../../models/pull-request'
import {
  RepositoryWithGitHubRepository,
  isRepositoryWithGitHubRepository,
} from '../../models/repository'
import { PullRequestStore } from '.'
import { PullRequestUpdater } from './helpers/pull-request-updater'
import { RepositoriesStore } from './repositories-store'
import { GitHubRepository } from '../../models/github-repository'

/**
 * One stop shop for all things pull requests.
 *
 * Manages the association between GitHubRepositories and
 * local Repositories. In other words, it's a layer between
 * AppStore and the PullRequestStore + PullRequestUpdaters.
 */
export class PullRequestCoordinator {
  private currentPullRequestUpdater: PullRequestUpdater | null = null
  private repositories: ReadonlyArray<
    RepositoryWithGitHubRepository
  > = new Array<RepositoryWithGitHubRepository>()

  /** Map GitHubRepository database IDs to Pull Request Lists */
  private readonly prCache = new Map<number, ReadonlyArray<PullRequest>>()

  public constructor(
    private readonly pullRequestStore: PullRequestStore,
    private readonly repositoriesStore: RepositoriesStore
  ) {
    // register an update handler for the repositories store
    this.repositoriesStore.onDidUpdate(allRepositories => {
      this.repositories = allRepositories.filter(
        isRepositoryWithGitHubRepository
      )
    })
  }

  /** Register a function to be called when the PullRequestStore updates */
  public onPullRequestsChanged(
    fn: (
      repository: RepositoryWithGitHubRepository,
      pullRequests: ReadonlyArray<PullRequest>
    ) => void
  ) {
    return this.pullRequestStore.onPullRequestsChanged(
      (ghRepo, pullRequests) => {
        // update cache
        if (ghRepo.dbID !== null) {
          this.prCache.set(ghRepo.dbID, pullRequests)
        }

        // find all related repos
        const { matches, forks } = findRepositoriesForGitHubRepository(
          ghRepo,
          this.repositories
        )

        // emit updates for forks
        for (const fork of forks) {
          this.getPullRequestsFor(fork.gitHubRepository).then(prs =>
            fn(fork, [...prs, ...pullRequests])
          )
        }

        // emit updates for matches
        for (const match of matches) {
          fn(match, pullRequests)
        }
      }
    )
  }

  /** Register a function to be called when PullRequestStore emits a loading event */
  public onIsLoadingPullRequests(
    fn: (
      repository: RepositoryWithGitHubRepository,
      isLoadingPullRequests: boolean
    ) => void
  ) {
    return this.pullRequestStore.onIsLoadingPullRequests(
      (ghRepo, pullRequests) => {
        const { matches, forks } = findRepositoriesForGitHubRepository(
          ghRepo,
          this.repositories
        )
        for (const repo of [...matches, ...forks]) {
          fn(repo, pullRequests)
        }
      }
    )
  }

  /** Loads (from remote) all pull requests for the given repository and its upstreams. */
  public refreshPullRequests(
    repository: RepositoryWithGitHubRepository,
    account: Account
  ) {
    if (repository.gitHubRepository.parent !== null) {
      return Promise.all([
        this.pullRequestStore.refreshPullRequests(
          repository.gitHubRepository,
          account
        ),
        this.pullRequestStore.refreshPullRequests(
          repository.gitHubRepository.parent,
          account
        ),
      ])
    } else {
      return this.pullRequestStore.refreshPullRequests(
        repository.gitHubRepository,
        account
      )
    }
  }

  /**
   * Get all Pull Requests that are stored locally for the given Repository
   * (Doesn't load anything from the GitHub API.)
   */
  public async getAllPullRequests(
    repository: RepositoryWithGitHubRepository
  ): Promise<ReadonlyArray<PullRequest>> {
    if (repository.gitHubRepository.parent !== null) {
      const [prs, upstreamPrs] = await Promise.all([
        this.getPullRequestsFor(repository.gitHubRepository),
        this.getPullRequestsFor(repository.gitHubRepository.parent),
      ])
      return [...prs, ...upstreamPrs]
    } else {
      return await this.getPullRequestsFor(repository.gitHubRepository)
    }<commit_msg>simplify promises with await<commit_after>import { Account } from '../../models/account'
import { PullRequest } from '../../models/pull-request'
import {
  RepositoryWithGitHubRepository,
  isRepositoryWithGitHubRepository,
} from '../../models/repository'
import { PullRequestStore } from '.'
import { PullRequestUpdater } from './helpers/pull-request-updater'
import { RepositoriesStore } from './repositories-store'
import { GitHubRepository } from '../../models/github-repository'

/**
 * One stop shop for all things pull requests.
 *
 * Manages the association between GitHubRepositories and
 * local Repositories. In other words, it's a layer between
 * AppStore and the PullRequestStore + PullRequestUpdaters.
 */
export class PullRequestCoordinator {
  private currentPullRequestUpdater: PullRequestUpdater | null = null
  private repositories: ReadonlyArray<
    RepositoryWithGitHubRepository
  > = new Array<RepositoryWithGitHubRepository>()

  /** Map GitHubRepository database IDs to Pull Request Lists */
  private readonly prCache = new Map<number, ReadonlyArray<PullRequest>>()

  public constructor(
    private readonly pullRequestStore: PullRequestStore,
    private readonly repositoriesStore: RepositoriesStore
  ) {
    // register an update handler for the repositories store
    this.repositoriesStore.onDidUpdate(allRepositories => {
      this.repositories = allRepositories.filter(
        isRepositoryWithGitHubRepository
      )
    })
  }

  /** Register a function to be called when the PullRequestStore updates */
  public onPullRequestsChanged(
    fn: (
      repository: RepositoryWithGitHubRepository,
      pullRequests: ReadonlyArray<PullRequest>
    ) => void
  ) {
    return this.pullRequestStore.onPullRequestsChanged(
      (ghRepo, pullRequests) => {
        // update cache
        if (ghRepo.dbID !== null) {
          this.prCache.set(ghRepo.dbID, pullRequests)
        }

        // find all related repos
        const { matches, forks } = findRepositoriesForGitHubRepository(
          ghRepo,
          this.repositories
        )

        // emit updates for forks
        for (const fork of forks) {
          this.getPullRequestsFor(fork.gitHubRepository).then(prs =>
            fn(fork, [...prs, ...pullRequests])
          )
        }

        // emit updates for matches
        for (const match of matches) {
          fn(match, pullRequests)
        }
      }
    )
  }

  /** Register a function to be called when PullRequestStore emits a loading event */
  public onIsLoadingPullRequests(
    fn: (
      repository: RepositoryWithGitHubRepository,
      isLoadingPullRequests: boolean
    ) => void
  ) {
    return this.pullRequestStore.onIsLoadingPullRequests(
      (ghRepo, pullRequests) => {
        const { matches, forks } = findRepositoriesForGitHubRepository(
          ghRepo,
          this.repositories
        )
        for (const repo of [...matches, ...forks]) {
          fn(repo, pullRequests)
        }
      }
    )
  }

  /** Loads (from remote) all pull requests for the given repository and its upstreams. */
  public async refreshPullRequests(
    repository: RepositoryWithGitHubRepository,
    account: Account
  ) {
    await this.pullRequestStore.refreshPullRequests(
      repository.gitHubRepository,
      account
    )
    if (repository.gitHubRepository.parent !== null) {
      await this.pullRequestStore.refreshPullRequests(
        repository.gitHubRepository.parent,
        account
      )
    }
  }

  /**
   * Get all Pull Requests that are stored locally for the given Repository
   * (Doesn't load anything from the GitHub API.)
   */
  public async getAllPullRequests(
    repository: RepositoryWithGitHubRepository
  ): Promise<ReadonlyArray<PullRequest>> {
    if (repository.gitHubRepository.parent !== null) {
      const [prs, upstreamPrs] = await Promise.all([
        this.getPullRequestsFor(repository.gitHubRepository),
        this.getPullRequestsFor(repository.gitHubRepository.parent),
      ])
      return [...prs, ...upstreamPrs]
    } else {
      return await this.getPullRequestsFor(repository.gitHubRepository)
    }
{'fraction_non_alphanumeric': 0.09179201705554897, 'fraction_numerical': 0.0, 'mean_word_length': 2.8381818181818184, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, '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': '6262782', 'n_tokens_mistral': 2334, 'n_tokens_neox': 2281, 'n_words': 567}