Skip to content

Commit 115232c

Browse files
committed
优化获取用户设置的逻辑
1 parent dbdfc27 commit 115232c

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/pages/popup.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
getV2P_Settings,
1818
setV2P_Settings,
1919
} from '../services'
20-
import type { StorageItems, Topic } from '../types'
20+
import type { Topic } from '../types'
2121
import { escapeHTML, formatTimestamp, getStorage, isSameDay } from '../utils'
2222
import { calculateLocalStorageSize, formatSizeUnits, isTabId } from './popup.helper'
2323
import type { PopupStorageData, RemoteDataStore } from './popup.type'
@@ -68,8 +68,8 @@ function loadSettings() {
6868
}
6969
})
7070

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

7474
if (api) {
7575
if (api.pat) {
@@ -233,12 +233,12 @@ function initTabs() {
233233
}
234234

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

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

243243
if (dailyInfo?.lastCheckInTime) {
244244
if (isSameDay(dailyInfo.lastCheckInTime, Date.now())) {
@@ -261,8 +261,8 @@ function initTabs() {
261261
}
262262

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

267267
if (api?.pat) {
268268
const loaded = $tabContent.find('.list').length > 0

src/services.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ async function request<Data>(url: string, options?: RequestInit): Promise<DataWr
5757
headers: { Authorization: PAT ? `Bearer ${PAT}` : '', ...options?.headers },
5858
})
5959

60-
const limit = res.headers.get('X-Rate-Limit-Limit')
61-
const reset = res.headers.get('X-Rate-Limit-Reset')
62-
const remaining = res.headers.get('X-Rate-Limit-Remaining')
60+
{
61+
const limit = res.headers.get('X-Rate-Limit-Limit')
62+
const reset = res.headers.get('X-Rate-Limit-Reset')
63+
const remaining = res.headers.get('X-Rate-Limit-Remaining')
6364

64-
chrome.storage.sync.get(StorageKey.API, (result: StorageItems) => {
6565
const api: API_Info = {
66-
pat: result[StorageKey.API]?.pat,
66+
pat: PAT,
6767
limit: limit ? Number(limit) : undefined,
6868
reset: reset ? Number(reset) : undefined,
6969
remaining: remaining ? Number(remaining) : undefined,
7070
}
7171
void chrome.storage.sync.set({ [StorageKey.API]: api })
72-
})
72+
}
7373

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

0 commit comments

Comments
 (0)