<commit_before>import { PullRequestStore } from '../pull-request-store'
import { Account } from '../../../models/account'
import { fatalError } from '../../fatal-error'
import { PullRequest } from '../../../models/pull-request'
import { GitHubRepository } from '../../../models/github-repository'

const PullRequestInterval = 1000 * 60 * 3
const StatusInterval = 1000 * 60
const PostPushInterval = 1000 * 30

enum TimeoutHandles {
  PullRequest = 'PullRequestHandle',
  Status = 'StatusHandle',
  PushedPullRequest = 'PushedPullRequestHandle',
}

export class PullRequestUpdater {
  private readonly repository: GitHubRepository
  private readonly account: Account
  private readonly store: PullRequestStore

  private readonly timeoutHandles = new Map<TimeoutHandles, number>()
  private isStopped: boolean = true

  private currentPullRequests: ReadonlyArray<PullRequest> = []

  public constructor(
    repository: GitHubRepository,
    account: Account,
    pullRequestStore: PullRequestStore
  ) {
    this.repository = repository
    this.account = account
    this.store = pullRequestStore
  }

  /** Starts the updater */
  public start() {
    if (!this.isStopped) {
      fatalError(
        'Cannot start the Pull Request Updater that is already running.'
      )

      return
    }

    this.timeoutHandles.set(
      TimeoutHandles.PullRequest,

      window.setTimeout(() => {
        this.store.refreshPullRequests(this.repository, this.account)
      }, PullRequestInterval)
    )

    this.timeoutHandles.set(
      TimeoutHandles.Status,
      window.setTimeout(() => {
        this.store.refreshPullRequestStatuses(this.repository, this.account)
      }, StatusInterval)
    )
  }

  public stop() {
    this.isStopped = true

    for (const timeoutHandle of this.timeoutHandles.values()) {
      window.clearTimeout(timeoutHandle)
    }

    this.timeoutHandles.clear()
  }

  /** Starts fetching the statuses of PRs at an accelerated rate */
  public didPushPullRequest(pullRequest: PullRequest) {
    if (this.currentPullRequests.find(p => p.id === pullRequest.id)) {
      return
    }

    this.currentPullRequests = [...this.currentPullRequests, pullRequest]

    const pushedPRHandle = this.timeoutHandles.get(
      TimeoutHandles.PushedPullRequest
    )
    if (pushedPRHandle) {
      return
    }

    const handle = window.setTimeout(
      () => this.refreshPullRequestStatus(),
      PostPushInterval
    )
    this.timeoutHandles.set(TimeoutHandles.PushedPullRequest, handle)
  }

  private async refreshPullRequestStatus() {
    await this.store.refreshPullRequestStatuses(this.repository, this.account)
    const prStatuses = await this.store.getPullRequestStatuses()

    for (const prStatus of prStatuses) {
      if (
        !prStatus.totalCount ||
        prStatus.state === 'success' ||
        prStatus.state === 'failure'
      ) {
        this.currentPullRequests = this.currentPullRequests.filter(
          p => p.number === prStatus.pullRequestNumber
        )
      }
    }

    if (!this.currentPullRequests.length) {
      const handle = this.timeoutHandles.get(TimeoutHandles.PushedPullRequest)

      if (handle) {
        window.clearTimeout(handle)
        this.timeoutHandles.delete(TimeoutHandles.PushedPullRequest)
      }
    }
  }
}
<commit_msg>Pass event handlers to updater and register with store<commit_after>import { PullRequestStore } from '../pull-request-store'
import { Account } from '../../../models/account'
import { fatalError } from '../../fatal-error'
import { PullRequest } from '../../../models/pull-request'
import { GitHubRepository } from '../../../models/github-repository'

const PullRequestInterval = 1000 * 60 * 3
const StatusInterval = 1000 * 60
const PostPushInterval = 1000 * 30

enum TimeoutHandles {
  PullRequest = 'PullRequestHandle',
  Status = 'StatusHandle',
  PushedPullRequest = 'PushedPullRequestHandle',
}

export class PullRequestUpdater {
  private readonly repository: GitHubRepository
  private readonly account: Account
  private readonly store: PullRequestStore

  private readonly timeoutHandles = new Map<TimeoutHandles, number>()
  private isStopped: boolean = true

  private currentPullRequests: ReadonlyArray<PullRequest> = []

  public constructor(
    repository: GitHubRepository,
    account: Account,
    pullRequestStore: PullRequestStore,
    onError: (error: Error) => void,
    onUpdate: () => void
  ) {
    this.repository = repository
    this.account = account
    this.store = pullRequestStore

    this.store.onDidError(onError)
    this.store.onDidUpdate(onUpdate)
  }

  /** Starts the updater */
  public start() {
    if (!this.isStopped) {
      fatalError(
        'Cannot start the Pull Request Updater that is already running.'
      )

      return
    }

    this.timeoutHandles.set(
      TimeoutHandles.PullRequest,

      window.setTimeout(() => {
        this.store.refreshPullRequests(this.repository, this.account)
      }, PullRequestInterval)
    )

    this.timeoutHandles.set(
      TimeoutHandles.Status,
      window.setTimeout(() => {
        this.store.refreshPullRequestStatuses(this.repository, this.account)
      }, StatusInterval)
    )
  }

  public stop() {
    this.isStopped = true

    for (const timeoutHandle of this.timeoutHandles.values()) {
      window.clearTimeout(timeoutHandle)
    }

    this.timeoutHandles.clear()
  }

  /** Starts fetching the statuses of PRs at an accelerated rate */
  public didPushPullRequest(pullRequest: PullRequest) {
    if (this.currentPullRequests.find(p => p.id === pullRequest.id)) {
      return
    }

    this.currentPullRequests = [...this.currentPullRequests, pullRequest]

    const pushedPRHandle = this.timeoutHandles.get(
      TimeoutHandles.PushedPullRequest
    )
    if (pushedPRHandle) {
      return
    }

    const handle = window.setTimeout(
      () => this.refreshPullRequestStatus(),
      PostPushInterval
    )
    this.timeoutHandles.set(TimeoutHandles.PushedPullRequest, handle)
  }

  private async refreshPullRequestStatus() {
    await this.store.refreshPullRequestStatuses(this.repository, this.account)
    const prStatuses = await this.store.getPullRequestStatuses()

    for (const prStatus of prStatuses) {
      if (
        !prStatus.totalCount ||
        prStatus.state === 'success' ||
        prStatus.state === 'failure'
      ) {
        this.currentPullRequests = this.currentPullRequests.filter(
          p => p.number === prStatus.pullRequestNumber
        )
      }
    }

    if (!this.currentPullRequests.length) {
      const handle = this.timeoutHandles.get(TimeoutHandles.PushedPullRequest)

      if (handle) {
        window.clearTimeout(handle)
        this.timeoutHandles.delete(TimeoutHandles.PushedPullRequest)
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.10649580203269995, 'fraction_numerical': 0.005597289733392252, 'mean_word_length': 3.0731853629274144, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '7053021', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1903, 'n_words': 408}