feat: resolve platform async

This commit is contained in:
EnixCoda 2020-03-28 22:08:58 +08:00
parent 85736300d8
commit 23cc6ba5b7
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
2 changed files with 36 additions and 24 deletions

View file

@ -1,33 +1,35 @@
import { withErrorLog } from 'analytics'
import { Gitako } from 'components/Gitako'
import { addMiddleware } from 'driver/connect'
import { platform } from 'platforms'
import { platform, resolvePlatformP } from 'platforms'
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import './content.scss'
if (platform.resolveMeta()) {
addMiddleware(withErrorLog)
resolvePlatformP.then(() => {
if (platform.resolveMeta()) {
addMiddleware(withErrorLog)
function init() {
// injects a copy of stylesheets so that other extensions(e.g. dark reader) could read
function injectStyles(url: string) {
var linkElement = document.createElement('link')
linkElement.rel = 'stylesheet'
linkElement.setAttribute('href', url)
document.head.appendChild(linkElement)
function init() {
// injects a copy of stylesheets so that other extensions(e.g. dark reader) could read
function injectStyles(url: string) {
var linkElement = document.createElement('link')
linkElement.rel = 'stylesheet'
linkElement.setAttribute('href', url)
document.head.appendChild(linkElement)
}
injectStyles(browser.extension.getURL('content.css'))
const SideBarElement = document.createElement('div')
document.body.appendChild(SideBarElement)
ReactDOM.render(<Gitako />, SideBarElement)
}
injectStyles(browser.extension.getURL('content.css'))
const SideBarElement = document.createElement('div')
document.body.appendChild(SideBarElement)
ReactDOM.render(<Gitako />, SideBarElement)
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init)
} else {
init()
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init)
} else {
init()
}
}
})

View file

@ -6,16 +6,26 @@ const hosts: Record<'GitHub' | 'GitLab' | 'Gitee', string[]> = {
GitLab: ['gitlab.com'],
Gitee: ['gitee.com'],
}
function isGitHub() {
return hosts.GitHub.includes(window.location.host)
}
function resolvePlatform(): Platform {
async function resolvePlatform(): Promise<Platform> {
if (isGitHub()) return GitHub
return dummyPlatformForTypeSafety
}
export const platform: Platform = resolvePlatform()
let p: Platform = dummyPlatformForTypeSafety
export const resolvePlatformP = resolvePlatform()
resolvePlatformP.then(platform => (p = platform))
export const platform: Platform = new Proxy<Platform>(dummyPlatformForTypeSafety, {
get(target, key: keyof Platform) {
return p[key]
},
})
export const errors = {
SERVER_FAULT: 'Server Fault',