Skip to content

Commit

Permalink
Add support for new ios bridge to FBReactBridgeJSExecutor
Browse files Browse the repository at this point in the history
Reviewed By: javache

Differential Revision: D3897535

fbshipit-source-id: 35bdaf885b03c0c95017a68b69f1f506e6943f2b
  • Loading branch information
mhorowitz authored and Facebook Github Bot committed Sep 28, 2016
1 parent 441d146 commit af5c8a8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
42 changes: 42 additions & 0 deletions React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,48 @@ - (void)enqueueCallback:(NSNumber *)cbID args:(NSArray *)args
} queue:RCTJSThread];
}

/**
* JS thread only
*/
- (JSValue *)callFunctionOnModule:(NSString *)module
method:(NSString *)method
arguments:(NSArray *)arguments
error:(NSError **)error
{
RCTJSCExecutor *jsExecutor = (RCTJSCExecutor *)_javaScriptExecutor;
if (![jsExecutor isKindOfClass:[RCTJSCExecutor class]]) {
RCTLogWarn(@"FBReactBridgeJSExecutor is only supported when running in JSC");
return nil;
}

__block JSValue *jsResult = nil;

RCTAssertJSThread();
RCT_PROFILE_BEGIN_EVENT(0, @"callFunctionOnModule", (@{ @"module": module, @"method": method }));
[jsExecutor callFunctionOnModule:module
method:method
arguments:arguments
jsValueCallback:^(JSValue *result, NSError *jsError) {
if (error) {
*error = jsError;
}

JSValue *length = result[@"length"];
RCTAssert([length isNumber] && [length toUInt32] == 2,
@"Return value of a callFunction must be an array of size 2");

jsResult = [result valueAtIndex:0];

NSArray *nativeModuleCalls = [[result valueAtIndex:1] toArray];
[self handleBuffer:nativeModuleCalls batchEnded:YES];
}];

RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"js_call");

return jsResult;
}


/**
* Private hack to support `setTimeout(fn, 0)`
*/
Expand Down
16 changes: 16 additions & 0 deletions React/Base/RCTBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "RCTFrameUpdate.h"
#import "RCTInvalidating.h"

@class JSValue;
@class RCTBridge;
@class RCTEventDispatcher;
@class RCTPerformanceLogger;
Expand Down Expand Up @@ -103,6 +104,21 @@ RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
- (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args;
- (void)enqueueJSCall:(NSString *)module method:(NSString *)method args:(NSArray *)args completion:(dispatch_block_t)completion;

/**
* This method is used to call functions in the JavaScript application context
* synchronously. This is intended for use by applications which do their own
* thread management and are careful to manage multi-threaded access to the JSVM.
* See also -[RCTBridgeDelgate shouldBridgeLoadJavaScriptSynchronously], which
* may be needed to ensure that any requires JS code is loaded before this method
* is called. If the underlying executor is not JSC, this will return nil. Safe
* to call from any thread.
*
* @experimental
*/
- (JSValue *)callFunctionOnModule:(NSString *)module
method:(NSString *)method
arguments:(NSArray *)arguments
error:(NSError **)error;

/**
* Retrieve a bridge module instance by name or class. Note that modules are
Expand Down
9 changes: 9 additions & 0 deletions React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,13 @@ - (void)enqueueCallback:(NSNumber *)cbID args:(NSArray *)args
[self.batchedBridge enqueueCallback:cbID args:args];
}

- (JSValue *)callFunctionOnModule:(NSString *)module
method:(NSString *)method
arguments:(NSArray *)arguments
error:(NSError **)error
{
return [self.batchedBridge callFunctionOnModule:module method:method arguments:arguments error:error];
}


@end

0 comments on commit af5c8a8

Please sign in to comment.