RCTBridge.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <UIKit/UIKit.h>
  8. #import <React/RCTBridgeDelegate.h>
  9. #import <React/RCTBridgeModule.h>
  10. #import <React/RCTDefines.h>
  11. #import <React/RCTFrameUpdate.h>
  12. #import <React/RCTInvalidating.h>
  13. @class JSValue;
  14. @class RCTBridge;
  15. @class RCTEventDispatcher;
  16. @class RCTPerformanceLogger;
  17. /**
  18. * This notification fires when the bridge initializes.
  19. */
  20. RCT_EXTERN NSString *const RCTJavaScriptWillStartLoadingNotification;
  21. /**
  22. * This notification fires when the bridge starts executing the JS bundle.
  23. */
  24. RCT_EXTERN NSString *const RCTJavaScriptWillStartExecutingNotification;
  25. /**
  26. * This notification fires when the bridge has finished loading the JS bundle.
  27. */
  28. RCT_EXTERN NSString *const RCTJavaScriptDidLoadNotification;
  29. /**
  30. * This notification fires when the bridge failed to load the JS bundle. The
  31. * `error` key can be used to determine the error that occurred.
  32. */
  33. RCT_EXTERN NSString *const RCTJavaScriptDidFailToLoadNotification;
  34. /**
  35. * This notification fires each time a native module is instantiated. The
  36. * `module` key will contain a reference to the newly-created module instance.
  37. * Note that this notification may be fired before the module is available via
  38. * the `[bridge moduleForClass:]` method.
  39. */
  40. RCT_EXTERN NSString *const RCTDidInitializeModuleNotification;
  41. /**
  42. * This notification fires each time a module is setup after it is initialized. The
  43. * `RCTDidSetupModuleNotificationModuleNameKey` key will contain a reference to the module name and
  44. * `RCTDidSetupModuleNotificationSetupTimeKey` will contain the setup time in ms.
  45. */
  46. RCT_EXTERN NSString *const RCTDidSetupModuleNotification;
  47. /**
  48. * Key for the module name (NSString) in the
  49. * RCTDidSetupModuleNotification userInfo dictionary.
  50. */
  51. RCT_EXTERN NSString *const RCTDidSetupModuleNotificationModuleNameKey;
  52. /**
  53. * Key for the setup time (NSNumber) in the
  54. * RCTDidSetupModuleNotification userInfo dictionary.
  55. */
  56. RCT_EXTERN NSString *const RCTDidSetupModuleNotificationSetupTimeKey;
  57. /**
  58. * DEPRECATED - Use RCTReloadCommand instead. This notification fires just before the bridge starts
  59. * processing a request to reload.
  60. */
  61. RCT_EXTERN NSString *const RCTBridgeWillReloadNotification;
  62. /**
  63. * This notification fires whenever a fast refresh happens.
  64. */
  65. RCT_EXTERN NSString *const RCTBridgeFastRefreshNotification;
  66. /**
  67. * This notification fires just before the bridge begins downloading a script
  68. * from the packager.
  69. */
  70. RCT_EXTERN NSString *const RCTBridgeWillDownloadScriptNotification;
  71. /**
  72. * This notification fires just after the bridge finishes downloading a script
  73. * from the packager.
  74. */
  75. RCT_EXTERN NSString *const RCTBridgeDidDownloadScriptNotification;
  76. /**
  77. * This notification fires right after the bridge is about to invalidate NativeModule
  78. * instances during teardown. Handle this notification to perform additional invalidation.
  79. */
  80. RCT_EXTERN NSString *const RCTBridgeWillInvalidateModulesNotification;
  81. /**
  82. * This notification fires right after the bridge finishes invalidating NativeModule
  83. * instances during teardown. Handle this notification to perform additional invalidation.
  84. */
  85. RCT_EXTERN NSString *const RCTBridgeDidInvalidateModulesNotification;
  86. /**
  87. * This notification fires right before the bridge starting invalidation process.
  88. * Handle this notification to perform additional invalidation.
  89. * The notification can be issued on any thread.
  90. */
  91. RCT_EXTERN NSString *const RCTBridgeWillBeInvalidatedNotification;
  92. /**
  93. * Key for the RCTSource object in the RCTBridgeDidDownloadScriptNotification
  94. * userInfo dictionary.
  95. */
  96. RCT_EXTERN NSString *const RCTBridgeDidDownloadScriptNotificationSourceKey;
  97. /**
  98. * Key for the reload reason in the RCTBridgeWillReloadNotification userInfo dictionary.
  99. */
  100. RCT_EXTERN NSString *const RCTBridgeDidDownloadScriptNotificationReasonKey;
  101. /**
  102. * Key for the bridge description (NSString_ in the
  103. * RCTBridgeDidDownloadScriptNotification userInfo dictionary.
  104. */
  105. RCT_EXTERN NSString *const RCTBridgeDidDownloadScriptNotificationBridgeDescriptionKey;
  106. /**
  107. * This block can be used to instantiate modules that require additional
  108. * init parameters, or additional configuration prior to being used.
  109. * The bridge will call this block to instantiate the modules, and will
  110. * be responsible for invalidating/releasing them when the bridge is destroyed.
  111. * For this reason, the block should always return new module instances, and
  112. * module instances should not be shared between bridges.
  113. */
  114. typedef NSArray<id<RCTBridgeModule>> * (^RCTBridgeModuleListProvider)(void);
  115. /**
  116. * This function returns the module name for a given class.
  117. */
  118. RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
  119. /**
  120. * Experimental.
  121. * Check/set if JSI-bound NativeModule is enabled. By default it's off.
  122. */
  123. RCT_EXTERN BOOL RCTTurboModuleEnabled(void);
  124. RCT_EXTERN void RCTEnableTurboModule(BOOL enabled);
  125. /**
  126. * Async batched bridge used to communicate with the JavaScript application.
  127. */
  128. @interface RCTBridge : NSObject <RCTInvalidating>
  129. /**
  130. * Creates a new bridge with a custom RCTBridgeDelegate.
  131. *
  132. * All the interaction with the JavaScript context should be done using the bridge
  133. * instance of the RCTBridgeModules. Modules will be automatically instantiated
  134. * using the default contructor, but you can optionally pass in an array of
  135. * pre-initialized module instances if they require additional init parameters
  136. * or configuration.
  137. */
  138. - (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)delegate launchOptions:(NSDictionary *)launchOptions;
  139. /**
  140. * DEPRECATED: Use initWithDelegate:launchOptions: instead
  141. *
  142. * The designated initializer. This creates a new bridge on top of the specified
  143. * executor. The bridge should then be used for all subsequent communication
  144. * with the JavaScript code running in the executor. Modules will be automatically
  145. * instantiated using the default contructor, but you can optionally pass in an
  146. * array of pre-initialized module instances if they require additional init
  147. * parameters or configuration.
  148. */
  149. - (instancetype)initWithBundleURL:(NSURL *)bundleURL
  150. moduleProvider:(RCTBridgeModuleListProvider)block
  151. launchOptions:(NSDictionary *)launchOptions;
  152. /**
  153. * This method is used to call functions in the JavaScript application context.
  154. * It is primarily intended for use by modules that require two-way communication
  155. * with the JavaScript code. Safe to call from any thread.
  156. */
  157. - (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args;
  158. - (void)enqueueJSCall:(NSString *)module
  159. method:(NSString *)method
  160. args:(NSArray *)args
  161. completion:(dispatch_block_t)completion;
  162. /**
  163. * This method registers the file path of an additional JS segment by its ID.
  164. *
  165. * @experimental
  166. */
  167. - (void)registerSegmentWithId:(NSUInteger)segmentId path:(NSString *)path;
  168. /**
  169. * Retrieve a bridge module instance by name or class. Note that modules are
  170. * lazily instantiated, so calling these methods for the first time with a given
  171. * module name/class may cause the class to be synchronously instantiated,
  172. * potentially blocking both the calling thread and main thread for a short time.
  173. *
  174. * Note: This method does NOT lazily load the particular module if it's not yet loaded.
  175. */
  176. - (id)moduleForName:(NSString *)moduleName;
  177. - (id)moduleForName:(NSString *)moduleName lazilyLoadIfNecessary:(BOOL)lazilyLoad;
  178. // Note: This method lazily load the module as necessary.
  179. - (id)moduleForClass:(Class)moduleClass;
  180. /**
  181. * When a NativeModule performs a lookup for a TurboModule, we need to query
  182. * the lookupDelegate.
  183. */
  184. - (void)setRCTTurboModuleLookupDelegate:(id<RCTTurboModuleLookupDelegate>)turboModuleLookupDelegate;
  185. /**
  186. * Convenience method for retrieving all modules conforming to a given protocol.
  187. * Modules will be synchronously instantiated if they haven't already been,
  188. * potentially blocking both the calling thread and main thread for a short time.
  189. */
  190. - (NSArray *)modulesConformingToProtocol:(Protocol *)protocol;
  191. /**
  192. * Test if a module has been initialized. Use this prior to calling
  193. * `moduleForClass:` or `moduleForName:` if you do not want to cause the module
  194. * to be instantiated if it hasn't been already.
  195. */
  196. - (BOOL)moduleIsInitialized:(Class)moduleClass;
  197. /**
  198. * All registered bridge module classes.
  199. */
  200. @property (nonatomic, copy, readonly) NSArray<Class> *moduleClasses;
  201. /**
  202. * URL of the script that was loaded into the bridge.
  203. */
  204. @property (nonatomic, strong, readonly) NSURL *bundleURL;
  205. /**
  206. * The class of the executor currently being used. Changes to this value will
  207. * take effect after the bridge is reloaded.
  208. */
  209. @property (nonatomic, strong) Class executorClass;
  210. /**
  211. * The delegate provided during the bridge initialization
  212. */
  213. @property (nonatomic, weak, readonly) id<RCTBridgeDelegate> delegate;
  214. /**
  215. * The launch options that were used to initialize the bridge.
  216. */
  217. @property (nonatomic, copy, readonly) NSDictionary *launchOptions;
  218. /**
  219. * Use this to check if the bridge is currently loading.
  220. */
  221. @property (nonatomic, readonly, getter=isLoading) BOOL loading;
  222. /**
  223. * Use this to check if the bridge has been invalidated.
  224. */
  225. @property (nonatomic, readonly, getter=isValid) BOOL valid;
  226. /**
  227. * Link to the Performance Logger that logs React Native perf events.
  228. */
  229. @property (nonatomic, readonly, strong) RCTPerformanceLogger *performanceLogger;
  230. /**
  231. * Reload the bundle and reset executor & modules. Safe to call from any thread.
  232. */
  233. - (void)reload __deprecated_msg("Use RCTReloadCommand instead");
  234. /**
  235. * Reload the bundle and reset executor & modules. Safe to call from any thread.
  236. */
  237. - (void)reloadWithReason:(NSString *)reason __deprecated_msg("Use RCTReloadCommand instead");
  238. /**
  239. * Handle notifications for a fast refresh. Safe to call from any thread.
  240. */
  241. - (void)onFastRefresh;
  242. /**
  243. * Inform the bridge, and anything subscribing to it, that it should reload.
  244. */
  245. - (void)requestReload __deprecated_msg("Use RCTReloadCommand instead");
  246. /**
  247. * Says whether bridge has started receiving calls from javascript.
  248. */
  249. - (BOOL)isBatchActive;
  250. @end