-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDemoGroup.ts
52 lines (42 loc) · 1.11 KB
/
DemoGroup.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
import { Configure } from './configContext';
import { Demo } from './Demo';
export class DemosGroup<P> {
static isDemoGroup(thing: unknown): thing is DemosGroup<unknown> {
return thing instanceof DemosGroup;
}
parent?: DemosGroup<P>;
constructor(
public readonly label: string,
public readonly children: Array<Demo<P> | DemosGroup<P>>,
private _configure?: Configure<P>,
) {
for (const child of this.children) {
child.parent = this;
}
}
async configure(props: P): Promise<P> {
if (this.parent) {
props = await this.parent.configure({ ...props });
}
if (this._configure) {
props = await this._configure({ ...props });
}
return props;
}
findDemo(path: string[]): Demo<unknown> | undefined {
if (path[0] !== this.label) {
return;
}
for (const child of this.children) {
if (Demo.isDemo(child) && child.label === path[1]) {
return child;
}
if (DemosGroup.isDemoGroup(child)) {
const found = child.findDemo(path.slice(1));
if (found) {
return found;
}
}
}
}
}