-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathDefaultChannelSupport.ts
More file actions
321 lines (290 loc) · 10.5 KB
/
DefaultChannelSupport.ts
File metadata and controls
321 lines (290 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import {
Channel,
ContextHandler,
Listener,
PrivateChannel,
ChannelSelector,
EventHandler,
ChannelError,
ApiEvent,
FDC3ChannelChangedEvent,
FDC3EventTypes,
} from '@finos/fdc3-standard';
import { Messaging } from '../Messaging.js';
import { ChannelSupport } from './ChannelSupport.js';
import { DefaultPrivateChannel } from './DefaultPrivateChannel.js';
import { DefaultChannel } from './DefaultChannel.js';
import { DefaultContextListener } from '../listeners/DefaultContextListener.js';
import { UserChannelContextListener } from '../listeners/UserChannelContextListener.js';
import { DesktopAgentEventListener } from '../listeners/DesktopAgentEventListener.js';
import {
GetCurrentChannelResponse,
GetCurrentChannelRequest,
GetUserChannelsResponse,
GetUserChannelsRequest,
GetOrCreateChannelResponse,
GetOrCreateChannelRequest,
CreatePrivateChannelResponse,
CreatePrivateChannelRequest,
LeaveCurrentChannelResponse,
LeaveCurrentChannelRequest,
JoinUserChannelResponse,
JoinUserChannelRequest,
BroadcastEvent,
} from '@finos/fdc3-schema/dist/generated/api/BrowserTypes.js';
import { throwIfUndefined } from '../util/throwIfUndefined.js';
import { Logger } from '../util/Logger.js';
export class DefaultChannelSupport implements ChannelSupport {
readonly messaging: Messaging;
readonly channelSelector: ChannelSelector;
readonly messageExchangeTimeout: number;
protected userChannels: Channel[] | null = null;
protected userChannelListeners: UserChannelContextListener[] = [];
private currentChannel: Channel | null = null;
constructor(messaging: Messaging, channelSelector: ChannelSelector, messageExchangeTimeout: number) {
this.messaging = messaging;
this.channelSelector = channelSelector;
this.messageExchangeTimeout = messageExchangeTimeout;
this.channelSelector.setChannelChangeCallback((channelId: string | null) => {
Logger.debug('Channel selector reports channel changed: ', channelId);
if (channelId == null) {
this.leaveUserChannel();
} else {
this.joinUserChannel(channelId);
}
});
this.addEventListener(async (e: ApiEvent) => {
const cce = e as FDC3ChannelChangedEvent;
const newChannelId = cce.details.currentChannelId;
Logger.debug('Desktop Agent reports channel changed: ', newChannelId);
let theChannel: Channel | null = null;
// if theres a newChannelId, retrieve details of the channel
if (newChannelId != null) {
theChannel = (await this.getUserChannelsCached()).find(uc => uc.id == newChannelId) ?? null;
if (!theChannel) {
// Channel not found - query user channels in case they have changed for some reason
Logger.debug('Unknown user channel, querying Desktop Agent for updated user channels: ', newChannelId);
await this.getUserChannels();
theChannel = (await this.getUserChannelsCached()).find(uc => uc.id == newChannelId) ?? null;
if (!theChannel) {
Logger.warn(
'Received user channel update with unknown user channel (user channel listeners will not work): ',
newChannelId
);
}
}
}
this.currentChannel = theChannel;
this.channelSelector.updateChannel(theChannel?.id ?? null, await this.getUserChannelsCached());
}, 'userChannelChanged');
}
async addEventListener(handler: EventHandler, type: FDC3EventTypes | null): Promise<Listener> {
const listener = new DesktopAgentEventListener(this.messaging, this.messageExchangeTimeout, type, handler);
await listener.register();
return listener;
}
async getUserChannel(): Promise<Channel | null> {
const request: GetCurrentChannelRequest = {
meta: this.messaging.createMeta(),
type: 'getCurrentChannelRequest',
payload: {},
};
const response = await this.messaging.exchange<GetCurrentChannelResponse>(
request,
'getCurrentChannelResponse',
this.messageExchangeTimeout
);
throwIfUndefined(
response.payload.channel,
'Invalid response from Desktop Agent to getCurrentChannel (channel should be explicitly null if no channel is set)!',
response,
ChannelError.NoChannelFound
);
//handle successful responses - errors will already have been thrown by exchange above
/* istanbul ignore else */
if (response.payload.channel) {
return new DefaultChannel(
this.messaging,
this.messageExchangeTimeout,
response.payload.channel.id,
'user',
response.payload.channel.displayMetadata
);
} else if (response.payload.channel === null) {
//this is a valid response if no channel is set
return null;
} else {
//Should not reach here as we will throw in exchange or throwIfNotFound
return null;
}
}
async getUserChannelsCached(): Promise<Channel[]> {
if (this.userChannels) {
return this.userChannels;
} else {
this.userChannels = await this.getUserChannels();
return this.userChannels;
}
}
async getUserChannels(): Promise<Channel[]> {
const request: GetUserChannelsRequest = {
meta: this.messaging.createMeta(),
type: 'getUserChannelsRequest',
payload: {},
};
const response = await this.messaging.exchange<GetUserChannelsResponse>(
request,
'getUserChannelsResponse',
this.messageExchangeTimeout
);
//handle successful responses
const channels = response.payload.userChannels!;
this.userChannels = channels.map(
c => new DefaultChannel(this.messaging, this.messageExchangeTimeout, c.id, 'user', c.displayMetadata)
);
return this.userChannels;
}
async getOrCreate(id: string): Promise<Channel> {
const request: GetOrCreateChannelRequest = {
meta: this.messaging.createMeta(),
type: 'getOrCreateChannelRequest',
payload: {
channelId: id,
},
};
const response = await this.messaging.exchange<GetOrCreateChannelResponse>(
request,
'getOrCreateChannelResponse',
this.messageExchangeTimeout
);
throwIfUndefined(
response.payload.channel,
'Invalid response from Desktop Agent to getOrCreate!',
response,
ChannelError.CreationFailed
);
const out = new DefaultChannel(
this.messaging,
this.messageExchangeTimeout,
id,
'app',
response.payload.channel!.displayMetadata
);
return out;
}
async createPrivateChannel(): Promise<PrivateChannel> {
const request: CreatePrivateChannelRequest = {
meta: this.messaging.createMeta(),
type: 'createPrivateChannelRequest',
payload: {},
};
const response = await this.messaging.exchange<CreatePrivateChannelResponse>(
request,
'createPrivateChannelResponse',
this.messageExchangeTimeout
);
throwIfUndefined(
response.payload.privateChannel,
'Invalid response from Desktop Agent to createPrivateChannel!',
response,
ChannelError.CreationFailed
);
return new DefaultPrivateChannel(this.messaging, this.messageExchangeTimeout, response.payload.privateChannel!.id);
}
async leaveUserChannel(): Promise<void> {
const request: LeaveCurrentChannelRequest = {
meta: this.messaging.createMeta(),
type: 'leaveCurrentChannelRequest',
payload: {},
};
await this.messaging.exchange<LeaveCurrentChannelResponse>(
request,
'leaveCurrentChannelResponse',
this.messageExchangeTimeout
);
this.currentChannel = null;
this.channelSelector.updateChannel(null, await this.getUserChannelsCached());
}
async joinUserChannel(id: string) {
const request: JoinUserChannelRequest = {
meta: this.messaging.createMeta(),
type: 'joinUserChannelRequest',
payload: {
channelId: id,
},
};
await this.messaging.exchange<JoinUserChannelResponse>(
request,
'joinUserChannelResponse',
this.messageExchangeTimeout
);
const userChannels = await this.getUserChannelsCached();
this.currentChannel = userChannels.find(c => c.id == id) ?? null;
if (this.currentChannel == null) {
throw new Error(ChannelError.NoChannelFound);
}
this.channelSelector.updateChannel(id, userChannels);
for (const l of this.userChannelListeners) {
await l.changeChannel();
}
}
async addContextListener(handler: ContextHandler, type: string | null): Promise<Listener> {
/**
* Utility class used to wrap the DefaultContextListener to match the internal channel id
* and ensure it gets removed when its unsubscribe function is called.
*/
class UnsubscribingDefaultContextListener extends DefaultContextListener implements UserChannelContextListener {
container: DefaultChannelSupport;
constructor(
container: DefaultChannelSupport,
messaging: Messaging,
messageExchangeTimeout: number,
contextType: string | null,
handler: ContextHandler,
messageType: string = 'broadcastEvent'
) {
super(messaging, messageExchangeTimeout, null, contextType, handler, messageType);
this.container = container;
}
async unsubscribe(): Promise<void> {
super.unsubscribe();
this.container.userChannelListeners = this.container.userChannelListeners.filter(l => l != this);
}
async register(): Promise<void> {
await super.register();
await this.changeChannel();
}
async changeChannel(): Promise<void> {
if (this.container.currentChannel != null) {
const context = await this.container.currentChannel?.getCurrentContext(this.contextType ?? undefined);
if (context) {
this.handler(context);
}
}
}
onAMatchingChannel(m: BroadcastEvent): boolean {
return this.container.currentChannel != null && m.payload.channelId == this.container.currentChannel.id;
}
openBroadcastEvent(m: BroadcastEvent): boolean {
return m.payload.channelId == null;
}
filter(m: BroadcastEvent): boolean {
return (
m.type == this.messageType &&
(this.onAMatchingChannel(m) || this.openBroadcastEvent(m)) &&
(m.payload.context?.type == this.contextType || this.contextType == null)
);
}
}
const listener = new UnsubscribingDefaultContextListener(
this,
this.messaging,
this.messageExchangeTimeout,
type,
handler
);
this.userChannelListeners.push(listener);
await listener.register();
return listener;
}
}