Skip to content

Commit

Permalink
iOS: Make each module implement getTurboModuleWithJsInvoker: instead …
Browse files Browse the repository at this point in the history
…of having centralized provider

Summary:
For better modularity, each module conforming to RCTTurboModule should provide a getter for the specific TurboModule instance for itself. This is a bit more extra work for devs, but simplify tooling and allow better modularity vs having a central function that provides the correct instance based on name.

Note: Android may or may not follow this new pattern -- TBD.

Reviewed By: RSNara

Differential Revision: D13882073

fbshipit-source-id: 6d5f82af67278c39c43c4f7970995690d4a82a98
  • Loading branch information
fkgozali authored and facebook-github-bot committed Jan 31, 2019
1 parent bbcb97a commit 8a50bc3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
5 changes: 2 additions & 3 deletions React/Base/RCTBridgeModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ RCT_EXTERN void RCTRegisterModule(Class); \
* Experimental.
* A protocol to declare that a class supports TurboModule.
* This may be removed in the future.
* See RCTTurboModule.h for actual signature.
*/
@protocol RCTTurboModule <NSObject>

@end
@protocol RCTTurboModule;
17 changes: 8 additions & 9 deletions ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,18 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule {
} // namespace react
} // namespace facebook

// TODO: Consolidate this extension with the one in RCTSurfacePresenter.
@interface RCTBridge ()
@protocol RCTTurboModule <NSObject>

- (std::shared_ptr<facebook::react::MessageQueueThread>)jsMessageThread;
@optional

// This should be required, after migration is done.
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker;

@end

/**
* A backward-compatible protocol to be adopted by an existing RCTCxxModule-based class
* so that it can support the TurboModule system.
*/
@protocol RCTTurboCxxModule <RCTTurboModule>
// TODO: Consolidate this extension with the one in RCTSurfacePresenter.
@interface RCTBridge ()

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker;
- (std::shared_ptr<facebook::react::MessageQueueThread>)jsMessageThread;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,18 @@ - (instancetype)initWithRuntime:(jsi::Runtime *)runtime
}
}

if ([module respondsToSelector:@selector(getTurboModuleWithJsInvoker:)]) {
return [module getTurboModuleWithJsInvoker:strongSelf->_jsInvoker];
}

// RCTCxxModule compatibility layer.
if ([moduleClass isSubclassOfClass:RCTCxxModule.class]) {
if ([module respondsToSelector:@selector(getTurboModuleWithJsInvoker:)]) {
return [((id<RCTTurboCxxModule>)module) getTurboModuleWithJsInvoker:strongSelf->_jsInvoker];
}

// Use TurboCxxModule compat class to wrap the CxxModule instance.
// This is only for migration convenience, despite less performant.
return std::make_shared<react::TurboCxxModule>([((RCTCxxModule *)module) createModule], strongSelf->_jsInvoker);
}

// This may be needed for migration purpose in case the module class doesn't provide the static getter.
return [strongSelf->_delegate getTurboModule:name instance:module jsInvoker:strongSelf->_jsInvoker];
};

Expand Down

0 comments on commit 8a50bc3

Please sign in to comment.