-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathBasicDirectory.ts
90 lines (78 loc) · 2.46 KB
/
BasicDirectory.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
import { Directory, DirectoryApp, DirectoryIntent } from './DirectoryInterface';
export function genericResultTypeSame(real: string | undefined, required: string | undefined) {
if (required == undefined) {
return true;
} else if (real == required) {
return true;
} else if (real == undefined) {
return false; // required is not undefined, so asking for something
} else if (real.startsWith('channel<') && required == 'channel') {
return true;
} else {
return false;
}
}
/**
* Basic directory implementation that allows queries over a set of apps.
*/
export class BasicDirectory implements Directory {
allApps: DirectoryApp[];
constructor(apps: DirectoryApp[]) {
this.allApps = apps;
}
private intentMatches(
i: DirectoryIntent,
contextType: string | undefined,
intentName: string | undefined,
resultType: string | undefined
): boolean {
const out =
(intentName == undefined || i.intentName == intentName) &&
(contextType == undefined || (i.contexts ?? []).includes(contextType)) &&
genericResultTypeSame(i.resultType, resultType);
return out;
}
private retrieveIntentsForApp(a: DirectoryApp): DirectoryIntent[] {
const lf = a.interop?.intents?.listensFor ?? {};
const lfa = Object.entries(lf);
const lfAugmented = lfa.map(([key, value]) => {
return {
intentName: key,
...value,
appId: a.appId,
};
});
return lfAugmented;
}
retrieveAllIntents(): DirectoryIntent[] {
const allIntents = this.retrieveAllApps().flatMap(a => this.retrieveIntentsForApp(a));
return allIntents;
}
retrieveIntents(
contextType: string | undefined,
intentName: string | undefined,
resultType: string | undefined
): DirectoryIntent[] {
const matchingIntents = this.retrieveAllIntents().filter(i =>
this.intentMatches(i, contextType, intentName, resultType)
);
return matchingIntents;
}
retrieveApps(
contextType: string | undefined,
intentName?: string | undefined,
resultType?: string | undefined
): DirectoryApp[] {
const result = this.retrieveAllApps().filter(
a =>
this.retrieveIntentsForApp(a).filter(i => this.intentMatches(i, contextType, intentName, resultType)).length > 0
);
return result;
}
retrieveAppsById(appId: string): DirectoryApp[] {
return this.retrieveAllApps().filter(a => a.appId == appId);
}
retrieveAllApps(): DirectoryApp[] {
return this.allApps;
}
}