{"size": 1136, "ext": "js", "max_stars_repo_path": "src/memoizing-account.js", "max_stars_repo_name": "foxford/idp-client", "max_stars_repo_stars_event_min_datetime": "2018-04-04T14:09:42.000Z", "max_stars_repo_stars_event_max_datetime": "2019-10-02T18:55:27.000Z", "max_issues_repo_path": "src/memoizing-account.js", "max_issues_repo_name": "foxford/idp-client", "max_issues_repo_issues_event_min_datetime": "2019-01-14T18:53:08.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-06T17:34:12.000Z", "max_forks_repo_path": "src/memoizing-account.js", "max_forks_repo_name": "foxford/idp-client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 23, "max_forks_count": null, "avg_line_length": 21.8461538462, "max_line_length": 101, "alphanum_fraction": 0.6241197183}
import { Account } from './account'

export class MemoizingAccount extends Account {
  constructor (...argv) {
    super(...argv)

    this.pendingRequests = new Map([])
  }

  memoizedPromise (label, fn) {
    const store = this.pendingRequests
    const maybePending = store.get(label)

    let nextPromise

    if (!maybePending) {
      nextPromise = fn()
        .then((data) => {
          store.delete(label)

          return data
        })
        .catch(() => {
          store.delete(label)
        })
      store.set(label, nextPromise)

      return nextPromise
    }

    return maybePending
  }

  account (storageLabel = '') {
    const label = storageLabel || this.id

    return this.memoizedPromise(`account_${label}`, () => super.account(storageLabel))
  }

  tokenData (storageLabel) {
    const label = storageLabel || this.id

    return this.memoizedPromise(`tokendata_${label}`, () => super.tokenData(storageLabel))
  }

  revokeRefreshToken (storageLabel) {
    const label = storageLabel || this.id

    return this.memoizedPromise(`revoketoken_${label}`, () => super.revokeRefreshToken(storageLabel))
  }
}
