11import {
22 Channel ,
3+ Connectable ,
34 ContextHandler ,
45 Listener ,
56 PrivateChannel ,
@@ -35,7 +36,7 @@ import {
3536import { throwIfUndefined } from '../util/throwIfUndefined.js' ;
3637import { Logger } from '../util/Logger.js' ;
3738
38- export class DefaultChannelSupport implements ChannelSupport {
39+ export class DefaultChannelSupport implements ChannelSupport , Connectable {
3940 readonly messaging : Messaging ;
4041 readonly channelSelector : ChannelSelector ;
4142 readonly messageExchangeTimeout : number ;
@@ -55,8 +56,14 @@ export class DefaultChannelSupport implements ChannelSupport {
5556 this . joinUserChannel ( channelId ) ;
5657 }
5758 } ) ;
59+ }
60+
61+ async connect ( ) : Promise < void > {
62+ //retrieve the current user channel in case the Desktop Agent started us on a channel
63+ this . currentChannel = await this . getUserChannel ( ) ;
5864
59- this . addEventListener ( async ( e : ApiEvent ) => {
65+ //register for channelChangedEvents to track any DesktopAgent managed user channel changes
66+ await this . addEventListener ( async ( e : ApiEvent ) => {
6067 const cce = e as FDC3ChannelChangedEvent ;
6168 const newChannelId = cce . details . currentChannelId ;
6269 Logger . debug ( 'Desktop Agent reports channel changed: ' , newChannelId ) ;
@@ -65,12 +72,12 @@ export class DefaultChannelSupport implements ChannelSupport {
6572
6673 // if theres a newChannelId, retrieve details of the channel
6774 if ( newChannelId != null ) {
68- theChannel = ( await this . getUserChannelsCached ( ) ) . find ( uc => uc . id == newChannelId ) ?? null ;
75+ theChannel = ( await this . getUserChannels ( ) ) . find ( uc => uc . id == newChannelId ) ?? null ;
6976 if ( ! theChannel ) {
7077 // Channel not found - query user channels in case they have changed for some reason
7178 Logger . debug ( 'Unknown user channel, querying Desktop Agent for updated user channels: ' , newChannelId ) ;
7279 await this . getUserChannels ( ) ;
73- theChannel = ( await this . getUserChannelsCached ( ) ) . find ( uc => uc . id == newChannelId ) ?? null ;
80+ theChannel = ( await this . getUserChannels ( ) ) . find ( uc => uc . id == newChannelId ) ?? null ;
7481 if ( ! theChannel ) {
7582 Logger . warn (
7683 'Received user channel update with unknown user channel (user channel listeners will not work): ' ,
@@ -81,83 +88,88 @@ export class DefaultChannelSupport implements ChannelSupport {
8188 }
8289
8390 this . currentChannel = theChannel ;
84- this . channelSelector . updateChannel ( theChannel ?. id ?? null , await this . getUserChannelsCached ( ) ) ;
91+ this . channelSelector . updateChannel ( theChannel ?. id ?? null , await this . getUserChannels ( ) ) ;
8592 } , 'userChannelChanged' ) ;
8693 }
8794
95+ async disconnect ( ) : Promise < void > {
96+ // no-op
97+ }
98+
8899 async addEventListener ( handler : EventHandler , type : FDC3EventTypes | null ) : Promise < Listener > {
89100 const listener = new DesktopAgentEventListener ( this . messaging , this . messageExchangeTimeout , type , handler ) ;
90101 await listener . register ( ) ;
91102 return listener ;
92103 }
93104
94105 async getUserChannel ( ) : Promise < Channel | null > {
95- const request : GetCurrentChannelRequest = {
96- meta : this . messaging . createMeta ( ) ,
97- type : 'getCurrentChannelRequest' ,
98- payload : { } ,
99- } ;
100- const response = await this . messaging . exchange < GetCurrentChannelResponse > (
101- request ,
102- 'getCurrentChannelResponse' ,
103- this . messageExchangeTimeout
104- ) ;
105-
106- throwIfUndefined (
107- response . payload . channel ,
108- 'Invalid response from Desktop Agent to getCurrentChannel (channel should be explicitly null if no channel is set)!' ,
109- response ,
110- ChannelError . NoChannelFound
111- ) ;
106+ if ( this . currentChannel ) {
107+ //if the current channel is know,, return it as this variable is maintained by a channelChangedEvent listener
108+ return this . currentChannel ;
109+ } else {
110+ const request : GetCurrentChannelRequest = {
111+ meta : this . messaging . createMeta ( ) ,
112+ type : 'getCurrentChannelRequest' ,
113+ payload : { } ,
114+ } ;
115+ const response = await this . messaging . exchange < GetCurrentChannelResponse > (
116+ request ,
117+ 'getCurrentChannelResponse' ,
118+ this . messageExchangeTimeout
119+ ) ;
112120
113- //handle successful responses - errors will already have been thrown by exchange above
114- /* istanbul ignore else */
115- if ( response . payload . channel ) {
116- return new DefaultChannel (
117- this . messaging ,
118- this . messageExchangeTimeout ,
119- response . payload . channel . id ,
120- 'user' ,
121- response . payload . channel . displayMetadata
121+ throwIfUndefined (
122+ response . payload . channel ,
123+ 'Invalid response from Desktop Agent to getCurrentChannel (channel should be explicitly null if no channel is set)!' ,
124+ response ,
125+ ChannelError . NoChannelFound
122126 ) ;
123- } else if ( response . payload . channel === null ) {
124- //this is a valid response if no channel is set
125- return null ;
126- } else {
127- //Should not reach here as we will throw in exchange or throwIfNotFound
128- return null ;
127+
128+ //handle successful responses - errors will already have been thrown by exchange above
129+ /* istanbul ignore else */
130+ if ( response . payload . channel ) {
131+ return new DefaultChannel (
132+ this . messaging ,
133+ this . messageExchangeTimeout ,
134+ response . payload . channel . id ,
135+ 'user' ,
136+ response . payload . channel . displayMetadata
137+ ) ;
138+ } else if ( response . payload . channel === null ) {
139+ //this is a valid response if no channel is set
140+ return null ;
141+ } else {
142+ //Should not reach here as we will throw in exchange or throwIfNotFound
143+ return null ;
144+ }
129145 }
130146 }
131147
132- async getUserChannelsCached ( ) : Promise < Channel [ ] > {
148+ async getUserChannels ( ) : Promise < Channel [ ] > {
149+ //If the user channels are known, return them as they are not expected to change
133150 if ( this . userChannels ) {
134151 return this . userChannels ;
135152 } else {
136- this . userChannels = await this . getUserChannels ( ) ;
153+ const request : GetUserChannelsRequest = {
154+ meta : this . messaging . createMeta ( ) ,
155+ type : 'getUserChannelsRequest' ,
156+ payload : { } ,
157+ } ;
158+ const response = await this . messaging . exchange < GetUserChannelsResponse > (
159+ request ,
160+ 'getUserChannelsResponse' ,
161+ this . messageExchangeTimeout
162+ ) ;
163+
164+ //handle successful responses
165+ const channels = response . payload . userChannels ! ;
166+ this . userChannels = channels . map (
167+ c => new DefaultChannel ( this . messaging , this . messageExchangeTimeout , c . id , 'user' , c . displayMetadata )
168+ ) ;
137169 return this . userChannels ;
138170 }
139171 }
140172
141- async getUserChannels ( ) : Promise < Channel [ ] > {
142- const request : GetUserChannelsRequest = {
143- meta : this . messaging . createMeta ( ) ,
144- type : 'getUserChannelsRequest' ,
145- payload : { } ,
146- } ;
147- const response = await this . messaging . exchange < GetUserChannelsResponse > (
148- request ,
149- 'getUserChannelsResponse' ,
150- this . messageExchangeTimeout
151- ) ;
152-
153- //handle successful responses
154- const channels = response . payload . userChannels ! ;
155- this . userChannels = channels . map (
156- c => new DefaultChannel ( this . messaging , this . messageExchangeTimeout , c . id , 'user' , c . displayMetadata )
157- ) ;
158- return this . userChannels ;
159- }
160-
161173 async getOrCreate ( id : string ) : Promise < Channel > {
162174 const request : GetOrCreateChannelRequest = {
163175 meta : this . messaging . createMeta ( ) ,
@@ -223,7 +235,7 @@ export class DefaultChannelSupport implements ChannelSupport {
223235 this . messageExchangeTimeout
224236 ) ;
225237 this . currentChannel = null ;
226- this . channelSelector . updateChannel ( null , await this . getUserChannelsCached ( ) ) ;
238+ this . channelSelector . updateChannel ( null , await this . getUserChannels ( ) ) ;
227239 }
228240
229241 async joinUserChannel ( id : string ) {
@@ -240,7 +252,7 @@ export class DefaultChannelSupport implements ChannelSupport {
240252 this . messageExchangeTimeout
241253 ) ;
242254
243- const userChannels = await this . getUserChannelsCached ( ) ;
255+ const userChannels = await this . getUserChannels ( ) ;
244256 this . currentChannel = userChannels . find ( c => c . id == id ) ?? null ;
245257 if ( this . currentChannel == null ) {
246258 throw new Error ( ChannelError . NoChannelFound ) ;
0 commit comments