-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathDesktopAgentProxy.ts
More file actions
209 lines (181 loc) · 6.55 KB
/
DesktopAgentProxy.ts
File metadata and controls
209 lines (181 loc) · 6.55 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
import {
AppIdentifier,
AppMetadata,
ContextHandler,
DesktopAgent,
EventHandler,
FDC3EventTypes,
ImplementationMetadata,
IntentHandler,
IntentResolution,
Listener,
LogLevel,
} from '@finos/fdc3-standard';
import { ChannelSupport } from './channels/ChannelSupport';
import { AppSupport } from './apps/AppSupport';
import { IntentSupport } from './intents/IntentSupport';
import { Connectable, Channel } from '@finos/fdc3-standard';
import { Context } from '@finos/fdc3-context';
import { HeartbeatSupport } from './heartbeat/HeartbeatSupport';
import { Logger } from './util/Logger';
/**
* This splits out the functionality of the desktop agent into
* app, channels and intents concerns.
*/
export class DesktopAgentProxy implements DesktopAgent, Connectable {
readonly heartbeat: HeartbeatSupport;
readonly channels: ChannelSupport;
readonly intents: IntentSupport;
readonly apps: AppSupport;
readonly connectables: Connectable[];
constructor(
heartbeat: HeartbeatSupport,
channels: ChannelSupport,
intents: IntentSupport,
apps: AppSupport,
connectables: Connectable[],
logLevel: LogLevel | null
) {
this.heartbeat = heartbeat;
this.intents = intents;
this.channels = channels;
this.apps = apps;
this.connectables = connectables;
//Default log level is set in the Logger utility
if (logLevel) {
Logger.setLogLevel(logLevel);
}
//bind all functions to allow destructuring
this.addEventListener = this.addEventListener.bind(this);
this.getInfo = this.getInfo.bind(this);
this.broadcast = this.broadcast.bind(this);
this.addContextListener = this.addContextListener.bind(this);
this.getUserChannels = this.getUserChannels.bind(this);
this.getSystemChannels = this.getSystemChannels.bind(this);
this.getOrCreateChannel = this.getOrCreateChannel.bind(this);
this.createPrivateChannel = this.createPrivateChannel.bind(this);
this.leaveCurrentChannel = this.leaveCurrentChannel.bind(this);
this.joinUserChannel = this.joinUserChannel.bind(this);
this.joinChannel = this.joinChannel.bind(this);
this.getCurrentChannel = this.getCurrentChannel.bind(this);
this.joinChannel = this.joinChannel.bind(this);
this.findIntent = this.findIntent.bind(this);
this.findIntentsByContext = this.findIntentsByContext.bind(this);
this.raiseIntent = this.raiseIntent.bind(this);
this.addIntentListener = this.addIntentListener.bind(this);
this.raiseIntentForContext = this.raiseIntentForContext.bind(this);
this.open = this.open.bind(this);
this.findInstances = this.findInstances.bind(this);
this.getAppMetadata = this.getAppMetadata.bind(this);
this.disconnect = this.disconnect.bind(this);
this.connect = this.connect.bind(this);
}
addEventListener(type: FDC3EventTypes | null, handler: EventHandler): Promise<Listener> {
switch (type) {
case 'userChannelChanged':
return this.channels.addChannelChangedEventHandler(handler);
default:
Logger.warn(`Tried to add a listener for an unknown event type: ${type}`);
return Promise.reject(new Error('UnknownEventType'));
}
}
getInfo(): Promise<ImplementationMetadata> {
return this.apps.getImplementationMetadata();
}
async broadcast(context: Context): Promise<void> {
const channel = await this.channels.getUserChannel();
if (channel) {
return channel.broadcast(context);
} else {
return Promise.resolve();
}
}
addContextListener(
contextTypeOrHandler: ContextHandler | string | null,
handler?: ContextHandler
): Promise<Listener> {
let theContextType: string | null;
let theHandler: ContextHandler;
if (contextTypeOrHandler == null && typeof handler === 'function') {
theContextType = null;
theHandler = handler;
} else if (typeof contextTypeOrHandler === 'string' && typeof handler === 'function') {
theContextType = contextTypeOrHandler;
theHandler = handler;
} else if (typeof contextTypeOrHandler === 'function') {
// deprecated one-arg version
theContextType = null;
theHandler = contextTypeOrHandler as ContextHandler;
} else {
//invalid call
// TODO: Replace with Standardized error when #1490 is resolved
throw new Error('Invalid arguments passed to addContextListener!');
}
return this.channels.addContextListener(theHandler, theContextType);
}
getUserChannels() {
return this.channels.getUserChannels();
}
getSystemChannels() {
return this.channels.getUserChannels();
}
getOrCreateChannel(channelId: string) {
return this.channels.getOrCreate(channelId);
}
createPrivateChannel() {
return this.channels.createPrivateChannel();
}
leaveCurrentChannel() {
return this.channels.leaveUserChannel();
}
joinUserChannel(channelId: string) {
return this.channels.joinUserChannel(channelId);
}
joinChannel(channelId: string) {
return this.channels.joinUserChannel(channelId);
}
getCurrentChannel(): Promise<Channel | null> {
return this.channels.getUserChannel();
}
findIntent(intent: string, context: Context, resultType: string | undefined) {
return this.intents.findIntent(intent, context, resultType);
}
findIntentsByContext(context: Context) {
return this.intents.findIntentsByContext(context);
}
private ensureAppId(app?: string | AppIdentifier): AppIdentifier | undefined {
if (typeof app === 'string') {
return {
appId: app,
};
} else if (app?.appId) {
return app as AppIdentifier;
} else {
return undefined;
}
}
raiseIntent(intent: string, context: Context, app?: string | AppIdentifier) {
return this.intents.raiseIntent(intent, context, this.ensureAppId(app));
}
addIntentListener(intent: string, handler: IntentHandler) {
return this.intents.addIntentListener(intent, handler);
}
raiseIntentForContext(context: Context, app?: string | AppIdentifier): Promise<IntentResolution> {
return this.intents.raiseIntentForContext(context, this.ensureAppId(app));
}
open(app: string | AppIdentifier, context?: Context | undefined) {
return this.apps.open(this.ensureAppId(app)!, context);
}
findInstances(app: AppIdentifier) {
return this.apps.findInstances(app);
}
getAppMetadata(app: AppIdentifier): Promise<AppMetadata> {
return this.apps.getAppMetadata(app);
}
async disconnect(): Promise<void> {
await Promise.all(this.connectables.map(c => c.disconnect()));
}
async connect(): Promise<void> {
await Promise.all(this.connectables.map(c => c.connect()));
}
}