RCTJavaScriptExecutor.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 <objc/runtime.h>
  8. #import <React/RCTBridgeModule.h>
  9. #import <React/RCTInvalidating.h>
  10. typedef void (^RCTJavaScriptCompleteBlock)(NSError *error);
  11. typedef void (^RCTJavaScriptCallback)(id result, NSError *error);
  12. /**
  13. * Abstracts away a JavaScript execution context - we may be running code in a
  14. * web view (for debugging purposes), or may be running code in a `JSContext`.
  15. */
  16. @protocol RCTJavaScriptExecutor <RCTInvalidating, RCTBridgeModule>
  17. /**
  18. * Used to set up the executor after the bridge has been fully initialized.
  19. * Do any expensive setup in this method instead of `-init`.
  20. */
  21. - (void)setUp;
  22. /**
  23. * Whether the executor has been invalidated
  24. */
  25. @property (nonatomic, readonly, getter=isValid) BOOL valid;
  26. /**
  27. * Executes BatchedBridge.flushedQueue on JS thread and calls the given callback
  28. * with JSValue, containing the next queue, and JSContext.
  29. */
  30. - (void)flushedQueue:(RCTJavaScriptCallback)onComplete;
  31. /**
  32. * Executes BatchedBridge.callFunctionReturnFlushedQueue with the module name,
  33. * method name and optional additional arguments on the JS thread and calls the
  34. * given callback with JSValue, containing the next queue, and JSContext.
  35. */
  36. - (void)callFunctionOnModule:(NSString *)module
  37. method:(NSString *)method
  38. arguments:(NSArray *)args
  39. callback:(RCTJavaScriptCallback)onComplete;
  40. /**
  41. * Executes BatchedBridge.invokeCallbackAndReturnFlushedQueue with the cbID,
  42. * and optional additional arguments on the JS thread and calls the
  43. * given callback with JSValue, containing the next queue, and JSContext.
  44. */
  45. - (void)invokeCallbackID:(NSNumber *)cbID arguments:(NSArray *)args callback:(RCTJavaScriptCallback)onComplete;
  46. /**
  47. * Runs an application script, and notifies of the script load being complete via `onComplete`.
  48. */
  49. - (void)executeApplicationScript:(NSData *)script
  50. sourceURL:(NSURL *)sourceURL
  51. onComplete:(RCTJavaScriptCompleteBlock)onComplete;
  52. - (void)injectJSONText:(NSString *)script
  53. asGlobalObjectNamed:(NSString *)objectName
  54. callback:(RCTJavaScriptCompleteBlock)onComplete;
  55. /**
  56. * Enqueue a block to run in the executors JS thread. Fallback to `dispatch_async`
  57. * on the main queue if the executor doesn't own a thread.
  58. */
  59. - (void)executeBlockOnJavaScriptQueue:(dispatch_block_t)block;
  60. /**
  61. * Special case for Timers + ContextExecutor - instead of the default
  62. * if jsthread then call else dispatch call on jsthread
  63. * ensure the call is made async on the jsthread
  64. */
  65. - (void)executeAsyncBlockOnJavaScriptQueue:(dispatch_block_t)block;
  66. @end