Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

Commit

Permalink
fix: awesome list 偵測
Browse files Browse the repository at this point in the history
  • Loading branch information
henry40408 committed May 14, 2018
1 parent 407f5eb commit 0687350
Showing 1 changed file with 19 additions and 32 deletions.
51 changes: 19 additions & 32 deletions app/scripts/background/githubService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import axios from 'axios/index'
import includes from 'lodash/includes'

import DIConstants from '../constants'
import { logError } from '../common'

class GithubService {
AWESOME_LIST_URL = 'https://raw.githubusercontent.com/sindresorhus/awesome/master/readme.md'
Expand Down Expand Up @@ -49,7 +48,7 @@ class GithubService {
if (!awesomeList) {
let response = await axios.get(this.AWESOME_LIST_URL)

awesomeList = await response.text()
awesomeList = response.data

this.cache.set(this.AWESOME_LIST_KEY, awesomeList)
}
Expand All @@ -66,31 +65,25 @@ class GithubService {
let numberFormatter = new Intl.NumberFormat('en-US')
let percentFormatter = new Intl.NumberFormat('en-US', { style: 'percent' })

try {
let response = await this.client.get('/rate_limit')
let { rate: { remaining, limit } } = response.data
let response = await this.client.get('/rate_limit')
let { rate: { remaining, limit } } = response.data

this.log('🚦 rate limit:', { remaining, limit })
this.log('🚦 rate limit:', { remaining, limit })

let title = chrome.i18n.getMessage('menuRateLimit', [
numberFormatter.format(remaining),
numberFormatter.format(limit),
percentFormatter.format(remaining / limit)
])
this.contextMenu.upsert(this.contextMenu.MENU_RATE_LIMIT, { title })
let title = chrome.i18n.getMessage('menuRateLimit', [
numberFormatter.format(remaining),
numberFormatter.format(limit),
percentFormatter.format(remaining / limit)
])
this.contextMenu.upsert(this.contextMenu.MENU_RATE_LIMIT, { title })

return { remaining, limit }
} catch (e) {
logError(e)
return { remaining: -1, limit: -1 }
}
return { remaining, limit }
}

async fetchMultipleStarCountAsync (tuples) {
for (let tuple of tuples) {
let { owner, name } = tuple
let starCount = await this.fetchStarCountAsync(owner, name)
tuple.star = starCount
tuple.star = await this.fetchStarCountAsync(owner, name)
}
return tuples
}
Expand All @@ -103,7 +96,9 @@ class GithubService {
limit === 0 ||
remaining / limit <= this.RATE_LIMIT_THRESHOLD
) {
return -1
throw new Error(
`rate limit ${remaining}/${limit} is below threshold ${this.RATE_LIMIT_THRESHOLD}`
)
}

await this.buildClient()
Expand All @@ -112,18 +107,10 @@ class GithubService {
let repo = this.cache.get(cacheKey)

if (!repo) {
try {
let response = await this.client.get(`/repos/${owner}/${name}`)
repo = response.data
this.log('🌍 fetch repository from Github', repo)
this.cache.set(cacheKey, repo)
} catch (e) {
logError(e)
this.contextMenu.upsert(this.contextMenu.MENU_RATE_LIMIT, {
title: chrome.i18n.getMessage('menuRateLimit', ['?', '?', 'N/A'])
})
return -1
}
let response = await this.client.get(`/repos/${owner}/${name}`)
repo = response.data
this.log('🌍 fetch repository from Github', repo)
this.cache.set(cacheKey, repo)
} else {
this.log('🗄 fetch repository from️ cache', repo)
}
Expand Down

0 comments on commit 0687350

Please sign in to comment.