forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.ts
153 lines (128 loc) · 6.13 KB
/
Commands.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
import * as _ from 'lodash';
import { CommandsObserver } from '../events/CommandsObserver';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
import { Options } from '../interfaces/Options';
import { Layout, LayoutRoot } from '../interfaces/Layout';
import { LayoutTreeParser } from './LayoutTreeParser';
import { LayoutTreeCrawler } from './LayoutTreeCrawler';
export class Commands {
constructor(
private readonly nativeCommandsSender: NativeCommandsSender,
private readonly layoutTreeParser: LayoutTreeParser,
private readonly layoutTreeCrawler: LayoutTreeCrawler,
private readonly commandsObserver: CommandsObserver,
private readonly uniqueIdProvider: UniqueIdProvider) {
}
public setRoot(simpleApi: LayoutRoot) {
const input = _.cloneDeep(simpleApi);
const root = this.layoutTreeParser.parse(input.root);
this.layoutTreeCrawler.crawl(root);
const modals = _.map(input.modals, (modal) => {
const modalLayout = this.layoutTreeParser.parse(modal);
this.layoutTreeCrawler.crawl(modalLayout);
return modalLayout;
});
const overlays = _.map(input.overlays, (overlay) => {
const overlayLayout = this.layoutTreeParser.parse(overlay);
this.layoutTreeCrawler.crawl(overlayLayout);
return overlayLayout;
});
const commandId = this.uniqueIdProvider.generate('setRoot');
const result = this.nativeCommandsSender.setRoot(commandId, { root, modals, overlays });
this.commandsObserver.notify('setRoot', { commandId, layout: { root, modals, overlays } });
return result;
}
public setDefaultOptions(options: Options) {
const input = _.cloneDeep(options);
this.layoutTreeCrawler.processOptions(input);
this.nativeCommandsSender.setDefaultOptions(input);
this.commandsObserver.notify('setDefaultOptions', { options });
}
public mergeOptions(componentId: string, options: Options) {
const input = _.cloneDeep(options);
this.layoutTreeCrawler.processOptions(input);
this.nativeCommandsSender.mergeOptions(componentId, input);
this.commandsObserver.notify('mergeOptions', { componentId, options });
}
public showModal(simpleApi: Layout) {
const input = _.cloneDeep(simpleApi);
const layout = this.layoutTreeParser.parse(input);
this.layoutTreeCrawler.crawl(layout);
const commandId = this.uniqueIdProvider.generate('showModal');
const result = this.nativeCommandsSender.showModal(commandId, layout);
this.commandsObserver.notify('showModal', { commandId, layout });
return result;
}
public dismissModal(componentId: string, mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('dismissModal');
const result = this.nativeCommandsSender.dismissModal(commandId, componentId, mergeOptions);
this.commandsObserver.notify('dismissModal', { commandId, componentId, mergeOptions});
return result;
}
public dismissAllModals(mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('dismissAllModals');
const result = this.nativeCommandsSender.dismissAllModals(commandId, mergeOptions);
this.commandsObserver.notify('dismissAllModals', { commandId, mergeOptions });
return result;
}
public push(componentId: string, simpleApi: Layout) {
const input = _.cloneDeep(simpleApi);
const layout = this.layoutTreeParser.parse(input);
this.layoutTreeCrawler.crawl(layout);
const commandId = this.uniqueIdProvider.generate('push');
const result = this.nativeCommandsSender.push(commandId, componentId, layout);
this.commandsObserver.notify('push', { commandId, componentId, layout });
return result;
}
public pop(componentId: string, mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('pop');
const result = this.nativeCommandsSender.pop(commandId, componentId, mergeOptions);
this.commandsObserver.notify('pop', { commandId, componentId, mergeOptions });
return result;
}
public popTo(componentId: string, mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('popTo');
const result = this.nativeCommandsSender.popTo(commandId, componentId, mergeOptions);
this.commandsObserver.notify('popTo', { commandId, componentId, mergeOptions });
return result;
}
public popToRoot(componentId: string, mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('popToRoot');
const result = this.nativeCommandsSender.popToRoot(commandId, componentId, mergeOptions);
this.commandsObserver.notify('popToRoot', { commandId, componentId, mergeOptions });
return result;
}
public setStackRoot(componentId: string, children: Layout[]) {
const input = _.map(_.cloneDeep(children), (simpleApi) => {
const layout = this.layoutTreeParser.parse(simpleApi);
this.layoutTreeCrawler.crawl(layout);
return layout;
});
const commandId = this.uniqueIdProvider.generate('setStackRoot');
const result = this.nativeCommandsSender.setStackRoot(commandId, componentId, input);
this.commandsObserver.notify('setStackRoot', { commandId, componentId, layout: input });
return result;
}
public showOverlay(simpleApi: Layout) {
const input = _.cloneDeep(simpleApi);
const layout = this.layoutTreeParser.parse(input);
this.layoutTreeCrawler.crawl(layout);
const commandId = this.uniqueIdProvider.generate('showOverlay');
const result = this.nativeCommandsSender.showOverlay(commandId, layout);
this.commandsObserver.notify('showOverlay', { commandId, layout });
return result;
}
public dismissOverlay(componentId: string) {
const commandId = this.uniqueIdProvider.generate('dismissOverlay');
const result = this.nativeCommandsSender.dismissOverlay(commandId, componentId);
this.commandsObserver.notify('dismissOverlay', { commandId, componentId });
return result;
}
public getLaunchArgs() {
const commandId = this.uniqueIdProvider.generate('getLaunchArgs');
const result = this.nativeCommandsSender.getLaunchArgs(commandId);
this.commandsObserver.notify('getLaunchArgs', { commandId });
return result;
}
}