Skip to content

Commit

Permalink
优化获取用户设置的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Codennnn committed Apr 25, 2023
1 parent dbdfc27 commit 115232c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/pages/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getV2P_Settings,
setV2P_Settings,
} from '../services'
import type { StorageItems, Topic } from '../types'
import type { Topic } from '../types'
import { escapeHTML, formatTimestamp, getStorage, isSameDay } from '../utils'
import { calculateLocalStorageSize, formatSizeUnits, isTabId } from './popup.helper'
import type { PopupStorageData, RemoteDataStore } from './popup.type'
Expand Down Expand Up @@ -68,8 +68,8 @@ function loadSettings() {
}
})

chrome.storage.sync.get(StorageKey.API, (result: StorageItems) => {
const api = result[StorageKey.API]
void getStorage().then((storage) => {
const api = storage[StorageKey.API]

if (api) {
if (api.pat) {
Expand Down Expand Up @@ -233,12 +233,12 @@ function initTabs() {
}

if (tabId === TabId.Feature) {
chrome.storage.sync.get(StorageKey.Daily, (result: StorageItems) => {
const dailyInfo = result[StorageKey.Daily]
const $checkIn = $('.feature-check-in').on('click', () => {
window.open(`${V2EX.Origin}/mission/daily`)
})

const $checkIn = $('.feature-check-in').on('click', () => {
window.open(`${V2EX.Origin}/mission/daily`)
})
void getStorage().then((storage) => {
const dailyInfo = storage[StorageKey.Daily]

if (dailyInfo?.lastCheckInTime) {
if (isSameDay(dailyInfo.lastCheckInTime, Date.now())) {
Expand All @@ -261,8 +261,8 @@ function initTabs() {
}

if (tabId === TabId.Message) {
chrome.storage.sync.get(StorageKey.API, (result: StorageItems) => {
const api = result[StorageKey.API]
void getStorage().then((storage) => {
const api = storage[StorageKey.API]

if (api?.pat) {
const loaded = $tabContent.find('.list').length > 0
Expand Down
12 changes: 6 additions & 6 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ async function request<Data>(url: string, options?: RequestInit): Promise<DataWr
headers: { Authorization: PAT ? `Bearer ${PAT}` : '', ...options?.headers },
})

const limit = res.headers.get('X-Rate-Limit-Limit')
const reset = res.headers.get('X-Rate-Limit-Reset')
const remaining = res.headers.get('X-Rate-Limit-Remaining')
{
const limit = res.headers.get('X-Rate-Limit-Limit')
const reset = res.headers.get('X-Rate-Limit-Reset')
const remaining = res.headers.get('X-Rate-Limit-Remaining')

chrome.storage.sync.get(StorageKey.API, (result: StorageItems) => {
const api: API_Info = {
pat: result[StorageKey.API]?.pat,
pat: PAT,
limit: limit ? Number(limit) : undefined,
reset: reset ? Number(reset) : undefined,
remaining: remaining ? Number(remaining) : undefined,
}
void chrome.storage.sync.set({ [StorageKey.API]: api })
})
}

const resultData: DataWrapper<Data> = await res.json()

Expand Down

0 comments on commit 115232c

Please sign in to comment.