react-native-navigation的迁移库

Commands.ts 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import cloneDeep from 'lodash/cloneDeep'
  2. import map from 'lodash/map'
  3. import { CommandsObserver } from '../events/CommandsObserver';
  4. import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
  5. import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
  6. import { Options } from '../interfaces/Options';
  7. import { Layout, LayoutRoot } from '../interfaces/Layout';
  8. import { LayoutTreeParser } from './LayoutTreeParser';
  9. import { LayoutTreeCrawler } from './LayoutTreeCrawler';
  10. import { OptionsProcessor } from './OptionsProcessor';
  11. import { Store } from '../components/Store';
  12. export class Commands {
  13. constructor(
  14. private readonly store: Store,
  15. private readonly nativeCommandsSender: NativeCommandsSender,
  16. private readonly layoutTreeParser: LayoutTreeParser,
  17. private readonly layoutTreeCrawler: LayoutTreeCrawler,
  18. private readonly commandsObserver: CommandsObserver,
  19. private readonly uniqueIdProvider: UniqueIdProvider,
  20. private readonly optionsProcessor: OptionsProcessor
  21. ) {}
  22. public setRoot(simpleApi: LayoutRoot) {
  23. const input = cloneDeep(simpleApi);
  24. const root = this.layoutTreeParser.parse(input.root);
  25. const modals = map(input.modals, (modal) => {
  26. return this.layoutTreeParser.parse(modal);
  27. });
  28. const overlays = map(input.overlays, (overlay) => {
  29. return this.layoutTreeParser.parse(overlay);
  30. });
  31. const commandId = this.uniqueIdProvider.generate('setRoot');
  32. this.commandsObserver.notify('setRoot', { commandId, layout: { root, modals, overlays } });
  33. this.layoutTreeCrawler.crawl(root);
  34. modals.forEach((modalLayout) => {
  35. this.layoutTreeCrawler.crawl(modalLayout);
  36. });
  37. overlays.forEach((overlayLayout) => {
  38. this.layoutTreeCrawler.crawl(overlayLayout);
  39. });
  40. const result = this.nativeCommandsSender.setRoot(commandId, { root, modals, overlays });
  41. return result;
  42. }
  43. public setDefaultOptions(options: Options) {
  44. const input = cloneDeep(options);
  45. this.optionsProcessor.processOptions(input);
  46. this.nativeCommandsSender.setDefaultOptions(input);
  47. this.commandsObserver.notify('setDefaultOptions', { options });
  48. }
  49. public mergeOptions(componentId: string, options: Options) {
  50. const input = cloneDeep(options);
  51. this.optionsProcessor.processOptions(input);
  52. this.nativeCommandsSender.mergeOptions(componentId, input);
  53. this.commandsObserver.notify('mergeOptions', { componentId, options });
  54. }
  55. public updateProps(componentId: string, props: object) {
  56. const input = cloneDeep(props);
  57. this.store.updateProps(componentId, input);
  58. this.commandsObserver.notify('updateProps', { componentId, props });
  59. }
  60. public showModal(layout: Layout) {
  61. const layoutCloned = cloneDeep(layout);
  62. const layoutNode = this.layoutTreeParser.parse(layoutCloned);
  63. const commandId = this.uniqueIdProvider.generate('showModal');
  64. this.commandsObserver.notify('showModal', { commandId, layout: layoutNode });
  65. this.layoutTreeCrawler.crawl(layoutNode);
  66. const result = this.nativeCommandsSender.showModal(commandId, layoutNode);
  67. return result;
  68. }
  69. public dismissModal(componentId: string, mergeOptions?: Options) {
  70. const commandId = this.uniqueIdProvider.generate('dismissModal');
  71. const result = this.nativeCommandsSender.dismissModal(commandId, componentId, mergeOptions);
  72. this.commandsObserver.notify('dismissModal', { commandId, componentId, mergeOptions});
  73. return result;
  74. }
  75. public dismissAllModals(mergeOptions?: Options) {
  76. const commandId = this.uniqueIdProvider.generate('dismissAllModals');
  77. const result = this.nativeCommandsSender.dismissAllModals(commandId, mergeOptions);
  78. this.commandsObserver.notify('dismissAllModals', { commandId, mergeOptions });
  79. return result;
  80. }
  81. public push(componentId: string, simpleApi: Layout) {
  82. const input = cloneDeep(simpleApi);
  83. const layout = this.layoutTreeParser.parse(input);
  84. const commandId = this.uniqueIdProvider.generate('push');
  85. this.commandsObserver.notify('push', { commandId, componentId, layout });
  86. this.layoutTreeCrawler.crawl(layout);
  87. const result = this.nativeCommandsSender.push(commandId, componentId, layout);
  88. return result;
  89. }
  90. public pop(componentId: string, mergeOptions?: Options) {
  91. const commandId = this.uniqueIdProvider.generate('pop');
  92. const result = this.nativeCommandsSender.pop(commandId, componentId, mergeOptions);
  93. this.commandsObserver.notify('pop', { commandId, componentId, mergeOptions });
  94. return result;
  95. }
  96. public popTo(componentId: string, mergeOptions?: Options) {
  97. const commandId = this.uniqueIdProvider.generate('popTo');
  98. const result = this.nativeCommandsSender.popTo(commandId, componentId, mergeOptions);
  99. this.commandsObserver.notify('popTo', { commandId, componentId, mergeOptions });
  100. return result;
  101. }
  102. public popToRoot(componentId: string, mergeOptions?: Options) {
  103. const commandId = this.uniqueIdProvider.generate('popToRoot');
  104. const result = this.nativeCommandsSender.popToRoot(commandId, componentId, mergeOptions);
  105. this.commandsObserver.notify('popToRoot', { commandId, componentId, mergeOptions });
  106. return result;
  107. }
  108. public setStackRoot(componentId: string, children: Layout[]) {
  109. const input = map(cloneDeep(children), (simpleApi) => {
  110. const layout = this.layoutTreeParser.parse(simpleApi);
  111. return layout;
  112. });
  113. const commandId = this.uniqueIdProvider.generate('setStackRoot');
  114. this.commandsObserver.notify('setStackRoot', { commandId, componentId, layout: input });
  115. input.forEach((layoutNode) => {
  116. this.layoutTreeCrawler.crawl(layoutNode);
  117. });
  118. const result = this.nativeCommandsSender.setStackRoot(commandId, componentId, input);
  119. return result;
  120. }
  121. public showOverlay(simpleApi: Layout) {
  122. const input = cloneDeep(simpleApi);
  123. const layout = this.layoutTreeParser.parse(input);
  124. const commandId = this.uniqueIdProvider.generate('showOverlay');
  125. this.commandsObserver.notify('showOverlay', { commandId, layout });
  126. this.layoutTreeCrawler.crawl(layout);
  127. const result = this.nativeCommandsSender.showOverlay(commandId, layout);
  128. return result;
  129. }
  130. public dismissOverlay(componentId: string) {
  131. const commandId = this.uniqueIdProvider.generate('dismissOverlay');
  132. const result = this.nativeCommandsSender.dismissOverlay(commandId, componentId);
  133. this.commandsObserver.notify('dismissOverlay', { commandId, componentId });
  134. return result;
  135. }
  136. public getLaunchArgs() {
  137. const commandId = this.uniqueIdProvider.generate('getLaunchArgs');
  138. const result = this.nativeCommandsSender.getLaunchArgs(commandId);
  139. this.commandsObserver.notify('getLaunchArgs', { commandId });
  140. return result;
  141. }
  142. }