Expose the bridge instance to CxxModule via a weak_ptr

Summary:
In order for `CxxModule` modules to be able to do things like emit events, we need to expose the bridge instance.

To allow classes derived from `CxxModule` in other projects to include `<cxxreact/Instance.h> I also needed to convert all the header includes to non-relative  ones.

Reviewed By: javache

Differential Revision: D4564145

fbshipit-source-id: a5bc28dd9b40e2b141af9e867105c56083fbdcdc
This commit is contained in:
Sean Kinsey 2017-02-15 09:49:07 -08:00 коммит произвёл Facebook Github Bot
Родитель f9df89ac95
Коммит c38d56d9db
17 изменённых файлов: 66 добавлений и 53 удалений

Просмотреть файл

@ -2,14 +2,14 @@
#pragma once
#include "MessageQueueThread.h"
#include <atomic>
#include <functional>
#include <chrono>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <memory>
#include <cxxreact/MessageQueueThread.h>
namespace facebook {
namespace react {

Просмотреть файл

@ -13,6 +13,10 @@
using namespace std::placeholders;
namespace facebook { namespace react {
class Instance;
}}
namespace facebook { namespace xplat { namespace module {
/**
@ -157,6 +161,26 @@ public:
* @return a list of methods this module exports to JS.
*/
virtual auto getMethods() -> std::vector<Method> = 0;
/**
* Called during the construction of CxxNativeModule.
*/
void setInstance(std::weak_ptr<react::Instance> instance) {
instance_ = instance;
}
/**
* @return a weak_ptr to the current instance of the bridge.
* When used with CxxNativeModule, this gives Cxx modules access to functions
* such as `callJSFunction`, allowing them to communicate back to JS outside
* of the regular callbacks.
*/
std::weak_ptr<react::Instance> getInstance() {
return instance_;
}
private:
std::weak_ptr<react::Instance> instance_;
};
}}}

Просмотреть файл

@ -50,7 +50,9 @@ CxxNativeModule::CxxNativeModule(std::weak_ptr<Instance> instance,
std::unique_ptr<CxxModule> module)
: instance_(instance)
, module_(std::move(module))
, methods_(module_->getMethods()) {}
, methods_(module_->getMethods()) {
module_->setInstance(instance);
}
std::string CxxNativeModule::getName() {
return module_->getName();

Просмотреть файл

@ -2,9 +2,8 @@
#pragma once
#include "NativeModule.h"
#include <cxxreact/CxxModule.h>
#include <cxxreact/NativeModule.h>
namespace facebook {
namespace react {

Просмотреть файл

@ -9,12 +9,11 @@
#include <sys/mman.h>
#include <vector>
#include <cxxreact/JSModulesUnbundle.h>
#include <folly/Exception.h>
#include <folly/Optional.h>
#include <folly/dynamic.h>
#include "JSModulesUnbundle.h"
#define RN_EXPORT __attribute__((visibility("default")))
namespace facebook {

Просмотреть файл

@ -2,7 +2,7 @@
#pragma once
#include "Executor.h"
#include <cxxreact/Executor.h>
namespace facebook {
namespace react {

Просмотреть файл

@ -2,8 +2,8 @@
#pragma once
#include "ExecutorToken.h"
#include "Executor.h"
#include <cxxreact/Executor.h>
#include <cxxreact/ExecutorToken.h>
namespace facebook {
namespace react {

Просмотреть файл

@ -4,12 +4,11 @@
#include <memory>
#include <cxxreact/ModuleRegistry.h>
#include <cxxreact/NativeModule.h>
#include <cxxreact/NativeToJsBridge.h>
#include <folly/dynamic.h>
#include "NativeToJsBridge.h"
#include "ModuleRegistry.h"
#include "NativeModule.h"
namespace facebook {
namespace react {

Просмотреть файл

@ -6,17 +6,15 @@
#include <memory>
#include <unordered_map>
#include <folly/json.h>
#include <cxxreact/Executor.h>
#include <cxxreact/ExecutorToken.h>
#include <cxxreact/JSCNativeModules.h>
#include <folly/Optional.h>
#include <jschelpers/JavaScriptCore.h>
#include <folly/json.h>
#include <jschelpers/JSCHelpers.h>
#include <jschelpers/JavaScriptCore.h>
#include <jschelpers/Value.h>
#include "Executor.h"
#include "ExecutorToken.h"
#include "JSCNativeModules.h"
namespace facebook {
namespace react {

Просмотреть файл

@ -2,15 +2,13 @@
#pragma once
#include <folly/Optional.h>
#include <memory>
#include <string>
#include <cxxreact/ModuleRegistry.h>
#include <folly/Optional.h>
#include <jschelpers/Value.h>
#include "ModuleRegistry.h"
namespace facebook {
namespace react {

Просмотреть файл

@ -2,10 +2,9 @@
#pragma once
#include <cxxreact/Executor.h>
#include <jschelpers/Value.h>
#include "Executor.h"
namespace facebook {
namespace react {

Просмотреть файл

@ -5,8 +5,8 @@
#include <fstream>
#include <memory>
#include "Executor.h"
#include "JSBundleType.h"
#include <cxxreact/Executor.h>
#include <cxxreact/JSBundleType.h>
namespace facebook {
namespace react {

Просмотреть файл

@ -2,12 +2,12 @@
#pragma once
#include <folly/Conv.h>
#include <folly/dynamic.h>
#include <exception>
#include <string>
#include <folly/Conv.h>
#include <folly/dynamic.h>
// When building a cross-platform module for React Native, arguments passed
// from JS are represented as a folly::dynamic. This class provides helpers to
// extract arguments from the folly::dynamic to concrete types usable by
@ -103,4 +103,4 @@ inline std::string jsArgAsString(const folly::dynamic& args, size_t n) {
}}
#include "JsArgumentHelpers-inl.h"
#include <cxxreact/JsArgumentHelpers-inl.h>

Просмотреть файл

@ -5,11 +5,10 @@
#include <memory>
#include <vector>
#include <folly/dynamic.h>
#include <cxxreact/ExecutorToken.h>
#include <cxxreact/NativeModule.h>
#include <folly/Optional.h>
#include "ExecutorToken.h"
#include "NativeModule.h"
#include <folly/dynamic.h>
namespace facebook {
namespace react {

Просмотреть файл

@ -5,10 +5,9 @@
#include <string>
#include <vector>
#include <cxxreact/ExecutorToken.h>
#include <folly/dynamic.h>
#include "ExecutorToken.h"
namespace facebook {
namespace react {

Просмотреть файл

@ -7,18 +7,16 @@
#include <map>
#include <vector>
#include <cxxreact/Executor.h>
#include <cxxreact/ExecutorToken.h>
#include <cxxreact/JSCExecutor.h>
#include <cxxreact/JSModulesUnbundle.h>
#include <cxxreact/MessageQueueThread.h>
#include <cxxreact/MethodCall.h>
#include <cxxreact/NativeModule.h>
#include <folly/dynamic.h>
#include <jschelpers/Value.h>
#include "Executor.h"
#include "ExecutorToken.h"
#include "JSCExecutor.h"
#include "JSModulesUnbundle.h"
#include "MessageQueueThread.h"
#include "MethodCall.h"
#include "NativeModule.h"
namespace folly {
struct dynamic;

Просмотреть файл

@ -6,11 +6,10 @@
#include <memory>
#include <string>
#include <cxxreact/Executor.h>
#include <cxxreact/MessageQueueThread.h>
#include <jschelpers/JavaScriptCore.h>
#include "Executor.h"
#include "MessageQueueThread.h"
namespace facebook {
namespace react {