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

Commit

Permalink
feat: 在 content script 統一處理 background error
Browse files Browse the repository at this point in the history
  • Loading branch information
henry40408 committed May 14, 2018
1 parent 0687350 commit 1f6ab16
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 35 deletions.
8 changes: 6 additions & 2 deletions app/scripts/background/messageRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ class MessageRouter {
this.github.fetchRateLimitAsync()
}

register (route, fn) {
register (route, fnAsync) {
return this.messageRouter.on(route, async (message) => {
this.log('📣', route, 'called with', message)
return fn(message)
try {
return { status: 200, data: await fnAsync(message) }
} catch (error) {
return { status: 500, error: error.message }
}
})
}

Expand Down
83 changes: 50 additions & 33 deletions app/scripts/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import chunkize from 'lodash/chunk'
import find from 'lodash/find'
import ParseGithubURL from 'parse-github-url'

import { log } from './common'
import { log, logError } from './common'
import UpdateNotification from './components/UpdateNotification'
import StarHOC from './components/StarHOC'

Expand Down Expand Up @@ -52,14 +52,18 @@ async function batchUpdateCountAsync (stars) {
for (let chunk of chunks) {
let tuples = chunk.map(star => star.getTuple())

let { data: tuplesWithStar } = await messageClient.message('/stars/get/batch', { tuples })
try {
let { data: tuplesWithStar } = await messageClient.message('/stars/get/batch', { tuples })

for (let star of chunk) {
let tuple = find(tuplesWithStar, star.getTuple())
star.updateCount(tuple.star)
}
for (let star of chunk) {
let tuple = find(tuplesWithStar, star.getTuple())
star.updateCount(tuple.star)
}

await messageClient.message('/rate-limit')
await messageClient.message('/rate-limit')
} catch (error) {
logError(error)
}
}
}

Expand All @@ -75,14 +79,19 @@ async function isAwesomeListAsync () {
}

let { owner, name } = parsed
let { data: isAwesomeList } = await messageClient.message('/awesome-list/check', { owner, name })

if (isAwesomeList) {
log(`🚨 awesome list ${owner}/${name} detected`)
return true
}
try {
let { data: isAwesomeList } = await messageClient.message('/awesome-list/check', { owner, name })

if (isAwesomeList) {
log(`🚨 awesome list ${owner}/${name} detected`)
return true
}

return false
return false
} catch (error) {
logError(error)
}
}

async function attachStarsOnLinksAsync (links) {
Expand All @@ -107,19 +116,23 @@ async function initForReadmeAsync () {
}

async function initForGithubIssuesAsync () {
let { data: applyOnGithubIssues } = await messageClient.message('/apply-on-github-issues/get')
if (!applyOnGithubIssues) {
return
}
try {
let { data: applyOnGithubIssues } = await messageClient.message('/apply-on-github-issues/get')
if (!applyOnGithubIssues) {
return
}

let isGithubIssues = !!window.location.href.match(GITHUB_ISSUES_URL_PATTERN)
if (!isGithubIssues) {
return
}
let isGithubIssues = !!window.location.href.match(GITHUB_ISSUES_URL_PATTERN)
if (!isGithubIssues) {
return
}

let links = [].slice.call(document.querySelectorAll('.comment-body a'))
let limitedLinks = links.slice(0, GITHUB_ISSUES_LINKS_LIMIT)
await attachStarsOnLinksAsync(limitedLinks)
let links = [].slice.call(document.querySelectorAll('.comment-body a'))
let limitedLinks = links.slice(0, GITHUB_ISSUES_LINKS_LIMIT)
await attachStarsOnLinksAsync(limitedLinks)
} catch (error) {
logError(error)
}
}

async function initAwesomeStarsAsync () {
Expand All @@ -137,15 +150,19 @@ function showUpdateNotification () {
}

async function checkUpdateNotificationSentAsync () {
let { data: updateNotificationSent } = await messageClient.message(
'/update-notification-sent/get'
)

if (!updateNotificationSent) {
showUpdateNotification()
messageClient.message('/update-notification-sent/set', {
updateNotificationSent: true
})
try {
let { data: updateNotificationSent } = await messageClient.message(
'/update-notification-sent/get'
)

if (!updateNotificationSent) {
showUpdateNotification()
messageClient.message('/update-notification-sent/set', {
updateNotificationSent: true
})
}
} catch (error) {
logError(error)
}
}

Expand Down

0 comments on commit 1f6ab16

Please sign in to comment.