mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
refactor: resolve platform async
This commit is contained in:
parent
23cc6ba5b7
commit
0f1f129c01
3 changed files with 39 additions and 14 deletions
|
|
@ -1,29 +1,49 @@
|
|||
import * as storageHelper from 'utils/storageHelper'
|
||||
import { dummyPlatformForTypeSafety } from './dummyPlatformForTypeSafety'
|
||||
import { GitHub } from './GitHub'
|
||||
|
||||
const hosts: Record<'GitHub' | 'GitLab' | 'Gitee', string[]> = {
|
||||
GitHub: ['github.com'],
|
||||
GitLab: ['gitlab.com'],
|
||||
Gitee: ['gitee.com'],
|
||||
const platformsMap: Record<
|
||||
'GitHub' | 'GitLab' | 'Gitee',
|
||||
{ platform: Platform; hosts: string[] }
|
||||
> = {
|
||||
GitHub: { platform: GitHub, hosts: ['github.com'] },
|
||||
GitLab: { platform: GitHub, hosts: ['gitlab.com'] },
|
||||
Gitee: { platform: GitHub, hosts: ['gitee.com'] },
|
||||
}
|
||||
|
||||
function isGitHub() {
|
||||
return hosts.GitHub.includes(window.location.host)
|
||||
const CustomDomainsStorageKey = 'CUSTOM_DOMAINS'
|
||||
async function loadCustomDomains() {
|
||||
const c = await storageHelper.get([CustomDomainsStorageKey])
|
||||
if (c) {
|
||||
const { [CustomDomainsStorageKey]: customDomains } = c
|
||||
if (customDomains) {
|
||||
Object.keys(customDomains).forEach(domain => {
|
||||
if (domain in platformsMap) {
|
||||
platformsMap[domain as keyof typeof platformsMap].hosts.push(...customDomains[domain])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
loadCustomDomains()
|
||||
|
||||
async function resolvePlatform(): Promise<Platform> {
|
||||
if (isGitHub()) return GitHub
|
||||
for (const { hosts, platform } of Object.values(platformsMap)) {
|
||||
if (hosts.some(host => host === window.location.host)) {
|
||||
return platform
|
||||
}
|
||||
}
|
||||
return dummyPlatformForTypeSafety
|
||||
}
|
||||
|
||||
let p: Platform = dummyPlatformForTypeSafety
|
||||
let $platform: Platform = dummyPlatformForTypeSafety
|
||||
|
||||
export const resolvePlatformP = resolvePlatform()
|
||||
resolvePlatformP.then(platform => (p = platform))
|
||||
resolvePlatformP.then(platform => ($platform = platform))
|
||||
|
||||
export const platform: Platform = new Proxy<Platform>(dummyPlatformForTypeSafety, {
|
||||
get(target, key: keyof Platform) {
|
||||
return p[key]
|
||||
return $platform[key]
|
||||
},
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ export const defaultConfigs: Config = {
|
|||
|
||||
const configKeyArray = Object.values(configKeys)
|
||||
|
||||
function applyDefaultConfigs(configs: Config) {
|
||||
function applyDefaultConfigs(configs: Partial<Config>) {
|
||||
return configKeyArray.reduce((applied, configKey) => {
|
||||
const key = configKey as keyof Config
|
||||
Object.assign(applied, { [key]: key in configs ? configs[key] : defaultConfigs[key] })
|
||||
|
|
@ -44,7 +44,8 @@ function applyDefaultConfigs(configs: Config) {
|
|||
}
|
||||
|
||||
export async function get(): Promise<Config> {
|
||||
return applyDefaultConfigs(await storageHelper.get(configKeyArray))
|
||||
const config = await storageHelper.get<Config>(configKeyArray)
|
||||
return applyDefaultConfigs(config || {})
|
||||
}
|
||||
|
||||
export async function set(partialConfig: Partial<Config>) {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
const localStorage = browser.storage.local
|
||||
|
||||
export function get(mapping: string[] | null): Promise<any> | any {
|
||||
export async function get<
|
||||
T extends {
|
||||
[key: string]: any
|
||||
}
|
||||
>(mapping: string[] | null): Promise<T | void> {
|
||||
try {
|
||||
return localStorage.get(mapping || undefined)
|
||||
return (await localStorage.get(mapping || undefined)) as T
|
||||
} catch (err) {}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue