-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathmessage-port.ts
More file actions
102 lines (79 loc) · 3.92 KB
/
message-port.ts
File metadata and controls
102 lines (79 loc) · 3.92 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
import { AppIdentifier, DesktopAgent, LogLevel } from '@finos/fdc3-standard';
import {
DesktopAgentProxy,
DefaultChannelSupport,
DefaultAppSupport,
DefaultIntentSupport,
ChannelSupport,
DefaultHeartbeatSupport,
} from '@finos/fdc3-agent-proxy';
import { ConnectionDetails, MessagePortMessaging } from './MessagePortMessaging';
import { DefaultDesktopAgentIntentResolver } from '../ui/DefaultDesktopAgentIntentResolver';
import { DefaultDesktopAgentChannelSelector } from '../ui/DefaultDesktopAgentChannelSelector';
import { NullIntentResolver } from '../ui/NullIntentResolver';
import { NullChannelSelector } from '../ui/NullChannelSelector';
import { ChannelSelector } from '@finos/fdc3-standard';
import { Logger } from '../util/Logger';
/**
* Given a message port, constructs a desktop agent to communicate via that.
*/
export async function createDesktopAgentAPI(
cd: ConnectionDetails,
appIdentifier: AppIdentifier,
logLevel: LogLevel | null
): Promise<DesktopAgent> {
Logger.debug('message-port: Creating Desktop Agent...');
//Message port should have already been started for use in identity validation
function string(o: string | boolean): string | null {
if (o == true || o == false) {
return null;
} else {
return o;
}
}
const messaging = new MessagePortMessaging(cd, appIdentifier);
const useResolver = cd.handshake.payload.intentResolverUrl && cd.options.intentResolver;
const useSelector = cd.handshake.payload.channelSelectorUrl && cd.options.channelSelector;
const intentResolver = useResolver
? new DefaultDesktopAgentIntentResolver(string(cd.handshake.payload.intentResolverUrl))
: new NullIntentResolver();
const channelSelector = useSelector
? new DefaultDesktopAgentChannelSelector(string(cd.handshake.payload.channelSelectorUrl))
: new NullChannelSelector();
Logger.debug('message-port: Setting up support components...');
const hs = new DefaultHeartbeatSupport(messaging);
const cs = new DefaultChannelSupport(messaging, channelSelector, cd.messageExchangeTimeout);
const is = new DefaultIntentSupport(messaging, intentResolver, cd.messageExchangeTimeout, cd.appLaunchTimeout);
const as = new DefaultAppSupport(messaging, cd.messageExchangeTimeout, cd.appLaunchTimeout);
const da = new DesktopAgentProxy(hs, cs, is, as, [hs, intentResolver, channelSelector], logLevel);
Logger.debug('message-port: Connecting components ...');
await da.connect();
Logger.debug('message-port: Populating channel selector...');
await populateChannelSelector(cs, channelSelector);
Logger.debug('message-port: Setting up disconnect handling...');
handleDisconnectOnPageHide(da, messaging);
Logger.debug('message-port: Returning...');
return da;
}
async function populateChannelSelector(cs: ChannelSupport, channelSelector: ChannelSelector): Promise<void> {
const channel = await cs.getUserChannel();
const userChannels = await cs.getUserChannels();
channelSelector.updateChannel(channel?.id ?? null, userChannels);
}
function handleDisconnectOnPageHide(da: DesktopAgentProxy, messaging: MessagePortMessaging) {
globalThis.window.addEventListener('pagehide', async event => {
Logger.log(`Received pagehide event with persisted ${event.persisted}`);
//If persisted == true then the page is stored and might come back if the user hits back
// In that case don't disconnect and let heartbeat handle that instead
//TODO: implement disconnect on any hide and reconnect if the page is shown again
// Will have to happen inside the DesktopAgentProxy as the reference to the DA needs to remain the same
// and any listeners need to be re-registered automatically etc.
if (!event.persisted) {
//the page is being destroyed, disconnect from the DA
//Notify the Desktop Agent implementation to disconnect
da.disconnect();
//disconnect the MessagePort - which should send WCP6Goodbye first
messaging.disconnect();
}
});
}