react-native-navigation的迁移库

Commands.ts 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. this.layoutTreeCrawler.crawl(root);
  23. const modals = _.map(input.modals, (modal) => {
  24. const modalLayout = this.layoutTreeParser.parse(modal);
  25. this.layoutTreeCrawler.crawl(modalLayout);
  26. return modalLayout;
  27. });
  28. const overlays = _.map(input.overlays, (overlay) => {
  29. const overlayLayout = this.layoutTreeParser.parse(overlay);
  30. this.layoutTreeCrawler.crawl(overlayLayout);
  31. return overlayLayout;
  32. });
  33. const commandId = this.uniqueIdProvider.generate('setRoot');
  34. const result = this.nativeCommandsSender.setRoot(commandId, { root, modals, overlays });
  35. this.commandsObserver.notify('setRoot', { commandId, layout: { root, modals, overlays } });
  36. return result;
  37. }
  38. public setDefaultOptions(options: Options) {
  39. const input = _.cloneDeep(options);
  40. this.optionsProcessor.processOptions(input);
  41. this.nativeCommandsSender.setDefaultOptions(input);
  42. this.commandsObserver.notify('setDefaultOptions', { options });
  43. }
  44. public mergeOptions(componentId: string, options: Options) {
  45. const input = _.cloneDeep(options);
  46. this.optionsProcessor.processOptions(input);
  47. this.nativeCommandsSender.mergeOptions(componentId, input);
  48. this.commandsObserver.notify('mergeOptions', { componentId, options });
  49. }
  50. public showModal(layout: Layout) {
  51. const layoutCloned = _.cloneDeep(layout);
  52. const layoutNode = this.layoutTreeParser.parse(layoutCloned);
  53. this.layoutTreeCrawler.crawl(layoutNode);
  54. const commandId = this.uniqueIdProvider.generate('showModal');
  55. const result = this.nativeCommandsSender.showModal(commandId, layoutNode);
  56. this.commandsObserver.notify('showModal', { commandId, layout: layoutNode });
  57. return result;
  58. }
  59. public dismissModal(componentId: string, mergeOptions?: Options) {
  60. const commandId = this.uniqueIdProvider.generate('dismissModal');
  61. const result = this.nativeCommandsSender.dismissModal(commandId, componentId, mergeOptions);
  62. this.commandsObserver.notify('dismissModal', { commandId, componentId, mergeOptions});
  63. return result;
  64. }
  65. public dismissAllModals(mergeOptions?: Options) {
  66. const commandId = this.uniqueIdProvider.generate('dismissAllModals');
  67. const result = this.nativeCommandsSender.dismissAllModals(commandId, mergeOptions);
  68. this.commandsObserver.notify('dismissAllModals', { commandId, mergeOptions });
  69. return result;
  70. }
  71. public push(componentId: string, simpleApi: Layout) {
  72. const input = _.cloneDeep(simpleApi);
  73. const layout = this.layoutTreeParser.parse(input);
  74. this.layoutTreeCrawler.crawl(layout);
  75. const commandId = this.uniqueIdProvider.generate('push');
  76. const result = this.nativeCommandsSender.push(commandId, componentId, layout);
  77. this.commandsObserver.notify('push', { commandId, componentId, layout });
  78. return result;
  79. }
  80. public pop(componentId: string, mergeOptions?: Options) {
  81. const commandId = this.uniqueIdProvider.generate('pop');
  82. const result = this.nativeCommandsSender.pop(commandId, componentId, mergeOptions);
  83. this.commandsObserver.notify('pop', { commandId, componentId, mergeOptions });
  84. return result;
  85. }
  86. public popTo(componentId: string, mergeOptions?: Options) {
  87. const commandId = this.uniqueIdProvider.generate('popTo');
  88. const result = this.nativeCommandsSender.popTo(commandId, componentId, mergeOptions);
  89. this.commandsObserver.notify('popTo', { commandId, componentId, mergeOptions });
  90. return result;
  91. }
  92. public popToRoot(componentId: string, mergeOptions?: Options) {
  93. const commandId = this.uniqueIdProvider.generate('popToRoot');
  94. const result = this.nativeCommandsSender.popToRoot(commandId, componentId, mergeOptions);
  95. this.commandsObserver.notify('popToRoot', { commandId, componentId, mergeOptions });
  96. return result;
  97. }
  98. public setStackRoot(componentId: string, children: Layout[]) {
  99. const input = _.map(_.cloneDeep(children), (simpleApi) => {
  100. const layout = this.layoutTreeParser.parse(simpleApi);
  101. this.layoutTreeCrawler.crawl(layout);
  102. return layout;
  103. });
  104. const commandId = this.uniqueIdProvider.generate('setStackRoot');
  105. const result = this.nativeCommandsSender.setStackRoot(commandId, componentId, input);
  106. this.commandsObserver.notify('setStackRoot', { commandId, componentId, layout: input });
  107. return result;
  108. }
  109. public showOverlay(simpleApi: Layout) {
  110. const input = _.cloneDeep(simpleApi);
  111. const layout = this.layoutTreeParser.parse(input);
  112. this.layoutTreeCrawler.crawl(layout);
  113. const commandId = this.uniqueIdProvider.generate('showOverlay');
  114. const result = this.nativeCommandsSender.showOverlay(commandId, layout);
  115. this.commandsObserver.notify('showOverlay', { commandId, layout });
  116. return result;
  117. }
  118. public dismissOverlay(componentId: string) {
  119. const commandId = this.uniqueIdProvider.generate('dismissOverlay');
  120. const result = this.nativeCommandsSender.dismissOverlay(commandId, componentId);
  121. this.commandsObserver.notify('dismissOverlay', { commandId, componentId });
  122. return result;
  123. }
  124. public getLaunchArgs() {
  125. const commandId = this.uniqueIdProvider.generate('getLaunchArgs');
  126. const result = this.nativeCommandsSender.getLaunchArgs(commandId);
  127. this.commandsObserver.notify('getLaunchArgs', { commandId });
  128. return result;
  129. }
  130. }