forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayoutTreeCrawler.ts
46 lines (40 loc) · 1.27 KB
/
LayoutTreeCrawler.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
import { LayoutType } from './LayoutType';
import { OptionsProcessor } from './OptionsProcessor';
import { Store } from '../components/Store';
import { CommandName } from '../interfaces/CommandName';
export interface Data {
name?: string;
options?: any;
passProps?: any;
}
export interface LayoutNode {
id: string;
type: LayoutType;
data: Data;
children: LayoutNode[];
}
export class LayoutTreeCrawler {
constructor(public readonly store: Store, private readonly optionsProcessor: OptionsProcessor) {
this.crawl = this.crawl.bind(this);
}
crawl(node: LayoutNode, commandName: CommandName): void {
if (node.type === LayoutType.Component) {
this.handleComponent(node);
}
this.optionsProcessor.processOptions(commandName, node.data.options);
node.children.forEach((value: LayoutNode) => this.crawl(value, commandName));
}
private handleComponent(node: LayoutNode) {
this.assertComponentDataName(node);
this.savePropsToStore(node);
node.data.passProps = undefined;
}
private savePropsToStore(node: LayoutNode) {
this.store.setPendingProps(node.id, node.data.passProps);
}
private assertComponentDataName(component: LayoutNode) {
if (!component.data.name) {
throw new Error('Missing component data.name');
}
}
}