-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathMethods.ts
308 lines (281 loc) · 11.5 KB
/
Methods.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
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
/**
* SPDX-License-Identifier: Apache-2.0
* Copyright FINOS FDC3 contributors - see NOTICE file
*/
import {
AppIdentifier,
AppIntent,
Channel,
ContextHandler,
IntentHandler,
IntentResolution,
Listener,
ImplementationMetadata,
AppMetadata,
PrivateChannel,
Intent,
StandardContextType,
StandardIntent,
ContextType,
FDC3EventTypes,
EventHandler,
DesktopAgent,
} from '..';
import { StandardContextsSet } from '../internal/contextConfiguration';
import { StandardIntentsSet } from '../internal/intentConfiguration';
import { Context } from '@finos/fdc3-context';
const UnavailableError = new Error('FDC3 DesktopAgent not available at `window.fdc3`.');
/**
* @deprecated This function depends on window.fdc3 (which may not be set for web-based Desktop Agents)
* and does not wait on the fdc3Ready event, so it may return errors on container-based Desktop Agents.
* Use `const fdc3 = getAgent()` to retrieve (and wait for) a reference to the FDC3 API instead.
*/
function rejectIfNoGlobal(f: (fdc3: DesktopAgent) => Promise<any>) {
return window.fdc3 ? f(window.fdc3) : Promise.reject(UnavailableError);
}
function isString(app: AppIdentifier | string | undefined): app is string {
return !!app && typeof app === 'string';
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function open(app: AppIdentifier | string, context?: Context): Promise<AppIdentifier> {
if (isString(app)) {
return window.fdc3 ? window.fdc3.open(app, context) : Promise.reject(UnavailableError);
} else {
return window.fdc3 ? window.fdc3.open(app, context) : Promise.reject(UnavailableError);
}
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function findIntent(intent: Intent, context?: Context, resultType?: string): Promise<AppIntent> {
return window.fdc3 ? window.fdc3.findIntent(intent, context, resultType) : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function findIntentsByContext(context: Context, resultType?: string): Promise<AppIntent[]> {
return window.fdc3 ? window.fdc3.findIntentsByContext(context, resultType) : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function broadcast(context: Context): Promise<void> {
return window.fdc3 ? window.fdc3.broadcast(context) : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function raiseIntent(intent: Intent, context: Context, app?: AppIdentifier | string): Promise<IntentResolution> {
if (isString(app)) {
return window.fdc3 ? window.fdc3.raiseIntent(intent, context, app) : Promise.reject(UnavailableError);
} else {
return window.fdc3 ? window.fdc3.raiseIntent(intent, context, app) : Promise.reject(UnavailableError);
}
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function raiseIntentForContext(context: Context, app?: AppIdentifier | string): Promise<IntentResolution> {
if (isString(app)) {
return window.fdc3 ? window.fdc3.raiseIntentForContext(context, app) : Promise.reject(UnavailableError);
} else {
return window.fdc3 ? window.fdc3.raiseIntentForContext(context, app) : Promise.reject(UnavailableError);
}
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function addIntentListener(intent: Intent, handler: IntentHandler): Promise<Listener> {
return window.fdc3 ? window.fdc3.addIntentListener(intent, handler) : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function addContextListener(
contextTypeOrHandler: ContextType | null | ContextHandler,
handler?: ContextHandler
): Promise<Listener> {
//Handle (deprecated) function signature that allowed contextType argument to be omitted
if (typeof contextTypeOrHandler !== 'function') {
return window.fdc3
? window.fdc3.addContextListener(contextTypeOrHandler, handler as ContextHandler)
: Promise.reject(UnavailableError);
} else {
return window.fdc3
? window.fdc3.addContextListener(null, contextTypeOrHandler as ContextHandler)
: Promise.reject(UnavailableError);
}
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function addEventListener(eventType: FDC3EventTypes, handler: EventHandler): Promise<Listener> {
return rejectIfNoGlobal(fdc3 => fdc3.addEventListener(eventType, handler));
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function getUserChannels(): Promise<Channel[]> {
if (window.fdc3) {
//fallback to getSystemChannels for FDC3 <2.0 implementations
if (window.fdc3.getUserChannels) {
return window.fdc3.getUserChannels();
} else {
return window.fdc3.getSystemChannels();
}
} else {
return Promise.reject(UnavailableError);
}
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function getSystemChannels(): Promise<Channel[]> {
//fall-forward to getUserChannels for FDC3 2.0+ implementations
return getUserChannels();
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function joinUserChannel(channelId: string): Promise<void> {
if (window.fdc3) {
//fallback to joinChannel for FDC3 <2.0 implementations
if (window.fdc3.joinUserChannel) {
return window.fdc3.joinUserChannel(channelId);
} else {
return window.fdc3.joinChannel(channelId);
}
} else {
return Promise.reject(UnavailableError);
}
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function joinChannel(channelId: string): Promise<void> {
//fall-forward to joinUserChannel for FDC3 2.0+ implementations
return joinUserChannel(channelId);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function getOrCreateChannel(channelId: string): Promise<Channel> {
return window.fdc3 ? window.fdc3.getOrCreateChannel(channelId) : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function getCurrentChannel(): Promise<Channel | null> {
return window.fdc3 ? window.fdc3.getCurrentChannel() : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function leaveCurrentChannel(): Promise<void> {
return window.fdc3 ? window.fdc3.leaveCurrentChannel() : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function createPrivateChannel(): Promise<PrivateChannel> {
return window.fdc3 ? window.fdc3.createPrivateChannel() : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function getInfo(): Promise<ImplementationMetadata> {
return window.fdc3 ? window.fdc3.getInfo() : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function getAppMetadata(app: AppIdentifier): Promise<AppMetadata> {
return window.fdc3 ? window.fdc3.getAppMetadata(app) : Promise.reject(UnavailableError);
}
/**
* @deprecated Importing individual FDC3 API calls is deprecated. Use `getAgent` to retrieve
* an FDC3 API reference and use the functions that it provides instead.
*/
export function findInstances(app: AppIdentifier): Promise<AppIdentifier[]> {
return window.fdc3 ? window.fdc3.findInstances(app) : Promise.reject(UnavailableError);
}
/**
* Check if the given context is a standard context type.
* @param contextType
*/
export function isStandardContextType(contextType: ContextType): contextType is StandardContextType {
return StandardContextsSet.has(contextType as StandardContextType);
}
/**
* Check if the given intent is a standard intent.
* @param intent
*/
export function isStandardIntent(intent: Intent): intent is StandardIntent {
return StandardIntentsSet.has(intent as StandardIntent);
}
/**
* Compare numeric semver version number strings (in the form `1.2.3`).
*
* Returns `-1` if the first argument is a lower version number than the second,
* `1` if the first argument is greater than the second, 0 if the arguments are
* equal and `null` if an error occurred during the comparison.
*
* @param a
* @param b
*/
export const compareVersionNumbers: (a: string, b: string) => number | null = (a, b) => {
try {
const aVerArr = a.split('.').map(Number);
const bVerArr = b.split('.').map(Number);
for (let index = 0; index < Math.max(aVerArr.length, bVerArr.length); index++) {
/* If one version number has more digits and the other does not, and they are otherwise equal,
assume the longer is greater. E.g. 1.1.1 > 1.1 */
if (index === aVerArr.length || aVerArr[index] < bVerArr[index]) {
return -1;
} else if (index === bVerArr.length || aVerArr[index] > bVerArr[index]) {
return 1;
}
}
return 0;
} catch (e) {
console.error('Failed to compare version strings', e);
return null;
}
};
/**
* Check if the FDC3 version in an ImplementationMetadata object is greater than
* or equal to the supplied numeric semver version number string (in the form `1.2.3`).
*
* Returns a boolean or null if an error occurred while comparing the version numbers.
*
* @param metadata
* @param version
*/
export const versionIsAtLeast: (metadata: ImplementationMetadata, version: string) => boolean | null = (
metadata,
version
) => {
const comparison = compareVersionNumbers(metadata.fdc3Version, version);
return comparison === null ? null : comparison >= 0 ? true : false;
};