-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathBasicDesktopAgent.ts
156 lines (131 loc) · 4.25 KB
/
BasicDesktopAgent.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
import {
AppIdentifier,
AppMetadata,
ContextHandler,
DesktopAgent,
EventHandler,
FDC3EventTypes,
ImplementationMetadata,
IntentHandler,
IntentResolution,
Listener,
} from '@kite9/fdc3-standard';
import { ChannelSupport } from './channels/ChannelSupport';
import { AppSupport } from './apps/AppSupport';
import { IntentSupport } from './intents/IntentSupport';
import { HandshakeSupport } from './handshake/HandshakeSupport';
import { Connectable } from '@kite9/fdc3-standard';
import { Context } from '@kite9/fdc3-context';
/**
* This splits out the functionality of the desktop agent into
* app, channels and intents concerns.
*/
export class BasicDesktopAgent implements DesktopAgent, Connectable {
readonly handshake: HandshakeSupport;
readonly channels: ChannelSupport;
readonly intents: IntentSupport;
readonly apps: AppSupport;
readonly connectables: Connectable[];
constructor(
handshake: HandshakeSupport,
channels: ChannelSupport,
intents: IntentSupport,
apps: AppSupport,
connectables: Connectable[]
) {
this.handshake = handshake;
this.intents = intents;
this.channels = channels;
this.apps = apps;
this.connectables = connectables;
}
async addEventListener(type: FDC3EventTypes | null, handler: EventHandler): Promise<Listener> {
switch (type) {
case 'userChannelChanged':
return this.channels.addChannelChangedEventHandler(handler);
default:
throw new Error('UnknownEventType');
}
}
async getInfo(): Promise<ImplementationMetadata> {
return this.handshake.getImplementationMetadata();
}
async broadcast(context: Context): Promise<void> {
const channel = await this.channels.getUserChannel();
if (channel) {
return channel.broadcast(context);
} else {
return Promise.resolve();
}
}
async addContextListener(context: ContextHandler | string | null, handler?: ContextHandler): Promise<Listener> {
const theHandler: ContextHandler = handler ? handler : (context as ContextHandler);
const theContextType: string | null = context && handler ? (context as string) : null;
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() {
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?: any): 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?: any) {
return this.intents.raiseIntent(intent, context, this.ensureAppId(app));
}
addIntentListener(intent: string, handler: IntentHandler) {
return this.intents.addIntentListener(intent, handler);
}
raiseIntentForContext(context: Context, app?: any): Promise<IntentResolution> {
return this.intents.raiseIntentForContext(context, this.ensureAppId(app));
}
open(app: any, 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()));
}
}