cxxreact/RAMBundleRegistry: nit fixes

Reviewed By: mhorowitz

Differential Revision: D7587411

fbshipit-source-id: 516753247af585914381308248de9652f18a6cf5
This commit is contained in:
Jean Lauliac 2018-04-17 11:54:49 -07:00 committed by Facebook Github Bot
parent c9094e952b
commit 281ed9f4ce
2 changed files with 41 additions and 23 deletions

View File

@ -12,33 +12,46 @@ namespace react {
constexpr uint32_t RAMBundleRegistry::MAIN_BUNDLE_ID; constexpr uint32_t RAMBundleRegistry::MAIN_BUNDLE_ID;
std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::singleBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle) { std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::singleBundleRegistry(
RAMBundleRegistry *registry = new RAMBundleRegistry(std::move(mainBundle)); std::unique_ptr<JSModulesUnbundle> mainBundle) {
return std::unique_ptr<RAMBundleRegistry>(registry); return folly::make_unique<RAMBundleRegistry>(std::move(mainBundle));
} }
std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::multipleBundlesRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory) { std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::multipleBundlesRegistry(
RAMBundleRegistry *registry = new RAMBundleRegistry(std::move(mainBundle), std::move(factory)); std::unique_ptr<JSModulesUnbundle> mainBundle,
return std::unique_ptr<RAMBundleRegistry>(registry); std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory) {
return folly::make_unique<RAMBundleRegistry>(
std::move(mainBundle), std::move(factory));
} }
RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory): m_factory(factory) { RAMBundleRegistry::RAMBundleRegistry(
std::unique_ptr<JSModulesUnbundle> mainBundle,
std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory):
m_factory(std::move(factory)) {
m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle)); m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle));
} }
void RAMBundleRegistry::registerBundle(uint32_t bundleId, std::string bundlePath) { void RAMBundleRegistry::registerBundle(
m_bundlePaths.emplace(bundleId, bundlePath); uint32_t bundleId, std::string bundlePath) {
m_bundlePaths.emplace(bundleId, std::move(bundlePath));
} }
JSModulesUnbundle::Module RAMBundleRegistry::getModule(uint32_t bundleId, uint32_t moduleId) { JSModulesUnbundle::Module RAMBundleRegistry::getModule(
uint32_t bundleId, uint32_t moduleId) {
if (m_bundles.find(bundleId) == m_bundles.end()) { if (m_bundles.find(bundleId) == m_bundles.end()) {
if (!m_factory) { if (!m_factory) {
throw std::runtime_error("You need to register factory function in order to support multiple RAM bundles."); throw std::runtime_error(
"You need to register factory function in order to "
"support multiple RAM bundles."
);
} }
auto bundlePath = m_bundlePaths.find(bundleId); auto bundlePath = m_bundlePaths.find(bundleId);
if (bundlePath == m_bundlePaths.end()) { if (bundlePath == m_bundlePaths.end()) {
throw std::runtime_error("In order to fetch RAM bundle from the registry, its file path needs to be registered first."); throw std::runtime_error(
"In order to fetch RAM bundle from the registry, its file "
"path needs to be registered first."
);
} }
m_bundles.emplace(bundleId, m_factory(bundlePath->second)); m_bundles.emplace(bundleId, m_factory(bundlePath->second));
} }
@ -53,7 +66,7 @@ JSModulesUnbundle::Module RAMBundleRegistry::getModule(uint32_t bundleId, uint32
}; };
} }
JSModulesUnbundle *RAMBundleRegistry::getBundle(uint32_t bundleId) const { JSModulesUnbundle* RAMBundleRegistry::getBundle(uint32_t bundleId) const {
return m_bundles.at(bundleId).get(); return m_bundles.at(bundleId).get();
} }

View File

@ -20,26 +20,31 @@ namespace react {
class RN_EXPORT RAMBundleRegistry : noncopyable { class RN_EXPORT RAMBundleRegistry : noncopyable {
public: public:
using unique_ram_bundle = std::unique_ptr<JSModulesUnbundle>;
using bundle_path = std::string;
constexpr static uint32_t MAIN_BUNDLE_ID = 0; constexpr static uint32_t MAIN_BUNDLE_ID = 0;
static std::unique_ptr<RAMBundleRegistry> singleBundleRegistry(unique_ram_bundle mainBundle); static std::unique_ptr<RAMBundleRegistry> singleBundleRegistry(
static std::unique_ptr<RAMBundleRegistry> multipleBundlesRegistry(unique_ram_bundle mainBundle, std::function<unique_ram_bundle(bundle_path)> factory); std::unique_ptr<JSModulesUnbundle> mainBundle);
static std::unique_ptr<RAMBundleRegistry> multipleBundlesRegistry(
std::unique_ptr<JSModulesUnbundle> mainBundle,
std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory);
explicit RAMBundleRegistry(
std::unique_ptr<JSModulesUnbundle> mainBundle,
std::function<
std::unique_ptr<JSModulesUnbundle>(std::string)> factory = nullptr);
RAMBundleRegistry(RAMBundleRegistry&&) = default; RAMBundleRegistry(RAMBundleRegistry&&) = default;
RAMBundleRegistry& operator=(RAMBundleRegistry&&) = default; RAMBundleRegistry& operator=(RAMBundleRegistry&&) = default;
void registerBundle(uint32_t bundleId, bundle_path bundlePath); void registerBundle(uint32_t bundleId, std::string bundlePath);
JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId); JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId);
virtual ~RAMBundleRegistry() {}; virtual ~RAMBundleRegistry() {};
private: private:
explicit RAMBundleRegistry(unique_ram_bundle mainBundle, std::function<unique_ram_bundle(bundle_path)> factory = {}); JSModulesUnbundle* getBundle(uint32_t bundleId) const;
JSModulesUnbundle *getBundle(uint32_t bundleId) const;
std::function<unique_ram_bundle(bundle_path)> m_factory; std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> m_factory;
std::unordered_map<uint32_t, bundle_path> m_bundlePaths; std::unordered_map<uint32_t, std::string> m_bundlePaths;
std::unordered_map<uint32_t, unique_ram_bundle> m_bundles; std::unordered_map<uint32_t, std::unique_ptr<JSModulesUnbundle>> m_bundles;
}; };
} // namespace react } // namespace react