CxxNativeModule.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <cxxreact/CxxModule.h>
  9. #include <cxxreact/NativeModule.h>
  10. #ifndef RN_EXPORT
  11. #define RN_EXPORT __attribute__((visibility("default")))
  12. #endif
  13. namespace facebook {
  14. namespace react {
  15. class Instance;
  16. class MessageQueueThread;
  17. std::function<void(folly::dynamic)> makeCallback(
  18. std::weak_ptr<Instance> instance,
  19. const folly::dynamic &callbackId);
  20. class RN_EXPORT CxxNativeModule : public NativeModule {
  21. public:
  22. CxxNativeModule(
  23. std::weak_ptr<Instance> instance,
  24. std::string name,
  25. xplat::module::CxxModule::Provider provider,
  26. std::shared_ptr<MessageQueueThread> messageQueueThread)
  27. : instance_(instance),
  28. name_(std::move(name)),
  29. provider_(provider),
  30. messageQueueThread_(messageQueueThread) {}
  31. std::string getName() override;
  32. std::vector<MethodDescriptor> getMethods() override;
  33. folly::dynamic getConstants() override;
  34. void invoke(unsigned int reactMethodId, folly::dynamic &&params, int callId)
  35. override;
  36. MethodCallResult callSerializableNativeHook(
  37. unsigned int hookId,
  38. folly::dynamic &&args) override;
  39. private:
  40. void lazyInit();
  41. std::weak_ptr<Instance> instance_;
  42. std::string name_;
  43. xplat::module::CxxModule::Provider provider_;
  44. std::shared_ptr<MessageQueueThread> messageQueueThread_;
  45. std::unique_ptr<xplat::module::CxxModule> module_;
  46. std::vector<xplat::module::CxxModule::Method> methods_;
  47. };
  48. } // namespace react
  49. } // namespace facebook