Skip to content

Commit

Permalink
perf(app): fetch github starred at background
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed Jul 15, 2023
1 parent cac319b commit 22f5847
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
43 changes: 38 additions & 5 deletions app/extension/chrome/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ let syncMarksSuccessCount = 0
let syncMarksFailCount = 0
let syncGithubSuccessCount = 0
let syncGithubFailCount = 0
let fetchGithubStarredEnd = true
const maxConcurrent = 3 // openai rate limit
const maxGithubPerPage = 100

async function sendSavedStatus(res: SwResponse) {
chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
Expand Down Expand Up @@ -121,7 +123,7 @@ function syncMarksToDB() {
function sendSyncGithubStatus() {
chrome.runtime.sendMessage({
action: 'syncGithubStatus',
syncStatus: { index: githubIdx, count: githubCount, state: stopSyncGithub,
syncStatus: { index: githubIdx, count: githubCount, state: stopSyncGithub, fetchEnd: fetchGithubStarredEnd,
isEnd: syncGithubEnd, successCount: syncGithubSuccessCount, failCount: syncGithubFailCount }
})
}
Expand All @@ -134,7 +136,7 @@ function sendSyncGithubStatusToContent() {
if (tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
action: 'syncGithubStatus',
syncStatus: { index: githubIdx, count: githubCount, state: stopSyncGithub,
syncStatus: { index: githubIdx, count: githubCount, state: stopSyncGithub, fetchEnd: fetchGithubStarredEnd,
isEnd: syncGithubEnd, successCount: syncGithubSuccessCount, failCount: syncGithubFailCount }
})
}
Expand Down Expand Up @@ -248,15 +250,46 @@ chrome.runtime.onMessage.addListener(async (request: ContentRequest, sender, sen
}
}
else if (action === 'syncGithubStarred') {
if (request.syncData) {
githubStarred = request.syncData
githubCount = githubStarred.length
if (request.fetchGithubData) {
const { user: githubUser, token: githubToken } = request.fetchGithubData
githubStarred = []
githubIdx = 0
githubCount = 0
syncGithubCount = 0
syncGithubSuccessCount = 0
syncGithubFailCount = 0
syncGithubEnd = false
stopSyncGithub = false
fetchGithubStarredEnd = false

let api = ''
let page = 1
let starredCountOfPage = 0

do {
if (githubUser)
api = `https://api.github.com/users/LarchLiu/starred?per_page=${maxGithubPerPage}&page=${page}`
else
api = `https://api.github.com/user/starred?per_page=${maxGithubPerPage}&page=${page}`
const res = await fetch(api, {
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${githubToken}`,
'X-GitHub-Api-Version': '2022-11-28'
}
})
if (res.status === 200) {
const starred = await res.json()
starredCountOfPage = starred.length
githubCount += starredCountOfPage
githubStarred = githubStarred.concat(starred.map((item: any) => item.html_url))
}
page++
} while(starredCountOfPage === maxGithubPerPage)
fetchGithubStarredEnd = true
githubCount = githubStarred.length

sendSyncGithubStatus()
syncGithubToDB()
sendResponse({ message: 'handling save to DB' })
}
Expand Down
45 changes: 10 additions & 35 deletions app/extension/chrome/src/popup/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ const syncGithubSuccessCount = ref(0)
const syncGithubFailCount = ref(0)
const syncMarksEnd = ref(false)
const syncGithubEnd = ref(false)
const fetchStarredEnd = ref(true)
const maxGithubPerPage = 100
const fetchGithubStarredEnd = ref(true)
let bookmarks: string[] = []
let githubStarred: string[] = []
let starTimer: NodeJS.Timeout
let bubblyTimer: NodeJS.Timeout
const colorPreset = [
Expand Down Expand Up @@ -185,38 +183,14 @@ async function onGithubSync() {
if (githubToken.value) {
githubCount.value = 0
githubIdx.value = 0
fetchStarredEnd.value = false
githubStarred = []
let api = ''
let page = 1
let starredCountOfPage = 0
do {
if (githubUser.value)
api = `https://api.github.com/users/LarchLiu/starred?per_page=${maxGithubPerPage}&page=${page}`
else
api = `https://api.github.com/user/starred?per_page=${maxGithubPerPage}&page=${page}`
const res = await fetch(api, {
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${githubToken.value}`,
'X-GitHub-Api-Version': '2022-11-28'
}
})
if (res.status === 200) {
const starred = await res.json()
starredCountOfPage = starred.length
githubCount.value += starredCountOfPage
githubStarred = githubStarred.concat(starred.map((item: any) => item.html_url))
}
page++
} while(starredCountOfPage === maxGithubPerPage)
fetchStarredEnd.value = true
fetchGithubStarredEnd.value = false
chrome.runtime.sendMessage(
{
action: 'syncGithubStarred',
syncData: githubStarred,
fetchGithubData: {
token: githubToken.value,
user: githubUser.value,
}
},
)
}
Expand Down Expand Up @@ -307,6 +281,7 @@ onMounted(() => {
syncGithubSuccessCount.value = status.successCount
syncGithubFailCount.value = status.failCount
syncGithubEnd.value = status.isEnd
fetchGithubStarredEnd.value = status.fetchEnd
}
}
})
Expand Down Expand Up @@ -456,7 +431,7 @@ onMounted(() => {
<label class="inline-block h-5" pr-2>{{ t('settings.syncGithub') }}</label>
</div>
<div flex items-center>
<div v-if="fetchStarredEnd && githubIdx < githubCount">
<div v-if="fetchGithubStarredEnd && githubIdx < githubCount">
<div cursor-pointer @click="sendSyncGithubState()">
<img class="hover:bg-#E6E6E6 hover:border hover:rounded-full" :src="stopSyncGithub ? iconPlay : iconStop" height="18">
</div>
Expand All @@ -466,7 +441,7 @@ onMounted(() => {
</div>
</div>
</div>
<div v-if="!fetchStarredEnd" flex items-center>
<div v-if="!fetchGithubStarredEnd" flex items-center>
<div w-full>
<el-progress
:percentage="100"
Expand All @@ -479,7 +454,7 @@ onMounted(() => {
</el-progress>
</div>
</div>
<div v-if="(githubIdx < githubCount || syncGithubEnd) && fetchStarredEnd" flex items-center>
<div v-if="(githubIdx < githubCount || syncGithubEnd) && fetchGithubStarredEnd" flex items-center>
<el-tooltip
effect="dark"
:content="`Total: ${githubCount} Success: ${syncGithubSuccessCount} Fail: ${syncGithubFailCount}`"
Expand Down
2 changes: 2 additions & 0 deletions app/extension/chrome/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface SyncStatus {
isEnd: boolean
successCount: number
failCount: number
fetchEnd: boolean
}

interface ContentRequest {
Expand All @@ -50,6 +51,7 @@ interface ContentRequest {
syncData?: string[]
syncState?: boolean
syncStatus?: SyncStatus
fetchGithubData?: { token: string; user?: string }
}

type ListenerSendResponse = (response: ListenerResponse) => void
Expand Down

1 comment on commit 22f5847

@vercel
Copy link

@vercel vercel bot commented on 22f5847 Jul 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

star-nexus – ./

star-nexus-larchliu.vercel.app
star-nexus-git-main-larchliu.vercel.app
star-nexus.vercel.app

Please sign in to comment.