From 1b943e338d3bca2efb81a0edd6612c7adfdefe60 Mon Sep 17 00:00:00 2001 From: AnhMTV Date: Thu, 23 May 2024 20:33:07 +0700 Subject: [PATCH] Update OpenBit First Install Page problems --- packages/extension-base/src/background/types.ts | 1 + .../src/koni/background/handlers/Extension.ts | 3 ++- .../src/koni/background/handlers/State.ts | 10 ++++++++-- .../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, 25 insertions(+), 21 deletions(-) diff --git a/packages/extension-base/src/background/types.ts b/packages/extension-base/src/background/types.ts index 9a8c3bcecf..7c0130f9f5 100644 --- a/packages/extension-base/src/background/types.ts +++ b/packages/extension-base/src/background/types.ts @@ -121,6 +121,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 b5f6e6e0aa..c47e077255 100644 --- a/packages/extension-base/src/koni/background/handlers/Extension.ts +++ b/packages/extension-base/src/koni/background/handlers/Extension.ts @@ -573,7 +573,6 @@ export default class KoniExtension { const keyringService = this.#koniState.keyringService; const transformedAccountProxies = transformAccountProxies(keyringService.accounts); - // @ts-ignore const responseData: AccountProxiesWithCurrentProxy = { accountProxies: transformedAccountProxies?.length ? [{ ...ACCOUNT_PROXY_ALL_JSON }, ...transformedAccountProxies] : [], currentAccountProxyId: keyringService.currentAccountProxy?.proxyId @@ -4921,6 +4920,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 78d32f6ed9..ed7e9c5bed 100644 --- a/packages/extension-base/src/koni/background/handlers/State.ts +++ b/packages/extension-base/src/koni/background/handlers/State.ts @@ -1693,6 +1693,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) { @@ -1792,7 +1797,7 @@ export default class KoniState { this.waitSleeping = null; } - private async _start (isWakeup = false) { + private async _start () { // Wait sleep finish before start to avoid conflict this.generalStatus === ServiceStatus.STOPPING && this.waitSleeping && await this.waitSleeping; @@ -1808,6 +1813,7 @@ export default class KoniState { return; } + const isWakeup = this.generalStatus === ServiceStatus.STOPPED; const starting = createPromiseHandler(); this.generalStatus = ServiceStatus.STARTING; @@ -1829,7 +1835,7 @@ export default class KoniState { } public async wakeup () { - await this._start(true); + await this._start(); } public cancelSubscription (id: string): boolean { 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 2c43f09291..b237ccc89d 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 1796530690..6873ae713c 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 { getLogoMaps, subscribeAccountProxiesData, subscribeAddressBook, subscribeAssetRegistry, subscribeAssetSettings, subscribeAuthorizeRequests, subscribeBalance, subscribeBitcoinConfirmationRequests, subscribeBuyServices, subscribeBuyTokens, subscribeChainInfoMap, subscribeChainStakingMetadata, subscribeChainStateMap, subscribeChainStatusMap, subscribeConfirmationRequests, subscribeConnectWCRequests, subscribeCrowdloan, subscribeKeyringState, subscribeMantaPayConfig, subscribeMantaPaySyncingState, subscribeMetadataRequests, subscribeMultiChainAssetMap, subscribeNftCollections, subscribeNftItems, subscribePrice, subscribeProcessingCampaign, subscribeSigningRequests, subscribeStaking, subscribeStakingNominatorMetadata, subscribeStakingReward, subscribeTransactionRequests, subscribeTxHistory, subscribeUiSettings, subscribeWalletConnectSessions, subscribeWCNotSupportRequests, subscribeXcmRefMap } 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 05264faf59..0a4109b7fd 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) {