@@ -7,70 +7,85 @@ import {
7
7
Listener ,
8
8
} from '..' ;
9
9
10
+ const unavailableError = new Error (
11
+ 'FDC3 DesktopAgent not available at `window.fdc3`.'
12
+ ) ;
13
+
14
+ const rejectIfNoGlobal = ( f : ( ) => Promise < any > ) => {
15
+ return window . fdc3 ? f ( ) : Promise . reject ( unavailableError ) ;
16
+ } ;
17
+
18
+ const throwIfNoGlobal = ( f : ( ) => any ) => {
19
+ if ( ! window . fdc3 ) {
20
+ throw unavailableError ;
21
+ }
22
+ return f ( ) ;
23
+ } ;
24
+
10
25
export const open : ( name : string , context ?: Context ) => Promise < void > = (
11
26
name ,
12
27
context
13
28
) => {
14
- return window . fdc3 . open ( name , context ) ;
29
+ return rejectIfNoGlobal ( ( ) => window . fdc3 . open ( name , context ) ) ;
15
30
} ;
16
31
17
32
export const findIntent : (
18
33
intent : string ,
19
34
context ?: Context
20
35
) => Promise < AppIntent > = ( intent , context ) => {
21
- return window . fdc3 . findIntent ( intent , context ) ;
36
+ return rejectIfNoGlobal ( ( ) => window . fdc3 . findIntent ( intent , context ) ) ;
22
37
} ;
23
38
24
39
export const findIntentsByContext : (
25
40
context : Context
26
41
) => Promise < Array < AppIntent > > = context => {
27
- return window . fdc3 . findIntentsByContext ( context ) ;
42
+ return rejectIfNoGlobal ( ( ) => window . fdc3 . findIntentsByContext ( context ) ) ;
28
43
} ;
29
44
30
45
export const broadcast : ( context : Context ) => void = context => {
31
- window . fdc3 . broadcast ( context ) ;
46
+ throwIfNoGlobal ( ( ) => window . fdc3 . broadcast ( context ) ) ;
32
47
} ;
33
48
34
49
export const raiseIntent : (
35
50
intent : string ,
36
51
context : Context ,
37
52
target ?: string
38
53
) => Promise < IntentResolution > = ( intent , context , target ) => {
39
- return window . fdc3 . raiseIntent ( intent , context , target ) ;
54
+ return rejectIfNoGlobal ( ( ) => window . fdc3 . raiseIntent ( intent , context , target ) ) ;
40
55
} ;
41
56
42
57
export const addIntentListener : (
43
58
intent : string ,
44
59
handler : ContextHandler
45
60
) => Listener = ( intent , handler ) => {
46
- return window . fdc3 . addIntentListener ( intent , handler ) ;
61
+ return throwIfNoGlobal ( ( ) => window . fdc3 . addIntentListener ( intent , handler ) ) ;
47
62
} ;
48
63
49
64
export const addContextListener : (
50
65
contextTypeOrHandler : string | ContextHandler ,
51
66
handler ?: ContextHandler
52
67
) => Listener = ( a , b ) => {
53
68
if ( typeof a !== 'function' ) {
54
- return window . fdc3 . addContextListener ( a as string , b as ContextHandler ) ;
69
+ return throwIfNoGlobal ( ( ) =>
70
+ window . fdc3 . addContextListener ( a as string , b as ContextHandler )
71
+ ) ;
55
72
} else {
56
- return window . fdc3 . addContextListener ( a as ContextHandler ) ;
73
+ return throwIfNoGlobal ( ( ) =>
74
+ window . fdc3 . addContextListener ( a as ContextHandler )
75
+ ) ;
57
76
}
58
77
} ;
59
78
60
79
export const getSystemChannels : ( ) => Promise < Array < Channel > > = ( ) => {
61
- return window . fdc3 . getSystemChannels ( ) ;
80
+ return rejectIfNoGlobal ( ( ) => window . fdc3 . getSystemChannels ( ) ) ;
62
81
} ;
63
82
64
83
export const joinChannel : ( channelId : string ) => Promise < void > = channelId => {
65
- return window . fdc3 . joinChannel ( channelId ) ;
84
+ return rejectIfNoGlobal ( ( ) => window . fdc3 . joinChannel ( channelId ) ) ;
66
85
} ;
67
86
68
87
export const getOrCreateChannel : (
69
88
channelId : string
70
89
) => Promise < Channel > = channelId => {
71
- return window . fdc3 . getOrCreateChannel ( channelId ) ;
72
- } ;
73
-
74
- export const getCurrentChannel : ( ) => Promise < Channel > = ( ) => {
75
- return window . fdc3 . getCurrentChannel ( ) ;
76
- } ;
90
+ return rejectIfNoGlobal ( ( ) => window . fdc3 . getOrCreateChannel ( channelId ) ) ;
91
+ } ;
0 commit comments