1- import { type CreateSessionParams , TransportTimeoutError , type TransportRequest , type TransportResponse } from '@metamask/multichain-api-client' ;
21import type { Session , SessionRequest } from '@metamask/mobile-wallet-protocol-core' ;
32import { SessionStore } from '@metamask/mobile-wallet-protocol-core' ;
43import type { DappClient } from '@metamask/mobile-wallet-protocol-dapp-client' ;
5-
6- import { createLogger , type ExtendedTransport , type RPCAPI , type Scope , type SessionData , type StoreAdapter } from '../../../domain' ;
4+ import { type CreateSessionParams , type TransportRequest , type TransportResponse , TransportTimeoutError } from '@metamask/multichain-api-client' ;
75import type { CaipAccountId } from '@metamask/utils' ;
6+ import { createLogger , type ExtendedTransport , type RPCAPI , type Scope , type SessionData , type StoreAdapter } from '../../../domain' ;
87import { addValidAccounts , getOptionalScopes , getValidAccounts , isSameScopesAndAccounts } from '../../utils' ;
98
109const DEFAULT_REQUEST_TIMEOUT = 60 * 1000 ;
@@ -34,6 +33,7 @@ export class MWPTransport implements ExtendedTransport {
3433 private __pendingRequests = new Map < string , PendingRequests > ( ) ;
3534 private notificationCallbacks = new Set < ( data : unknown ) => void > ( ) ;
3635 private currentSessionRequest : SessionRequest | undefined ;
36+ private windowFocusHandler : ( ( ) => void ) | undefined ;
3737
3838 get pendingRequests ( ) {
3939 return this . __pendingRequests ;
@@ -54,7 +54,8 @@ export class MWPTransport implements ExtendedTransport {
5454 ) {
5555 this . dappClient . on ( 'message' , this . handleMessage . bind ( this ) ) ;
5656 if ( typeof window !== 'undefined' && typeof window . addEventListener !== 'undefined' ) {
57- window . addEventListener ( 'focus' , this . onWindowFocus . bind ( this ) ) ;
57+ this . windowFocusHandler = this . onWindowFocus . bind ( this ) ;
58+ window . addEventListener ( 'focus' , this . windowFocusHandler ) ;
5859 }
5960 }
6061
@@ -121,9 +122,9 @@ export class MWPTransport implements ExtendedTransport {
121122 if ( walletSession && options ) {
122123 const currentScopes = Object . keys ( walletSession ?. sessionScopes ?? { } ) as Scope [ ] ;
123124 const proposedScopes = options ?. scopes ?? [ ] ;
124- const proposedCaipAccountIds = options ?. caipAccountIds ?? [ ] ;
125- const hasSameScopesAndAccounts = isSameScopesAndAccounts ( currentScopes , proposedScopes , walletSession , proposedCaipAccountIds ) ;
126- if ( ! hasSameScopesAndAccounts ) {
125+ const proposedCaipAccountIds = options ?. caipAccountIds ?? [ ] ;
126+ const hasSameScopesAndAccounts = isSameScopesAndAccounts ( currentScopes , proposedScopes , walletSession , proposedCaipAccountIds ) ;
127+ if ( ! hasSameScopesAndAccounts ) {
127128 const optionalScopes = addValidAccounts ( getOptionalScopes ( options ?. scopes ?? [ ] ) , getValidAccounts ( options ?. caipAccountIds ?? [ ] ) ) ;
128129 const sessionRequest : CreateSessionParams < RPCAPI > = { optionalScopes } ;
129130 const response = await this . request ( { method : 'wallet_createSession' , params : sessionRequest } ) ;
@@ -169,7 +170,7 @@ export class MWPTransport implements ExtendedTransport {
169170 logger ( 'active session found' , activeSession ) ;
170171 session = activeSession ;
171172 }
172- } catch { }
173+ } catch { }
173174
174175 let timeout : NodeJS . Timeout ;
175176 const connectionPromise = new Promise < void > ( ( resolve , reject ) => {
@@ -229,6 +230,11 @@ export class MWPTransport implements ExtendedTransport {
229230 * Disconnects from the Mobile Wallet Protocol
230231 */
231232 async disconnect ( ) : Promise < void > {
233+ // Clean up window focus event listener
234+ if ( typeof window !== 'undefined' && typeof window . removeEventListener !== 'undefined' && this . windowFocusHandler ) {
235+ window . removeEventListener ( 'focus' , this . windowFocusHandler ) ;
236+ this . windowFocusHandler = undefined ;
237+ }
232238 return this . dappClient . disconnect ( ) ;
233239 }
234240
0 commit comments