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