react-native-navigation的迁移库

Navigation.ts 7.4KB

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