react-native-navigation的迁移库

Commands.ts 6.6KB

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