1
- import {
2
- AppIntent ,
3
- Channel ,
4
- Context ,
5
- ContextHandler ,
6
- IntentResolution ,
7
- Listener ,
8
- ImplementationMetadata ,
9
- } from '..' ;
1
+ import { AppIntent , Channel , Context , ContextHandler , IntentResolution , Listener , ImplementationMetadata } from '..' ;
10
2
import { TargetApp } from './Types' ;
11
3
12
- const unavailableError = new Error (
13
- 'FDC3 DesktopAgent not available at `window.fdc3`.'
14
- ) ;
4
+ const DEFAULT_TIMEOUT = 5000 ;
15
5
16
- const rejectIfNoGlobal = ( f : ( ) => Promise < any > ) => {
17
- return window . fdc3 ? f ( ) : Promise . reject ( unavailableError ) ;
18
- } ;
6
+ const UnavailableError = new Error ( 'FDC3 DesktopAgent not available at `window.fdc3`.' ) ;
7
+ const TimeoutError = new Error ( 'Timed out waiting for `fdc3Ready` event.' ) ;
8
+ const UnexpectedError = new Error ( '`fdc3Ready` event fired, but `window.fdc3` not set to DesktopAgent.' ) ;
9
+
10
+ function rejectIfNoGlobal ( f : ( ) => Promise < any > ) {
11
+ return window . fdc3 ? f ( ) : Promise . reject ( UnavailableError ) ;
12
+ }
19
13
20
- const throwIfNoGlobal = ( f : ( ) => any ) => {
14
+ function throwIfNoGlobal ( f : ( ) => any ) {
21
15
if ( ! window . fdc3 ) {
22
- throw unavailableError ;
16
+ throw UnavailableError ;
23
17
}
24
18
return f ( ) ;
19
+ }
20
+
21
+ export const fdc3Ready = async ( waitForMs = DEFAULT_TIMEOUT ) : Promise < void > => {
22
+ return new Promise ( ( resolve , reject ) => {
23
+ // if the global is already available resolve immediately
24
+ if ( window . fdc3 ) {
25
+ resolve ( ) ;
26
+ } else {
27
+ // if its not available setup a timeout to return a rejected promise
28
+ const timeout = setTimeout ( ( ) => ( window . fdc3 ? resolve ( ) : reject ( TimeoutError ) ) , waitForMs ) ;
29
+ // listen for the fdc3Ready event
30
+ window . addEventListener (
31
+ 'fdc3Ready' ,
32
+ ( ) => {
33
+ clearTimeout ( timeout ) ;
34
+ window . fdc3 ? resolve ( ) : reject ( UnexpectedError ) ;
35
+ } ,
36
+ { once : true }
37
+ ) ;
38
+ }
39
+ } ) ;
25
40
} ;
26
41
27
- export const open : ( app : TargetApp , context ?: Context ) => Promise < void > = (
28
- app ,
29
- context
30
- ) => {
42
+ export function open ( app : TargetApp , context ?: Context ) : Promise < void > {
31
43
return rejectIfNoGlobal ( ( ) => window . fdc3 . open ( app , context ) ) ;
32
- } ;
44
+ }
33
45
34
- export const findIntent : (
35
- intent : string ,
36
- context ?: Context
37
- ) => Promise < AppIntent > = ( intent , context ) => {
46
+ export function findIntent ( intent : string , context ?: Context ) : Promise < AppIntent > {
38
47
return rejectIfNoGlobal ( ( ) => window . fdc3 . findIntent ( intent , context ) ) ;
39
- } ;
48
+ }
40
49
41
- export const findIntentsByContext : (
42
- context : Context
43
- ) => Promise < Array < AppIntent > > = context => {
50
+ export function findIntentsByContext ( context : Context ) : Promise < AppIntent [ ] > {
44
51
return rejectIfNoGlobal ( ( ) => window . fdc3 . findIntentsByContext ( context ) ) ;
45
- } ;
52
+ }
46
53
47
- export const broadcast : ( context : Context ) => void = context => {
54
+ export function broadcast ( context : Context ) : void {
48
55
throwIfNoGlobal ( ( ) => window . fdc3 . broadcast ( context ) ) ;
49
- } ;
56
+ }
50
57
51
- export const raiseIntent : (
52
- intent : string ,
53
- context : Context ,
54
- app ?: TargetApp
55
- ) => Promise < IntentResolution > = ( intent , context , app ) => {
58
+ export function raiseIntent ( intent : string , context : Context , app ?: TargetApp ) : Promise < IntentResolution > {
56
59
return rejectIfNoGlobal ( ( ) => window . fdc3 . raiseIntent ( intent , context , app ) ) ;
57
- } ;
60
+ }
58
61
59
- export const raiseIntentForContext : (
60
- context : Context ,
61
- app ?: TargetApp
62
- ) => Promise < IntentResolution > = ( context , app ) => {
63
- return rejectIfNoGlobal ( ( ) =>
64
- window . fdc3 . raiseIntentForContext ( context , app )
65
- ) ;
66
- } ;
62
+ export function raiseIntentForContext ( context : Context , app ?: TargetApp ) : Promise < IntentResolution > {
63
+ return rejectIfNoGlobal ( ( ) => window . fdc3 . raiseIntentForContext ( context , app ) ) ;
64
+ }
67
65
68
- export const addIntentListener : (
69
- intent : string ,
70
- handler : ContextHandler
71
- ) => Listener = ( intent , handler ) => {
66
+ export function addIntentListener ( intent : string , handler : ContextHandler ) : Listener {
72
67
return throwIfNoGlobal ( ( ) => window . fdc3 . addIntentListener ( intent , handler ) ) ;
73
- } ;
68
+ }
74
69
75
- export const addContextListener : (
76
- contextTypeOrHandler : string | ContextHandler ,
77
- handler ?: ContextHandler
78
- ) => Listener = ( a , b ) => {
79
- if ( typeof a !== 'function' ) {
70
+ export function addContextListener ( contextTypeOrHandler : string | ContextHandler , handler ?: ContextHandler ) : Listener {
71
+ if ( typeof contextTypeOrHandler !== 'function' ) {
80
72
return throwIfNoGlobal ( ( ) =>
81
- window . fdc3 . addContextListener ( a as string , b as ContextHandler )
73
+ window . fdc3 . addContextListener ( contextTypeOrHandler as string , handler as ContextHandler )
82
74
) ;
83
75
} else {
84
- return throwIfNoGlobal ( ( ) =>
85
- window . fdc3 . addContextListener ( a as ContextHandler )
86
- ) ;
76
+ return throwIfNoGlobal ( ( ) => window . fdc3 . addContextListener ( contextTypeOrHandler as ContextHandler ) ) ;
87
77
}
88
- } ;
78
+ }
89
79
90
- export const getSystemChannels : ( ) => Promise < Array < Channel > > = ( ) = > {
80
+ export function getSystemChannels ( ) : Promise < Channel [ ] > {
91
81
return rejectIfNoGlobal ( ( ) => window . fdc3 . getSystemChannels ( ) ) ;
92
- } ;
82
+ }
93
83
94
- export const joinChannel : ( channelId : string ) => Promise < void > = channelId = > {
84
+ export function joinChannel ( channelId : string ) : Promise < void > {
95
85
return rejectIfNoGlobal ( ( ) => window . fdc3 . joinChannel ( channelId ) ) ;
96
- } ;
86
+ }
97
87
98
- export const getOrCreateChannel : (
99
- channelId : string
100
- ) => Promise < Channel > = channelId => {
88
+ export function getOrCreateChannel ( channelId : string ) : Promise < Channel > {
101
89
return rejectIfNoGlobal ( ( ) => window . fdc3 . getOrCreateChannel ( channelId ) ) ;
102
- } ;
90
+ }
103
91
104
- export const getCurrentChannel : ( ) => Promise < Channel | null > = ( ) = > {
92
+ export function getCurrentChannel ( ) : Promise < Channel | null > {
105
93
return rejectIfNoGlobal ( ( ) => window . fdc3 . getCurrentChannel ( ) ) ;
106
- } ;
94
+ }
107
95
108
- export const leaveCurrentChannel : ( ) => Promise < void > = ( ) = > {
96
+ export function leaveCurrentChannel ( ) : Promise < void > {
109
97
return rejectIfNoGlobal ( ( ) => window . fdc3 . leaveCurrentChannel ( ) ) ;
110
- } ;
98
+ }
99
+
100
+ export function getInfo ( ) : ImplementationMetadata {
101
+ return throwIfNoGlobal ( ( ) => window . fdc3 . getInfo ( ) ) ;
102
+ }
111
103
112
104
/**
113
105
* Compare numeric semver version number strings (in the form `1.2.3`).
@@ -119,19 +111,12 @@ export const leaveCurrentChannel: () => Promise<void> = () => {
119
111
* @param a
120
112
* @param b
121
113
*/
122
- export const compareVersionNumbers : ( a : string , b : string ) => number | null = (
123
- a ,
124
- b
125
- ) => {
114
+ export const compareVersionNumbers : ( a : string , b : string ) => number | null = ( a , b ) => {
126
115
try {
127
116
let aVerArr = a . split ( '.' ) . map ( Number ) ;
128
117
let bVerArr = b . split ( '.' ) . map ( Number ) ;
129
- for (
130
- let index = 0 ;
131
- index < Math . max ( aVerArr . length , bVerArr . length ) ;
132
- index ++
133
- ) {
134
- /* If one version number has more digits and the other does not, and they are otherwise equal,
118
+ for ( let index = 0 ; index < Math . max ( aVerArr . length , bVerArr . length ) ; index ++ ) {
119
+ /* If one version number has more digits and the other does not, and they are otherwise equal,
135
120
assume the longer is greater. E.g. 1.1.1 > 1.1 */
136
121
if ( index === aVerArr . length || aVerArr [ index ] < bVerArr [ index ] ) {
137
122
return - 1 ;
@@ -155,10 +140,10 @@ export const compareVersionNumbers: (a: string, b: string) => number | null = (
155
140
* @param metadata
156
141
* @param version
157
142
*/
158
- export const versionIsAtLeast : (
159
- metadata : ImplementationMetadata ,
160
- version : string
161
- ) => boolean | null = ( metadata , version ) => {
143
+ export const versionIsAtLeast : ( metadata : ImplementationMetadata , version : string ) => boolean | null = (
144
+ metadata ,
145
+ version
146
+ ) => {
162
147
let comparison = compareVersionNumbers ( metadata . fdc3Version , version ) ;
163
148
return comparison === null ? null : comparison >= 0 ? true : false ;
164
149
} ;
0 commit comments