-
Notifications
You must be signed in to change notification settings - Fork 132
/
DefaultDesktopAgentIntentResolver.ts
62 lines (51 loc) · 2.42 KB
/
DefaultDesktopAgentIntentResolver.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
import { AppIntent } from "@kite9/fdc3-standard";
import { IntentResolver, IntentResolutionChoice } from '@kite9/fdc3-standard'
import { AbstractUIComponent } from "./AbstractUIComponent";
import { BrowserTypes } from "@kite9/fdc3-schema";
import { Context } from "@kite9/fdc3-context";
import { FDC3_USER_INTERFACE_RESOLVE_ACTION_TYPE, FDC3_USER_INTERFACE_RESOLVE_TYPE } from "@kite9/fdc3-schema/generated/api/BrowserTypes";
type Fdc3UserInterfaceResolveAction = BrowserTypes.Fdc3UserInterfaceResolveAction
type Fdc3UserInterfaceResolve = BrowserTypes.Fdc3UserInterfaceResolve
/**
* Works with the desktop agent to provide a resolution to the intent choices.
* This is the default implementation, but can be overridden by app implementers calling
* the getAgent() method
*/
export class DefaultDesktopAgentIntentResolver extends AbstractUIComponent implements IntentResolver {
private pendingResolve: ((x: IntentResolutionChoice | void) => void) | null = null
constructor(url: string | null) {
super(url ?? "https://fdc3.finos.org/webui/intent_resolver.html", "FDC3 Intent Resolver")
}
async setupMessagePort(port: MessagePort): Promise<void> {
await super.setupMessagePort(port)
this.port = port
this.port.addEventListener("message", (e) => {
console.log("Got resolve action")
if (e.data.type == FDC3_USER_INTERFACE_RESOLVE_ACTION_TYPE) {
const choice = e.data as Fdc3UserInterfaceResolveAction
if ((choice.payload.action == 'click') && (this.pendingResolve)) {
this.pendingResolve({
appId: choice.payload.appIdentifier!!,
intent: choice.payload.intent!!
})
} else if ((choice.payload.action == 'cancel') && (this.pendingResolve)) {
this.pendingResolve()
}
this.pendingResolve = null
}
})
}
async chooseIntent(appIntents: AppIntent[], context: Context): Promise<IntentResolutionChoice | void> {
const out = new Promise<IntentResolutionChoice | void>((resolve, _reject) => {
this.pendingResolve = resolve
})
this.port?.postMessage({
type: FDC3_USER_INTERFACE_RESOLVE_TYPE,
payload: {
appIntents,
context
}
} as Fdc3UserInterfaceResolve)
return out
}
}