-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathDefaultDesktopAgentChannelSelector.ts
62 lines (53 loc) · 2.14 KB
/
DefaultDesktopAgentChannelSelector.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
import { Channel } from '@finos/fdc3-standard';
import { ChannelSelector } from '@finos/fdc3-standard';
import { AbstractUIComponent, DEFAULT_UI_ROOT_URL } from './AbstractUIComponent';
import { BrowserTypes } from '@finos/fdc3-schema';
import { Logger } from '../util/Logger';
const { isFdc3UserInterfaceChannelSelected } = BrowserTypes;
type Fdc3UserInterfaceChannels = BrowserTypes.Fdc3UserInterfaceChannels;
/**
* Handles communication between an injected Channel Selector UI and the getAgent implementation.
*/
export class DefaultDesktopAgentChannelSelector extends AbstractUIComponent implements ChannelSelector {
private callback: ((channelId: string | null) => void) | null = null;
constructor(url: string | null) {
//TODO: check default UI URL is correct on release
super(url ?? DEFAULT_UI_ROOT_URL + 'channel_selector.html', 'FDC3 Channel Selector');
}
async setupMessagePort(port: MessagePort): Promise<void> {
this.port = port;
port.addEventListener('message', e => {
if (isFdc3UserInterfaceChannelSelected(e.data)) {
Logger.debug(`DefaultDesktopAgentChannelSelector: Received channel selection message: `, e.data);
const choice = e.data;
if (this.callback) {
this.callback(choice.payload.selected);
}
}
});
//This starts the port so do it last
await super.setupMessagePort(port);
}
async updateChannel(channelId: string | null, availableChannels: Channel[]): Promise<void> {
const message: Fdc3UserInterfaceChannels = {
type: 'Fdc3UserInterfaceChannels',
payload: {
selected: channelId,
userChannels: availableChannels.map(ch => {
return {
id: ch.id,
type: 'user',
displayMetadata: ch.displayMetadata,
};
}),
},
};
//don't post until the messageport is ready
await this.messagePortIsReady;
this.port?.postMessage(message);
Logger.debug(`DefaultDesktopAgentChannelSelector: Sent channels data to channel selector: `, message);
}
setChannelChangeCallback(callback: (channelId: string | null) => void): void {
this.callback = callback;
}
}