From 23cc6ba5b7c0787537e0f0e650aa7bf8e25a5a9d Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sat, 28 Mar 2020 22:08:58 +0800 Subject: [PATCH] feat: resolve platform async --- src/content.tsx | 46 ++++++++++++++++++++++-------------------- src/platforms/index.ts | 14 +++++++++++-- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/content.tsx b/src/content.tsx index f4ed9b5..b65f0ab 100644 --- a/src/content.tsx +++ b/src/content.tsx @@ -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(, SideBarElement) } - injectStyles(browser.extension.getURL('content.css')) - - const SideBarElement = document.createElement('div') - document.body.appendChild(SideBarElement) - ReactDOM.render(, SideBarElement) + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', init) + } else { + init() + } } - - if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', init) - } else { - init() - } -} +}) diff --git a/src/platforms/index.ts b/src/platforms/index.ts index fa5f96f..cd54f77 100644 --- a/src/platforms/index.ts +++ b/src/platforms/index.ts @@ -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 { 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(dummyPlatformForTypeSafety, { + get(target, key: keyof Platform) { + return p[key] + }, +}) export const errors = { SERVER_FAULT: 'Server Fault',