react-native-navigation的迁移库

Navigation.ts 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { NativeCommandsSender } from './adapters/NativeCommandsSender';
  2. import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
  3. import { UniqueIdProvider } from './adapters/UniqueIdProvider';
  4. import { Store } from './components/Store';
  5. import { ComponentRegistry } from './components/ComponentRegistry';
  6. import { Commands } from './commands/Commands';
  7. import { LayoutTreeParser } from './commands/LayoutTreeParser';
  8. import { LayoutTreeCrawler } from './commands/LayoutTreeCrawler';
  9. import { EventsRegistry } from './events/EventsRegistry';
  10. import { ComponentProvider } from 'react-native';
  11. import { Element } from './adapters/Element';
  12. import { CommandsObserver } from './events/CommandsObserver';
  13. import { Constants } from './adapters/Constants';
  14. import { ComponentType } from 'react';
  15. import { ComponentEventsObserver } from './events/ComponentEventsObserver';
  16. export class Navigation {
  17. public readonly Element: React.ComponentType<{ elementId: any; resizeMode?: any; }>;
  18. public readonly store: Store;
  19. private readonly nativeEventsReceiver: NativeEventsReceiver;
  20. private readonly uniqueIdProvider: UniqueIdProvider;
  21. private readonly componentRegistry: ComponentRegistry;
  22. private readonly layoutTreeParser: LayoutTreeParser;
  23. private readonly layoutTreeCrawler: LayoutTreeCrawler;
  24. private readonly nativeCommandsSender: NativeCommandsSender;
  25. private readonly commands: Commands;
  26. private readonly eventsRegistry: EventsRegistry;
  27. private readonly commandsObserver: CommandsObserver;
  28. private readonly componentEventsObserver: ComponentEventsObserver;
  29. constructor() {
  30. this.Element = Element;
  31. this.store = new Store();
  32. this.nativeEventsReceiver = new NativeEventsReceiver();
  33. this.uniqueIdProvider = new UniqueIdProvider();
  34. this.componentEventsObserver = new ComponentEventsObserver(this.nativeEventsReceiver);
  35. this.componentRegistry = new ComponentRegistry(this.store, this.componentEventsObserver);
  36. this.layoutTreeParser = new LayoutTreeParser();
  37. this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store);
  38. this.nativeCommandsSender = new NativeCommandsSender();
  39. this.commandsObserver = new CommandsObserver();
  40. this.commands = new Commands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler, this.commandsObserver, this.uniqueIdProvider);
  41. this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.commandsObserver, this.componentEventsObserver);
  42. this.componentEventsObserver.registerOnceForAllComponentEvents();
  43. }
  44. /**
  45. * Every navigation component in your app must be registered with a unique name.
  46. * The component itself is a traditional React component extending React.Component.
  47. */
  48. public registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): ComponentType<any> {
  49. return this.componentRegistry.registerComponent(componentName, getComponentClassFunc);
  50. }
  51. /**
  52. * Utility helper function like registerComponent,
  53. * wraps the provided component with a react-redux Provider with the passed redux store
  54. */
  55. public registerComponentWithRedux(componentName: string, getComponentClassFunc: ComponentProvider, ReduxProvider: any, reduxStore: any): ComponentType<any> {
  56. return this.componentRegistry.registerComponent(componentName, getComponentClassFunc, ReduxProvider, reduxStore);
  57. }
  58. /**
  59. * Reset the app to a new layout
  60. */
  61. public setRoot(layout): Promise<any> {
  62. return this.commands.setRoot(layout);
  63. }
  64. /**
  65. * Set default options to all screens. Useful for declaring a consistent style across the app.
  66. */
  67. public setDefaultOptions(options): void {
  68. this.commands.setDefaultOptions(options);
  69. }
  70. /**
  71. * Change a component's navigation options
  72. */
  73. public mergeOptions(componentId: string, options): void {
  74. this.commands.mergeOptions(componentId, options);
  75. }
  76. /**
  77. * Show a screen as a modal.
  78. */
  79. public showModal(layout): Promise<any> {
  80. return this.commands.showModal(layout);
  81. }
  82. /**
  83. * Dismiss a modal by componentId. The dismissed modal can be anywhere in the stack.
  84. */
  85. public dismissModal(componentId: string): Promise<any> {
  86. return this.commands.dismissModal(componentId);
  87. }
  88. /**
  89. * Dismiss all Modals
  90. */
  91. public dismissAllModals(): Promise<any> {
  92. return this.commands.dismissAllModals();
  93. }
  94. /**
  95. * Push a new layout into this screen's navigation stack.
  96. */
  97. public push(componentId: string, layout): Promise<any> {
  98. return this.commands.push(componentId, layout);
  99. }
  100. /**
  101. * Pop a component from the stack, regardless of it's position.
  102. */
  103. public pop(componentId: string, params?): Promise<any> {
  104. return this.commands.pop(componentId, params);
  105. }
  106. /**
  107. * Pop the stack to a given component
  108. */
  109. public popTo(componentId: string): Promise<any> {
  110. return this.commands.popTo(componentId);
  111. }
  112. /**
  113. * Pop the component's stack to root.
  114. */
  115. public popToRoot(componentId: string): Promise<any> {
  116. return this.commands.popToRoot(componentId);
  117. }
  118. /**
  119. * Sets new root component to stack.
  120. */
  121. public setStackRoot(componentId: string, layout): Promise<any> {
  122. return this.commands.setStackRoot(componentId, layout);
  123. }
  124. /**
  125. * Show overlay on top of the entire app
  126. */
  127. public showOverlay(layout): Promise<any> {
  128. return this.commands.showOverlay(layout);
  129. }
  130. /**
  131. * dismiss overlay by componentId
  132. */
  133. public dismissOverlay(componentId: string): Promise<any> {
  134. return this.commands.dismissOverlay(componentId);
  135. }
  136. /**
  137. * Resolves arguments passed on launch
  138. */
  139. public getLaunchArgs(): Promise<any> {
  140. return this.commands.getLaunchArgs();
  141. }
  142. /**
  143. * Obtain the events registry instance
  144. */
  145. public events(): EventsRegistry {
  146. return this.eventsRegistry;
  147. }
  148. /**
  149. * Constants coming from native
  150. */
  151. public async constants(): Promise<any> {
  152. return await Constants.get();
  153. }
  154. }