-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathDefaultChannelSupport.ts
212 lines (191 loc) · 7.26 KB
/
DefaultChannelSupport.ts
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
import {
Channel,
ContextHandler,
Listener,
PrivateChannel,
ChannelSelector,
EventHandler,
ChannelError,
} from '@finos/fdc3-standard';
import { Messaging } from '../Messaging';
import { ChannelSupport } from './ChannelSupport';
import { DefaultPrivateChannel } from './DefaultPrivateChannel';
import { DefaultChannel } from './DefaultChannel';
import { DefaultContextListener } from '../listeners/DefaultContextListener';
import { UserChannelContextListener } from '../listeners/UserChannelContextListener';
import { EventListener } from '../listeners/EventListener';
import {
GetCurrentChannelResponse,
GetCurrentChannelRequest,
GetUserChannelsResponse,
GetUserChannelsRequest,
GetOrCreateChannelResponse,
GetOrCreateChannelRequest,
CreatePrivateChannelResponse,
CreatePrivateChannelRequest,
LeaveCurrentChannelResponse,
LeaveCurrentChannelRequest,
JoinUserChannelResponse,
JoinUserChannelRequest,
} from '@finos/fdc3-schema/dist/generated/api/BrowserTypes';
import { throwIfUndefined } from '../util/throwIfUndefined';
import { Logger } from '../util/Logger';
export class DefaultChannelSupport implements ChannelSupport {
readonly messaging: Messaging;
readonly channelSelector: ChannelSelector;
protected userChannels: Channel[] = [];
protected userChannelListeners: UserChannelContextListener[] = [];
constructor(messaging: Messaging, channelSelector: ChannelSelector) {
this.messaging = messaging;
this.channelSelector = channelSelector;
this.channelSelector.setChannelChangeCallback((channelId: string | null) => {
Logger.debug('Channel selector reports channel changed: ', channelId);
if (channelId == null) {
this.leaveUserChannel();
} else {
this.joinUserChannel(channelId);
}
});
this.addChannelChangedEventHandler(e => {
Logger.debug('Desktop Agent reports channel changed: ', e.details.newChannelId);
this.channelSelector.updateChannel(e.details.newChannelId, this.userChannels);
});
}
async addChannelChangedEventHandler(handler: EventHandler): Promise<Listener> {
const listener = new EventListener(this.messaging, 'channelChangedEvent', 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');
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,
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 getUserChannels(): Promise<Channel[]> {
const request: GetUserChannelsRequest = {
meta: this.messaging.createMeta(),
type: 'getUserChannelsRequest',
payload: {},
};
const response = await this.messaging.exchange<GetUserChannelsResponse>(request, 'getUserChannelsResponse');
//handle successful responses
const channels = response.payload.userChannels!;
this.userChannels = channels.map(c => new DefaultChannel(this.messaging, 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');
throwIfUndefined(
response.payload.channel,
'Invalid response from Desktop Agent to getOrCreate!',
response,
ChannelError.CreationFailed
);
const out = new DefaultChannel(this.messaging, 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'
);
throwIfUndefined(
response.payload.privateChannel,
'Invalid response from Desktop Agent to createPrivateChannel!',
response,
ChannelError.CreationFailed
);
return new DefaultPrivateChannel(this.messaging, 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.channelSelector.updateChannel(null, this.userChannels);
for (const l of this.userChannelListeners) {
await l.changeChannel(null);
}
}
async joinUserChannel(id: string) {
const request: JoinUserChannelRequest = {
meta: this.messaging.createMeta(),
type: 'joinUserChannelRequest',
payload: {
channelId: id,
},
};
await this.messaging.exchange<JoinUserChannelResponse>(request, 'joinUserChannelResponse');
this.channelSelector.updateChannel(id, this.userChannels);
for (const l of this.userChannelListeners) {
await l.changeChannel(new DefaultChannel(this.messaging, id, 'user'));
}
}
async addContextListener(handler: ContextHandler, type: string | null): Promise<Listener> {
/** Utility class used to wrap the DefaultContextListener and ensure it gets removed
* when its unsubscribe function is called.
*/
class UnsubscribingDefaultContextListener extends DefaultContextListener {
container: DefaultChannelSupport;
constructor(
container: DefaultChannelSupport,
messaging: Messaging,
channelId: string | null,
contextType: string | null,
handler: ContextHandler,
messageType: string = 'broadcastEvent'
) {
super(messaging, channelId, contextType, handler, messageType);
this.container = container;
}
async unsubscribe(): Promise<void> {
super.unsubscribe();
this.container.userChannelListeners = this.container.userChannelListeners.filter(l => l != this);
}
}
const currentChannelId = (await this.getUserChannel())?.id ?? null;
const listener = new UnsubscribingDefaultContextListener(this, this.messaging, currentChannelId, type, handler);
this.userChannelListeners.push(listener);
await listener.register();
return listener;
}
}