<commit_before>import { Emitter, Disposable } from 'event-kit'
import Repository from '../../models/repository'
import User from '../../models/user'
import GitHubRepository from '../../models/github-repository'
import API, { getUserForEndpoint } from '../api'
import { GitUserDatabase, IGitUser } from './git-user-database'

/**
 * The store for git users. This is used to match commit authors to GitHub
 * users and avatars.
 */
export default class GitUserStore {
  private readonly emitter = new Emitter()

  private readonly requestsInFlight = new Set<string>()

  private readonly inMemoryCache = new Map<string, IGitUser>()

  private emitQueued = false

  private readonly database: GitUserDatabase

  public constructor(database: GitUserDatabase) {
    this.database = database
  }

  private emitUpdate() {
    if (this.emitQueued) { return }

    this.emitQueued = true

    window.requestAnimationFrame(() => {
      this.emitter.emit('did-update', {})
      this.emitQueued = false
    })
  }

  /** Register a function to be called when the store updates. */
  public onDidUpdate(fn: () => void): Disposable {
    return this.emitter.on('did-update', fn)
  }

  /** Get the cached git user for the repository and email. */
  public getUser(repository: Repository, email: string): IGitUser | null {
    const key = keyForRequest(repository, email)
    const user = this.inMemoryCache.get(key)
    return user ? user : null
  }

  /** Not to be called externally. See `Dispatcher`. */
  public async _loadAndCacheUser(users: ReadonlyArray<User>, repository: Repository, sha: string, email: string) {
    const key = keyForRequest(repository, email)
    if (this.requestsInFlight.has(key)) { return }

    const gitHubRepository = repository.gitHubRepository
    // TODO: Big ol' shrug if there's no GitHub repository. Maybe try Gravatar
    // instead?
    if (!gitHubRepository) {
      return
    }

    const user = getUserForEndpoint(users, gitHubRepository.endpoint)
    // TODO: Same as above. If they aren't logged in, maybe try Gravatar?
    if (!user) {
      return
    }

    this.requestsInFlight.add(key)

    let gitUser: IGitUser | null = await this.database.users.where('[endpoint+email]').equals([ user.endpoint, email ]).limit(1).first()

    // TODO: Invalidate the stored user in the db after ... some reasonable time
    // period.
    if (!gitUser) {<commit_msg>Newlines<commit_after>import { Emitter, Disposable } from 'event-kit'
import Repository from '../../models/repository'
import User from '../../models/user'
import GitHubRepository from '../../models/github-repository'
import API, { getUserForEndpoint } from '../api'
import { GitUserDatabase, IGitUser } from './git-user-database'

/**
 * The store for git users. This is used to match commit authors to GitHub
 * users and avatars.
 */
export default class GitUserStore {
  private readonly emitter = new Emitter()

  private readonly requestsInFlight = new Set<string>()

  private readonly inMemoryCache = new Map<string, IGitUser>()

  private emitQueued = false

  private readonly database: GitUserDatabase

  public constructor(database: GitUserDatabase) {
    this.database = database
  }

  private emitUpdate() {
    if (this.emitQueued) { return }

    this.emitQueued = true

    window.requestAnimationFrame(() => {
      this.emitter.emit('did-update', {})
      this.emitQueued = false
    })
  }

  /** Register a function to be called when the store updates. */
  public onDidUpdate(fn: () => void): Disposable {
    return this.emitter.on('did-update', fn)
  }

  /** Get the cached git user for the repository and email. */
  public getUser(repository: Repository, email: string): IGitUser | null {
    const key = keyForRequest(repository, email)
    const user = this.inMemoryCache.get(key)
    return user ? user : null
  }

  /** Not to be called externally. See `Dispatcher`. */
  public async _loadAndCacheUser(users: ReadonlyArray<User>, repository: Repository, sha: string, email: string) {
    const key = keyForRequest(repository, email)
    if (this.requestsInFlight.has(key)) { return }

    const gitHubRepository = repository.gitHubRepository
    // TODO: Big ol' shrug if there's no GitHub repository. Maybe try Gravatar
    // instead?
    if (!gitHubRepository) {
      return
    }

    const user = getUserForEndpoint(users, gitHubRepository.endpoint)
    // TODO: Same as above. If they aren't logged in, maybe try Gravatar?
    if (!user) {
      return
    }

    this.requestsInFlight.add(key)

    let gitUser: IGitUser | null = await this.database.users.where('[endpoint+email]')
      .equals([ user.endpoint, email ])
      .limit(1)
      .first()

    // TODO: Invalidate the stored user in the db after ... some reasonable time
    // period.
    if (!gitUser) {
{'fraction_non_alphanumeric': 0.11568872243029546, 'fraction_numerical': 0.0004161464835622139, 'mean_word_length': 3.31508078994614, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '5550456', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1344, 'n_words': 440}