66 type CloudUserInfo ,
77 type ExtensionBridgeCommand ,
88 type TaskBridgeCommand ,
9+ type ExtensionMetadata ,
910 ConnectionState ,
1011 ExtensionSocketEvents ,
1112 TaskSocketEvents ,
@@ -21,6 +22,7 @@ export interface BridgeOrchestratorOptions {
2122 token : string
2223 provider : TaskProviderLike
2324 sessionId ?: string
25+ extensionMetadata : ExtensionMetadata
2426}
2527
2628/**
@@ -39,6 +41,7 @@ export class BridgeOrchestrator {
3941 private readonly token : string
4042 private readonly provider : TaskProviderLike
4143 private readonly instanceId : string
44+ private readonly extensionMetadata : ExtensionMetadata
4245
4346 // Components
4447 private socketTransport : SocketTransport
@@ -61,66 +64,69 @@ export class BridgeOrchestrator {
6164 public static async connectOrDisconnect (
6265 userInfo : CloudUserInfo | null ,
6366 remoteControlEnabled : boolean | undefined ,
64- options ? : BridgeOrchestratorOptions ,
67+ options : BridgeOrchestratorOptions ,
6568 ) : Promise < void > {
66- const isEnabled = BridgeOrchestrator . isEnabled ( userInfo , remoteControlEnabled )
67- const instance = BridgeOrchestrator . instance
69+ if ( BridgeOrchestrator . isEnabled ( userInfo , remoteControlEnabled ) ) {
70+ await BridgeOrchestrator . connect ( options )
71+ } else {
72+ await BridgeOrchestrator . disconnect ( )
73+ }
74+ }
6875
69- if ( isEnabled ) {
70- if ( ! instance ) {
71- if ( ! options ) {
72- console . error (
73- `[BridgeOrchestrator#connectOrDisconnect] Cannot connect: options are required for connection` ,
74- )
75- return
76- }
77- try {
78- console . log ( `[BridgeOrchestrator#connectOrDisconnect] Connecting...` )
79- BridgeOrchestrator . instance = new BridgeOrchestrator ( options )
80- await BridgeOrchestrator . instance . connect ( )
81- } catch ( error ) {
82- console . error (
83- `[BridgeOrchestrator#connectOrDisconnect] connect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
84- )
85- }
86- } else {
87- if (
88- instance . connectionState === ConnectionState . FAILED ||
89- instance . connectionState === ConnectionState . DISCONNECTED
90- ) {
91- console . log (
92- `[BridgeOrchestrator#connectOrDisconnect] Re-connecting... (state: ${ instance . connectionState } )` ,
93- )
76+ public static async connect ( options : BridgeOrchestratorOptions ) {
77+ const instance = BridgeOrchestrator . instance
9478
95- instance . reconnect ( ) . catch ( ( error ) => {
96- console . error (
97- `[BridgeOrchestrator#connectOrDisconnect] reconnect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
98- )
99- } )
100- } else {
101- console . log (
102- `[BridgeOrchestrator#connectOrDisconnect] Already connected or connecting (state: ${ instance . connectionState } )` ,
103- )
104- }
79+ if ( ! instance ) {
80+ try {
81+ console . log ( `[BridgeOrchestrator#connectOrDisconnect] Connecting...` )
82+ BridgeOrchestrator . instance = new BridgeOrchestrator ( options )
83+ await BridgeOrchestrator . instance . connect ( )
84+ } catch ( error ) {
85+ console . error (
86+ `[BridgeOrchestrator#connectOrDisconnect] connect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
87+ )
10588 }
10689 } else {
107- if ( instance ) {
108- try {
109- console . log (
110- `[BridgeOrchestrator#connectOrDisconnect] Disconnecting... (state: ${ instance . connectionState } )` ,
111- )
90+ if (
91+ instance . connectionState === ConnectionState . FAILED ||
92+ instance . connectionState === ConnectionState . DISCONNECTED
93+ ) {
94+ console . log (
95+ `[BridgeOrchestrator#connectOrDisconnect] Re-connecting... (state: ${ instance . connectionState } )` ,
96+ )
11297
113- await instance . disconnect ( )
114- } catch ( error ) {
98+ instance . reconnect ( ) . catch ( ( error ) => {
11599 console . error (
116- `[BridgeOrchestrator#connectOrDisconnect] disconnect () failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
100+ `[BridgeOrchestrator#connectOrDisconnect] reconnect () failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
117101 )
118- } finally {
119- BridgeOrchestrator . instance = null
120- }
102+ } )
121103 } else {
122- console . log ( `[BridgeOrchestrator#connectOrDisconnect] Already disconnected` )
104+ console . log (
105+ `[BridgeOrchestrator#connectOrDisconnect] Already connected or connecting (state: ${ instance . connectionState } )` ,
106+ )
107+ }
108+ }
109+ }
110+
111+ public static async disconnect ( ) {
112+ const instance = BridgeOrchestrator . instance
113+
114+ if ( instance ) {
115+ try {
116+ console . log (
117+ `[BridgeOrchestrator#connectOrDisconnect] Disconnecting... (state: ${ instance . connectionState } )` ,
118+ )
119+
120+ await instance . disconnect ( )
121+ } catch ( error ) {
122+ console . error (
123+ `[BridgeOrchestrator#connectOrDisconnect] disconnect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
124+ )
125+ } finally {
126+ BridgeOrchestrator . instance = null
123127 }
128+ } else {
129+ console . log ( `[BridgeOrchestrator#connectOrDisconnect] Already disconnected` )
124130 }
125131 }
126132
@@ -146,6 +152,7 @@ export class BridgeOrchestrator {
146152 this . token = options . token
147153 this . provider = options . provider
148154 this . instanceId = options . sessionId || crypto . randomUUID ( )
155+ this . extensionMetadata = options . extensionMetadata
149156
150157 this . socketTransport = new SocketTransport ( {
151158 url : this . socketBridgeUrl ,
@@ -166,8 +173,14 @@ export class BridgeOrchestrator {
166173 onReconnect : ( ) => this . handleReconnect ( ) ,
167174 } )
168175
169- this . extensionChannel = new ExtensionChannel ( this . instanceId , this . userId , this . provider )
170- this . taskChannel = new TaskChannel ( this . instanceId )
176+ this . extensionChannel = new ExtensionChannel ( {
177+ instanceId : this . instanceId ,
178+ userId : this . userId ,
179+ provider : this . provider ,
180+ extensionMetadata : this . extensionMetadata ,
181+ } )
182+
183+ this . taskChannel = new TaskChannel ( { instanceId : this . instanceId , extensionMetadata : this . extensionMetadata } )
171184 }
172185
173186 private setupSocketListeners ( ) {
0 commit comments