@@ -15,6 +15,7 @@ import type { KeypairType } from '@polkadot/util-crypto/types';
15
15
16
16
import { PORT_EXTENSION } from '@polkadot/extension-base/defaults' ;
17
17
import { getId } from '@polkadot/extension-base/utils/getId' ;
18
+ import { ensurePortConnection } from '@polkadot/extension-base/utils/portUtils' ;
18
19
import { metadataExpand } from '@polkadot/extension-chains' ;
19
20
20
21
import allChains from './util/chains.js' ;
@@ -30,41 +31,11 @@ interface Handler {
30
31
31
32
type Handlers = Record < string , Handler > ;
32
33
33
- async function wakeupBackground ( ) : Promise < Error | null > {
34
- try {
35
- await chrome . runtime . sendMessage ( { type : 'wakeup' } ) ;
36
-
37
- return null ;
38
- } catch ( cause ) {
39
- return cause instanceof Error ? cause : new Error ( String ( cause ) ) ;
40
- }
41
- }
42
-
43
- async function createPort ( name : string , maxAttempts : number , delayMs : number ) : Promise < chrome . runtime . Port > {
44
- let lastError : Error | null = null ;
45
-
46
- for ( let attempt = 0 ; attempt < maxAttempts ; attempt ++ ) {
47
- const error = await wakeupBackground ( ) ;
48
-
49
- if ( error ) {
50
- lastError = error ;
51
- await new Promise ( ( resolve ) => setTimeout ( resolve , delayMs ) ) ;
52
- continue ;
53
- }
54
-
55
- const port = chrome . runtime . connect ( { name } ) ;
56
-
57
- return port ;
58
- }
59
-
60
- throw new Error ( 'Failed to create port after multiple attempts' , { cause : lastError } ) ;
61
- }
62
-
63
- const port = await createPort ( PORT_EXTENSION , 5 , 1000 ) ;
64
34
const handlers : Handlers = { } ;
65
35
66
- // setup a listener for messages, any incoming resolves the promise
67
- port . onMessage . addListener ( ( data : Message [ 'data' ] ) : void => {
36
+ let port : chrome . runtime . Port | undefined ;
37
+
38
+ function onPortMessageHandler ( data : Message [ 'data' ] ) : void {
68
39
const handler = handlers [ data . id ] ;
69
40
70
41
if ( ! handler ) {
@@ -85,7 +56,17 @@ port.onMessage.addListener((data: Message['data']): void => {
85
56
} else {
86
57
handler . resolve ( data . response ) ;
87
58
}
88
- } ) ;
59
+ }
60
+
61
+ function onPortDisconnectHandler ( ) : void {
62
+ port = undefined ;
63
+ }
64
+
65
+ const portConfig = {
66
+ onPortDisconnectHandler,
67
+ onPortMessageHandler,
68
+ portName : PORT_EXTENSION
69
+ } ;
89
70
90
71
function sendMessage < TMessageType extends MessageTypesWithNullRequest > ( message : TMessageType ) : Promise < ResponseTypes [ TMessageType ] > ;
91
72
function sendMessage < TMessageType extends MessageTypesWithNoSubscriptions > ( message : TMessageType , request : RequestTypes [ TMessageType ] ) : Promise < ResponseTypes [ TMessageType ] > ;
@@ -96,7 +77,13 @@ function sendMessage<TMessageType extends MessageTypes> (message: TMessageType,
96
77
97
78
handlers [ id ] = { reject, resolve, subscriber } ;
98
79
99
- port . postMessage ( { id, message, request : request || { } } ) ;
80
+ ensurePortConnection ( port , portConfig ) . then ( ( connectedPort ) => {
81
+ connectedPort . postMessage ( { id, message, request : request || { } } ) ;
82
+ port = connectedPort ;
83
+ } ) . catch ( ( error ) => {
84
+ console . error ( `Failed to send message: ${ ( error as Error ) . message } ` ) ;
85
+ reject ( error ) ;
86
+ } ) ;
100
87
} ) ;
101
88
}
102
89
0 commit comments