react-native-navigation的迁移库

Commands.ts 6.3KB

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