Plugin and Module implementations
Summary: Last diff in the the RNW saga. This change creates the necessary bridgings between the module and the plugin. With this in place, JS plugins can be written and used. Also, we have a fully functional RNW sample app. Reviewed By: aigoncharov Differential Revision: D39087480 fbshipit-source-id: f4fde404716aa619a64553ffa556d060f49c0ac7
This commit is contained in:
committed by
Facebook GitHub Bot
parent
322a1ba6b1
commit
6a9d54f93a
@@ -11,8 +11,11 @@
|
||||
#include "NativeModules.h"
|
||||
|
||||
#include <FlipperReactDeviceInfo.h>
|
||||
#include <FlipperReactPlugin.h>
|
||||
#include <FlipperReactPluginManager.h>
|
||||
#include <FlipperReactScheduler.h>
|
||||
#include <FlipperReactSocket.h>
|
||||
#include <map>
|
||||
#include "../../../../xplat/Flipper/FlipperClient.h"
|
||||
#include "../../../../xplat/Flipper/FlipperInitConfig.h"
|
||||
#include "../../../../xplat/Flipper/FlipperScheduler.h"
|
||||
@@ -26,6 +29,7 @@ REACT_MODULE(FlipperModule, L"Flipper")
|
||||
struct FlipperModule {
|
||||
std::unique_ptr<facebook::flipper::Scheduler> sonarScheduler;
|
||||
std::unique_ptr<facebook::flipper::Scheduler> connectionScheduler;
|
||||
std::unique_ptr<facebook::flipper::FlipperReactPluginManager> pluginManager;
|
||||
|
||||
REACT_INIT(Initialize)
|
||||
void Initialize(ReactContext const& reactContext) noexcept {
|
||||
@@ -36,6 +40,9 @@ struct FlipperModule {
|
||||
connectionScheduler =
|
||||
std::make_unique<facebook::flipper::FlipperReactScheduler>();
|
||||
|
||||
pluginManager =
|
||||
std::make_unique<facebook::flipper::FlipperReactPluginManager>();
|
||||
|
||||
facebook::flipper::FlipperReactDeviceInfo deviceInfo;
|
||||
facebook::flipper::FlipperInitConfig config;
|
||||
config.deviceData.host = deviceInfo.getHost();
|
||||
@@ -60,29 +67,73 @@ struct FlipperModule {
|
||||
void registerPlugin(
|
||||
std::string pluginId,
|
||||
bool inBackground,
|
||||
std::function<void(std::string)>&& callback) noexcept {}
|
||||
std::function<void(std::string)>&& callback) noexcept {
|
||||
bool registered = pluginManager->registerPlugin(
|
||||
pluginId,
|
||||
inBackground,
|
||||
[this](
|
||||
const std::string& pluginId,
|
||||
facebook::flipper::FlipperReactPluginEvent e) {
|
||||
const std::map<std::string, std::string> args{{"plugin", pluginId}};
|
||||
switch (e) {
|
||||
case facebook::flipper::FlipperReactPluginEvent::CONNECTED:
|
||||
return pluginOnConnect(args);
|
||||
case facebook::flipper::FlipperReactPluginEvent::DISCONNECTED:
|
||||
return pluginOnDisconnect(args);
|
||||
}
|
||||
});
|
||||
if (registered) {
|
||||
callback("ok");
|
||||
} else {
|
||||
callback("noflipper");
|
||||
}
|
||||
}
|
||||
|
||||
REACT_METHOD(send)
|
||||
void
|
||||
send(std::string pluginId, std::string method, std::string data) noexcept {}
|
||||
send(std::string pluginId, std::string method, std::string data) noexcept {
|
||||
pluginManager->send(pluginId, method, data);
|
||||
}
|
||||
|
||||
REACT_METHOD(reportErrorWithMetadata)
|
||||
void reportErrorWithMetadata(
|
||||
std::string pluginId,
|
||||
std::string reason,
|
||||
std::string stacktrace) noexcept {}
|
||||
std::string stacktrace) noexcept {
|
||||
pluginManager->reportError(pluginId, reason, stacktrace);
|
||||
}
|
||||
|
||||
REACT_METHOD(reportError)
|
||||
void reportError(std::string pluginId, std::string error) noexcept {}
|
||||
void reportError(std::string pluginId, std::string error) noexcept {
|
||||
pluginManager->reportError(pluginId, error);
|
||||
}
|
||||
|
||||
REACT_METHOD(subscribe)
|
||||
void subscribe(std::string pluginId, std::string method) noexcept {}
|
||||
void subscribe(std::string pluginId, std::string method) noexcept {
|
||||
pluginManager->subscribe(
|
||||
pluginId, method, [this](std::map<std::string, std::string> args) {
|
||||
onReceive(args);
|
||||
});
|
||||
}
|
||||
|
||||
REACT_METHOD(respondSuccess)
|
||||
void respondSuccess(std::string responderId, std::string data) noexcept {}
|
||||
void respondSuccess(std::string responderId, std::string data) noexcept {
|
||||
pluginManager->respondSuccess(responderId, data);
|
||||
}
|
||||
|
||||
REACT_METHOD(respondError)
|
||||
void respondError(std::string responderId, std::string data) noexcept {}
|
||||
void respondError(std::string responderId, std::string data) noexcept {
|
||||
pluginManager->respondError(responderId, data);
|
||||
}
|
||||
|
||||
REACT_EVENT(pluginOnConnect, L"react-native-flipper-plugin-connect")
|
||||
std::function<void(std::map<std::string, std::string>)> pluginOnConnect;
|
||||
|
||||
REACT_EVENT(pluginOnDisconnect, L"react-native-flipper-plugin-disconnect")
|
||||
std::function<void(std::map<std::string, std::string>)> pluginOnDisconnect;
|
||||
|
||||
REACT_EVENT(onReceive, L"react-native-flipper-receive-event")
|
||||
std::function<void(std::map<std::string, std::string>)> onReceive;
|
||||
|
||||
private:
|
||||
ReactContext m_reactContext{nullptr};
|
||||
|
||||
Reference in New Issue
Block a user