DispatchMessageQueueThread.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #include <glog/logging.h>
  8. #include <React/RCTLog.h>
  9. #include <cxxreact/MessageQueueThread.h>
  10. namespace facebook {
  11. namespace react {
  12. // RCTNativeModule arranges for native methods to be invoked on a queue which
  13. // is not the JS thread. C++ modules don't use RCTNativeModule, so this little
  14. // adapter does the work.
  15. class DispatchMessageQueueThread : public MessageQueueThread {
  16. public:
  17. DispatchMessageQueueThread(RCTModuleData *moduleData) : moduleData_(moduleData) {}
  18. void runOnQueue(std::function<void()> &&func) override
  19. {
  20. dispatch_queue_t queue = moduleData_.methodQueue;
  21. dispatch_block_t block = [func = std::move(func)] { func(); };
  22. RCTAssert(block != nullptr, @"Invalid block generated in call to %@", moduleData_);
  23. if (queue && block) {
  24. dispatch_async(queue, block);
  25. }
  26. }
  27. void runOnQueueSync(std::function<void()> &&__unused func) override
  28. {
  29. LOG(FATAL) << "Unsupported operation";
  30. }
  31. void quitSynchronous() override
  32. {
  33. LOG(FATAL) << "Unsupported operation";
  34. }
  35. private:
  36. RCTModuleData *moduleData_;
  37. };
  38. }
  39. }