-
Notifications
You must be signed in to change notification settings - Fork 692
/
container.ts
65 lines (58 loc) · 2.14 KB
/
container.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
import { Reflection, TraverseCallback, TraverseProperty } from "./abstract";
import { ReflectionCategory } from "../ReflectionCategory";
import { ReflectionGroup } from "../ReflectionGroup";
import type { ReflectionKind } from "./kind";
import type { Serializer, JSONOutput, Deserializer } from "../../serialization";
import type { DeclarationReflection } from "./declaration";
export abstract class ContainerReflection extends Reflection {
/**
* The children of this reflection.
*/
children?: DeclarationReflection[];
/**
* All children grouped by their kind.
*/
groups?: ReflectionGroup[];
/**
* All children grouped by their category.
*/
categories?: ReflectionCategory[];
/**
* Return a list of all children of a certain kind.
*
* @param kind The desired kind of children.
* @returns An array containing all children with the desired kind.
*/
getChildrenByKind(kind: ReflectionKind): DeclarationReflection[] {
return (this.children || []).filter((child) => child.kindOf(kind));
}
override traverse(callback: TraverseCallback) {
for (const child of this.children?.slice() || []) {
if (callback(child, TraverseProperty.Children) === false) {
return;
}
}
}
override toObject(serializer: Serializer): JSONOutput.ContainerReflection {
return {
...super.toObject(serializer),
children: serializer.toObjectsOptional(this.children),
groups: serializer.toObjectsOptional(this.groups),
categories: serializer.toObjectsOptional(this.categories),
};
}
override fromObject(de: Deserializer, obj: JSONOutput.ContainerReflection) {
super.fromObject(de, obj);
this.children = de.reviveMany(obj.children, (child) =>
de.constructReflection(child)
);
this.groups = de.reviveMany(
obj.groups,
(group) => new ReflectionGroup(group.title)
);
this.categories = de.reviveMany(
obj.categories,
(cat) => new ReflectionCategory(cat.title)
);
}
}