From 049e411a30cf77e12212860f509e9cb88046e841 Mon Sep 17 00:00:00 2001 From: AnhMTV Date: Thu, 23 May 2024 20:42:18 +0700 Subject: [PATCH] [Issue-2205] Mv3 Handle ping and optimize startup speed --- packages/extension-base/src/background/types.ts | 1 + .../src/koni/background/handlers/Extension.ts | 2 ++ .../src/koni/background/handlers/State.ts | 5 +++++ .../src/koni/background/handlers/index.ts | 1 + .../src/services/event-service/index.ts | 1 - .../extension-koni-ui/src/contexts/DataContext.tsx | 3 +++ packages/extension-koni-ui/src/messaging/index.ts | 4 ++++ packages/extension-koni/src/background-init.ts | 14 -------------- .../extension-koni/src/helper/ActionHandler.ts | 9 ++++++--- 9 files changed, 22 insertions(+), 18 deletions(-) diff --git a/packages/extension-base/src/background/types.ts b/packages/extension-base/src/background/types.ts index 810a17263f..09e4124d0e 100644 --- a/packages/extension-base/src/background/types.ts +++ b/packages/extension-base/src/background/types.ts @@ -120,6 +120,7 @@ export interface SigningRequest extends ConfirmationRequestBase { // [MessageType]: [RequestType, ResponseType, SubscriptionMessageType?] export interface RequestSignatures extends KoniRequestSignatures { // private/internal requests, i.e. from a popup + 'pri(ping)': [null, string]; 'pri(accounts.create.external)': [RequestAccountCreateExternal, boolean]; 'pri(accounts.create.hardware)': [RequestAccountCreateHardware, boolean]; 'pri(accounts.create.suri)': [RequestAccountCreateSuri, boolean]; diff --git a/packages/extension-base/src/koni/background/handlers/Extension.ts b/packages/extension-base/src/koni/background/handlers/Extension.ts index 35727348c1..694eea34ae 100644 --- a/packages/extension-base/src/koni/background/handlers/Extension.ts +++ b/packages/extension-base/src/koni/background/handlers/Extension.ts @@ -4436,6 +4436,8 @@ export default class KoniExtension { } switch (type) { + case 'pri(ping)': + return 'pong'; /// Clone from PolkadotJs case 'pri(accounts.create.external)': return this.accountsCreateExternal(request as RequestAccountCreateExternal); diff --git a/packages/extension-base/src/koni/background/handlers/State.ts b/packages/extension-base/src/koni/background/handlers/State.ts index 9213e5a960..c6b95f91e2 100644 --- a/packages/extension-base/src/koni/background/handlers/State.ts +++ b/packages/extension-base/src/koni/background/handlers/State.ts @@ -1676,6 +1676,11 @@ export default class KoniState { private async onMV3Install () { await SWStorage.instance.setItem('mv3_migration', 'done'); + + // Open expand page + const url = `${chrome.runtime.getURL('index.html')}#/welcome`; + + withErrorLog(() => chrome.tabs.create({ url })); } public onInstallOrUpdate (details: chrome.runtime.InstalledDetails) { diff --git a/packages/extension-base/src/koni/background/handlers/index.ts b/packages/extension-base/src/koni/background/handlers/index.ts index e04a93ab24..f76503906b 100644 --- a/packages/extension-base/src/koni/background/handlers/index.ts +++ b/packages/extension-base/src/koni/background/handlers/index.ts @@ -53,6 +53,7 @@ export class SWHandler { const isMobile = port.name === PORT_MOBILE; const isExtension = port.name === PORT_EXTENSION; const sender = port.sender; + // console.debug('Handle', message); const from = isExtension ? 'extension' diff --git a/packages/extension-base/src/services/event-service/index.ts b/packages/extension-base/src/services/event-service/index.ts index 544a24f9bf..7957a793aa 100644 --- a/packages/extension-base/src/services/event-service/index.ts +++ b/packages/extension-base/src/services/event-service/index.ts @@ -59,7 +59,6 @@ export class EventService extends EventEmitter { private generateWaitPromise (eventType: T): Promise { return new Promise((resolve) => { this.once(eventType, (isReady) => { - console.log('===LOG generateWaitPromise eventType', eventType); resolve(isReady); }); }); diff --git a/packages/extension-koni-ui/src/contexts/DataContext.tsx b/packages/extension-koni-ui/src/contexts/DataContext.tsx index da51c60c0c..2978f9e4ab 100644 --- a/packages/extension-koni-ui/src/contexts/DataContext.tsx +++ b/packages/extension-koni-ui/src/contexts/DataContext.tsx @@ -1,6 +1,7 @@ // Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { ping } from '@subwallet/extension-koni-ui/messaging'; import { persistor, store, StoreName } from '@subwallet/extension-koni-ui/stores'; import { getMissionPoolData, subscribeAccountsData, subscribeAddressBook, subscribeAssetLogoMaps, subscribeAssetRegistry, subscribeAssetSettings, subscribeAuthorizeRequests, subscribeAuthUrls, subscribeBalance, subscribeBuyServices, subscribeBuyTokens, subscribeChainInfoMap, subscribeChainLogoMaps, subscribeChainStakingMetadata, subscribeChainStateMap, subscribeChainStatusMap, subscribeConfirmationRequests, subscribeConnectWCRequests, subscribeCrowdloan, subscribeKeyringState, subscribeMantaPayConfig, subscribeMantaPaySyncingState, subscribeMetadataRequests, subscribeMultiChainAssetMap, subscribeNftCollections, subscribeNftItems, subscribePrice, subscribeProcessingCampaign, subscribeRewardHistory, subscribeSigningRequests, subscribeStaking, subscribeStakingNominatorMetadata, subscribeStakingReward, subscribeSwapPairs, subscribeTransactionRequests, subscribeTxHistory, subscribeUiSettings, subscribeWalletConnectSessions, subscribeWCNotSupportRequests, subscribeXcmRefMap, subscribeYieldMinAmountPercent, subscribeYieldPoolInfo, subscribeYieldPositionInfo, subscribeYieldReward } from '@subwallet/extension-koni-ui/stores/utils'; import Bowser from 'bowser'; @@ -184,6 +185,8 @@ export function initBasicData () { export const DataContext = React.createContext(_DataContext); export const DataContextProvider = ({ children }: DataContextProviderProps) => { + ping().catch(console.error); + // Init basic data initBasicData(); diff --git a/packages/extension-koni-ui/src/messaging/index.ts b/packages/extension-koni-ui/src/messaging/index.ts index 7e9b322a24..912a07e827 100644 --- a/packages/extension-koni-ui/src/messaging/index.ts +++ b/packages/extension-koni-ui/src/messaging/index.ts @@ -7,6 +7,10 @@ import { CronReloadRequest, Notification, RequestGetTransaction, RequestParseEvm import { SWTransactionResult } from '@subwallet/extension-base/services/transaction-service/types'; import { sendMessage } from '@subwallet/extension-koni-ui/messaging/base'; +export async function ping () { + return sendMessage('pri(ping)', null); +} + export async function windowOpen (params: WindowOpenParams): Promise { return sendMessage('pri(window.open)', params); } diff --git a/packages/extension-koni/src/background-init.ts b/packages/extension-koni/src/background-init.ts index 4adca00c74..80f216f1af 100644 --- a/packages/extension-koni/src/background-init.ts +++ b/packages/extension-koni/src/background-init.ts @@ -24,20 +24,6 @@ chrome.runtime.onConnect.addListener((port): void => { // Open expand page after install chrome.runtime.onInstalled.addListener(function (details) { actionHandler.onInstalled(details); - - if (details.reason === 'install') { - // Add small timeout to avoid unwanted problems with the extension popup in the first time loaded - setTimeout(() => { - try { - // Open expand page - const url = `${chrome.runtime.getURL('index.html')}#/`; - - withErrorLog(() => chrome.tabs.create({ url })); - } catch (e) { - console.error(e); - } - }, 900); - } }); // Setup uninstall URL every background start diff --git a/packages/extension-koni/src/helper/ActionHandler.ts b/packages/extension-koni/src/helper/ActionHandler.ts index ef82a609c7..69906b5db0 100644 --- a/packages/extension-koni/src/helper/ActionHandler.ts +++ b/packages/extension-koni/src/helper/ActionHandler.ts @@ -44,9 +44,11 @@ export class ActionHandler { } public onInstalled (details: chrome.runtime.InstalledDetails): void { - this.waitMainHandler.promise.then((handler) => { + (async () => { + const handler = await this.waitMainHandler.promise; + handler.state.onInstallOrUpdate(details); - }).catch(console.error); + })().catch(console.error); } private _getPortId (port: chrome.runtime.Port): string { @@ -54,6 +56,7 @@ export class ActionHandler { } private async _onPortMessage (port: chrome.runtime.Port, data: TransportRequestMessage, portId: string) { + // console.debug(data.message, data.id, portId); // message and disconnect handlers if (!this.mainHandler) { this.mainHandler = await this.waitMainHandler.promise; @@ -81,7 +84,7 @@ export class ActionHandler { } } - this.mainHandler?.handle(data, port); + this.mainHandler.handle(data, port); } private _onPortDisconnect (port: chrome.runtime.Port, portId: string) {