RCTBridgeModule.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 <Foundation/Foundation.h>
  8. #import <React/RCTDefines.h>
  9. @class RCTBridge;
  10. @protocol RCTBridgeMethod;
  11. /**
  12. * The type of a block that is capable of sending a response to a bridged
  13. * operation. Use this for returning callback methods to JS.
  14. */
  15. typedef void (^RCTResponseSenderBlock)(NSArray *response);
  16. /**
  17. * The type of a block that is capable of sending an error response to a
  18. * bridged operation. Use this for returning error information to JS.
  19. */
  20. typedef void (^RCTResponseErrorBlock)(NSError *error);
  21. /**
  22. * Block that bridge modules use to resolve the JS promise waiting for a result.
  23. * Nil results are supported and are converted to JS's undefined value.
  24. */
  25. typedef void (^RCTPromiseResolveBlock)(id result);
  26. /**
  27. * Block that bridge modules use to reject the JS promise waiting for a result.
  28. * The error may be nil but it is preferable to pass an NSError object for more
  29. * precise error messages.
  30. */
  31. typedef void (^RCTPromiseRejectBlock)(NSString *code, NSString *message, NSError *error);
  32. /**
  33. * This constant can be returned from +methodQueue to force module
  34. * methods to be called on the JavaScript thread. This can have serious
  35. * implications for performance, so only use this if you're sure it's what
  36. * you need.
  37. *
  38. * NOTE: RCTJSThread is not a real libdispatch queue
  39. */
  40. RCT_EXTERN dispatch_queue_t RCTJSThread;
  41. RCT_EXTERN_C_BEGIN
  42. typedef struct RCTMethodInfo {
  43. const char *const jsName;
  44. const char *const objcName;
  45. const BOOL isSync;
  46. } RCTMethodInfo;
  47. RCT_EXTERN_C_END
  48. /**
  49. * Provides the interface needed to register a bridge module.
  50. */
  51. @protocol RCTBridgeModule <NSObject>
  52. /**
  53. * Place this macro in your class implementation to automatically register
  54. * your module with the bridge when it loads. The optional js_name argument
  55. * will be used as the JS module name. If omitted, the JS module name will
  56. * match the Objective-C class name.
  57. */
  58. #define RCT_EXPORT_MODULE(js_name) \
  59. RCT_EXTERN void RCTRegisterModule(Class); \
  60. + (NSString *)moduleName { return @#js_name; } \
  61. + (void)load { RCTRegisterModule(self); }
  62. /**
  63. * Same as RCT_EXPORT_MODULE, but uses __attribute__((constructor)) for module
  64. * registration. Useful for registering swift classes that forbids use of load
  65. * Used in RCT_EXTERN_REMAP_MODULE
  66. */
  67. #define RCT_EXPORT_MODULE_NO_LOAD(js_name, objc_name) \
  68. RCT_EXTERN void RCTRegisterModule(Class); \
  69. + (NSString *)moduleName { return @#js_name; } \
  70. __attribute__((constructor)) static void \
  71. RCT_CONCAT(initialize_, objc_name)() { RCTRegisterModule([objc_name class]); }
  72. /**
  73. * To improve startup performance users may want to generate their module lists
  74. * at build time and hook the delegate to merge with the runtime list. This
  75. * macro takes the place of the above for those cases by omitting the +load
  76. * generation.
  77. *
  78. */
  79. #define RCT_EXPORT_PRE_REGISTERED_MODULE(js_name) \
  80. + (NSString *)moduleName { return @#js_name; }
  81. // Implemented by RCT_EXPORT_MODULE
  82. + (NSString *)moduleName;
  83. @optional
  84. /**
  85. * A reference to the RCTBridge. Useful for modules that require access
  86. * to bridge features, such as sending events or making JS calls. This
  87. * will be set automatically by the bridge when it initializes the module.
  88. * To implement this in your module, just add `@synthesize bridge = _bridge;`
  89. * If using Swift, add `@objc var bridge: RCTBridge!` to your module.
  90. */
  91. @property (nonatomic, weak, readonly) RCTBridge *bridge;
  92. /**
  93. * The queue that will be used to call all exported methods. If omitted, this
  94. * will call on a default background queue, which is avoids blocking the main
  95. * thread.
  96. *
  97. * If the methods in your module need to interact with UIKit methods, they will
  98. * probably need to call those on the main thread, as most of UIKit is main-
  99. * thread-only. You can tell React Native to call your module methods on the
  100. * main thread by returning a reference to the main queue, like this:
  101. *
  102. * - (dispatch_queue_t)methodQueue
  103. * {
  104. * return dispatch_get_main_queue();
  105. * }
  106. *
  107. * If you don't want to specify the queue yourself, but you need to use it
  108. * inside your class (e.g. if you have internal methods that need to dispatch
  109. * onto that queue), you can just add `@synthesize methodQueue = _methodQueue;`
  110. * and the bridge will populate the methodQueue property for you automatically
  111. * when it initializes the module.
  112. */
  113. @property (nonatomic, strong, readonly) dispatch_queue_t methodQueue;
  114. /**
  115. * Wrap the parameter line of your method implementation with this macro to
  116. * expose it to JS. By default the exposed method will match the first part of
  117. * the Objective-C method selector name (up to the first colon). Use
  118. * RCT_REMAP_METHOD to specify the JS name of the method.
  119. *
  120. * For example, in ModuleName.m:
  121. *
  122. * - (void)doSomething:(NSString *)aString withA:(NSInteger)a andB:(NSInteger)b
  123. * { ... }
  124. *
  125. * becomes
  126. *
  127. * RCT_EXPORT_METHOD(doSomething:(NSString *)aString
  128. * withA:(NSInteger)a
  129. * andB:(NSInteger)b)
  130. * { ... }
  131. *
  132. * and is exposed to JavaScript as `NativeModules.ModuleName.doSomething`.
  133. *
  134. * ## Promises
  135. *
  136. * Bridge modules can also define methods that are exported to JavaScript as
  137. * methods that return a Promise, and are compatible with JS async functions.
  138. *
  139. * Declare the last two parameters of your native method to be a resolver block
  140. * and a rejecter block. The resolver block must precede the rejecter block.
  141. *
  142. * For example:
  143. *
  144. * RCT_EXPORT_METHOD(doSomethingAsync:(NSString *)aString
  145. * resolver:(RCTPromiseResolveBlock)resolve
  146. * rejecter:(RCTPromiseRejectBlock)reject
  147. * { ... }
  148. *
  149. * Calling `NativeModules.ModuleName.doSomethingAsync(aString)` from
  150. * JavaScript will return a promise that is resolved or rejected when your
  151. * native method implementation calls the respective block.
  152. *
  153. */
  154. #define RCT_EXPORT_METHOD(method) \
  155. RCT_REMAP_METHOD(, method)
  156. /**
  157. * Same as RCT_EXPORT_METHOD but the method is called from JS
  158. * synchronously **on the JS thread**, possibly returning a result.
  159. *
  160. * WARNING: in the vast majority of cases, you should use RCT_EXPORT_METHOD which
  161. * allows your native module methods to be called asynchronously: calling
  162. * methods synchronously can have strong performance penalties and introduce
  163. * threading-related bugs to your native modules.
  164. *
  165. * The return type must be of object type (id) and should be serializable
  166. * to JSON. This means that the hook can only return nil or JSON values
  167. * (e.g. NSNumber, NSString, NSArray, NSDictionary).
  168. *
  169. * Calling these methods when running under the websocket executor
  170. * is currently not supported.
  171. */
  172. #define RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(method) \
  173. RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(id, method)
  174. #define RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(returnType, method) \
  175. RCT_REMAP_BLOCKING_SYNCHRONOUS_METHOD(, returnType, method)
  176. /**
  177. * Similar to RCT_EXPORT_METHOD but lets you set the JS name of the exported
  178. * method. Example usage:
  179. *
  180. * RCT_REMAP_METHOD(executeQueryWithParameters,
  181. * executeQuery:(NSString *)query parameters:(NSDictionary *)parameters)
  182. * { ... }
  183. */
  184. #define RCT_REMAP_METHOD(js_name, method) \
  185. _RCT_EXTERN_REMAP_METHOD(js_name, method, NO) \
  186. - (void)method RCT_DYNAMIC;
  187. /**
  188. * Similar to RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD but lets you set
  189. * the JS name of the exported method. Example usage:
  190. *
  191. * RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(executeQueryWithParameters,
  192. * executeQuery:(NSString *)query parameters:(NSDictionary *)parameters)
  193. * { ... }
  194. */
  195. #define RCT_REMAP_BLOCKING_SYNCHRONOUS_METHOD(js_name, returnType, method) \
  196. _RCT_EXTERN_REMAP_METHOD(js_name, method, YES) \
  197. - (returnType)method RCT_DYNAMIC;
  198. /**
  199. * Use this macro in a private Objective-C implementation file to automatically
  200. * register an external module with the bridge when it loads. This allows you to
  201. * register Swift or private Objective-C classes with the bridge.
  202. *
  203. * For example if one wanted to export a Swift class to the bridge:
  204. *
  205. * MyModule.swift:
  206. *
  207. * @objc(MyModule) class MyModule: NSObject {
  208. *
  209. * @objc func doSomething(string: String! withFoo a: Int, bar b: Int) { ... }
  210. *
  211. * }
  212. *
  213. * MyModuleExport.m:
  214. *
  215. * #import <React/RCTBridgeModule.h>
  216. *
  217. * @interface RCT_EXTERN_MODULE(MyModule, NSObject)
  218. *
  219. * RCT_EXTERN_METHOD(doSomething:(NSString *)string withFoo:(NSInteger)a bar:(NSInteger)b)
  220. *
  221. * @end
  222. *
  223. * This will now expose MyModule and the method to JavaScript via
  224. * `NativeModules.MyModule.doSomething`
  225. */
  226. #define RCT_EXTERN_MODULE(objc_name, objc_supername) \
  227. RCT_EXTERN_REMAP_MODULE(, objc_name, objc_supername)
  228. /**
  229. * Like RCT_EXTERN_MODULE, but allows setting a custom JavaScript name.
  230. */
  231. #define RCT_EXTERN_REMAP_MODULE(js_name, objc_name, objc_supername) \
  232. objc_name : objc_supername \
  233. @end \
  234. @interface objc_name (RCTExternModule) <RCTBridgeModule> \
  235. @end \
  236. @implementation objc_name (RCTExternModule) \
  237. RCT_EXPORT_MODULE_NO_LOAD(js_name, objc_name)
  238. /**
  239. * Use this macro in accordance with RCT_EXTERN_MODULE to export methods
  240. * of an external module.
  241. */
  242. #define RCT_EXTERN_METHOD(method) \
  243. _RCT_EXTERN_REMAP_METHOD(, method, NO)
  244. /**
  245. * Use this macro in accordance with RCT_EXTERN_MODULE to export methods
  246. * of an external module that should be invoked synchronously.
  247. */
  248. #define RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(method) \
  249. _RCT_EXTERN_REMAP_METHOD(, method, YES)
  250. /**
  251. * Like RCT_EXTERN_REMAP_METHOD, but allows setting a custom JavaScript name
  252. * and also whether this method is synchronous.
  253. */
  254. #define _RCT_EXTERN_REMAP_METHOD(js_name, method, is_blocking_synchronous_method) \
  255. + (const RCTMethodInfo *)RCT_CONCAT(__rct_export__, RCT_CONCAT(js_name, RCT_CONCAT(__LINE__, __COUNTER__))) { \
  256. static RCTMethodInfo config = {#js_name, #method, is_blocking_synchronous_method}; \
  257. return &config; \
  258. }
  259. /**
  260. * Most modules can be used from any thread. All of the modules exported non-sync method will be called on its
  261. * methodQueue, and the module will be constructed lazily when its first invoked. Some modules have main need to access
  262. * information that's main queue only (e.g. most UIKit classes). Since we don't want to dispatch synchronously to the
  263. * main thread to this safely, we construct these moduels and export their constants ahead-of-time.
  264. *
  265. * Note that when set to false, the module constructor will be called from any thread.
  266. *
  267. * This requirement is currently inferred by checking if the module has a custom initializer or if there's exported
  268. * constants. In the future, we'll stop automatically inferring this and instead only rely on this method.
  269. */
  270. + (BOOL)requiresMainQueueSetup;
  271. /**
  272. * Injects methods into JS. Entries in this array are used in addition to any
  273. * methods defined using the macros above. This method is called only once,
  274. * before registration.
  275. */
  276. - (NSArray<id<RCTBridgeMethod>> *)methodsToExport;
  277. /**
  278. * Injects constants into JS. These constants are made accessible via NativeModules.ModuleName.X. It is only called once
  279. * for the lifetime of the bridge, so it is not suitable for returning dynamic values, but may be used for long-lived
  280. * values such as session keys, that are regenerated only as part of a reload of the entire React application.
  281. *
  282. * If you implement this method and do not implement `requiresMainQueueSetup`, you will trigger deprecated logic
  283. * that eagerly initializes your module on bridge startup. In the future, this behaviour will be changed to default
  284. * to initializing lazily, and even modules with constants will be initialized lazily.
  285. */
  286. - (NSDictionary *)constantsToExport;
  287. /**
  288. * Notifies the module that a batch of JS method invocations has just completed.
  289. */
  290. - (void)batchDidComplete;
  291. /**
  292. * Notifies the module that the active batch of JS method invocations has been
  293. * partially flushed.
  294. *
  295. * This occurs before -batchDidComplete, and more frequently.
  296. */
  297. - (void)partialBatchDidFlush;
  298. @end
  299. /**
  300. * A protocol that allows TurboModules to do lookup on other TurboModules.
  301. * Calling these methods may cause a module to be synchronously instantiated.
  302. */
  303. @protocol RCTTurboModuleLookupDelegate <NSObject>
  304. - (id)moduleForName:(const char *)moduleName;
  305. /**
  306. * Rationale:
  307. * When TurboModules lookup other modules by name, we first check the TurboModule
  308. * registry to see if a TurboModule exists with the respective name. In this case,
  309. * we don't want a RedBox to be raised if the TurboModule isn't found.
  310. *
  311. * This method is deprecated and will be deleted after the migration from
  312. * TurboModules to TurboModules is complete.
  313. */
  314. - (id)moduleForName:(const char *)moduleName warnOnLookupFailure:(BOOL)warnOnLookupFailure;
  315. - (BOOL)moduleIsInitialized:(const char *)moduleName;
  316. @end
  317. /**
  318. * Experimental.
  319. * A protocol to declare that a class supports TurboModule.
  320. * This may be removed in the future.
  321. * See RCTTurboModule.h for actual signature.
  322. */
  323. @protocol RCTTurboModule;