2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
2017-06-21 18:56:08 +00:00
|
|
|
#include <unordered_set>
|
2017-06-23 23:49:55 +00:00
|
|
|
#include <vector>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-06-23 23:49:55 +00:00
|
|
|
#include <cxxreact/JSExecutor.h>
|
2016-10-11 14:19:31 +00:00
|
|
|
#include <folly/Optional.h>
|
2017-02-15 17:49:07 +00:00
|
|
|
#include <folly/dynamic.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-06-27 18:15:36 +00:00
|
|
|
#ifndef RN_EXPORT
|
|
|
|
#define RN_EXPORT __attribute__((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
class NativeModule;
|
|
|
|
|
2016-10-11 14:19:31 +00:00
|
|
|
struct ModuleConfig {
|
|
|
|
size_t index;
|
|
|
|
folly::dynamic config;
|
|
|
|
};
|
|
|
|
|
2017-06-27 18:15:36 +00:00
|
|
|
class RN_EXPORT ModuleRegistry {
|
2016-05-04 02:29:58 +00:00
|
|
|
public:
|
|
|
|
// not implemented:
|
|
|
|
// onBatchComplete: see https://our.intern.facebook.com/intern/tasks/?t=5279396
|
|
|
|
// getModule: only used by views
|
|
|
|
// getAllModules: only used for cleanup; use RAII instead
|
|
|
|
// notifyCatalystInstanceInitialized: this is really only used by view-related code
|
|
|
|
// notifyCatalystInstanceDestroy: use RAII instead
|
|
|
|
|
2017-08-11 13:16:55 +00:00
|
|
|
using ModuleNotFoundCallback = std::function<bool(const std::string &name)>;
|
|
|
|
|
|
|
|
ModuleRegistry(std::vector<std::unique_ptr<NativeModule>> modules, ModuleNotFoundCallback callback = nullptr);
|
2017-04-07 16:27:05 +00:00
|
|
|
void registerModules(std::vector<std::unique_ptr<NativeModule>> modules);
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
std::vector<std::string> moduleNames();
|
2016-10-11 14:19:31 +00:00
|
|
|
|
|
|
|
folly::Optional<ModuleConfig> getConfig(const std::string& name);
|
|
|
|
|
2017-04-25 12:29:45 +00:00
|
|
|
void callNativeMethod(unsigned int moduleId, unsigned int methodId, folly::dynamic&& params, int callId);
|
|
|
|
MethodCallResult callSerializableNativeHook(unsigned int moduleId, unsigned int methodId, folly::dynamic&& args);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
private:
|
2016-05-18 19:46:01 +00:00
|
|
|
// This is always populated
|
2016-05-04 02:29:58 +00:00
|
|
|
std::vector<std::unique_ptr<NativeModule>> modules_;
|
2016-05-18 19:46:01 +00:00
|
|
|
|
2017-06-21 18:56:08 +00:00
|
|
|
// This is used to extend the population of modulesByName_ if registerModules is called after moduleNames
|
|
|
|
void updateModuleNamesFromIndex(size_t size);
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
// This is only populated if moduleNames() is called. Values are indices into modules_.
|
|
|
|
std::unordered_map<std::string, size_t> modulesByName_;
|
2017-06-21 18:56:08 +00:00
|
|
|
|
|
|
|
// This is populated with modules that are requested via getConfig but are unknown.
|
|
|
|
// An error will be thrown if they are subsquently added to the registry.
|
|
|
|
std::unordered_set<std::string> unknownModules_;
|
2017-08-11 13:16:55 +00:00
|
|
|
|
|
|
|
// Function will be called if a module was requested but was not found.
|
|
|
|
// If the function returns true, ModuleRegistry will try to find the module again (assuming it's registered)
|
|
|
|
// If the functon returns false, ModuleRegistry will not try to find the module and return nullptr instead.
|
|
|
|
ModuleNotFoundCallback moduleNotFoundCallback_;
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|