1 /*
2  * Copyright (c) Meta Platforms, Inc. and 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 
8 #include <glog/logging.h>
9 
10 #include <ABI48_0_0React/ABI48_0_0RCTLog.h>
11 #include <ABI48_0_0cxxreact/ABI48_0_0MessageQueueThread.h>
12 
13 namespace ABI48_0_0facebook {
14 namespace ABI48_0_0React {
15 
16 // ABI48_0_0RCTNativeModule arranges for native methods to be invoked on a queue which
17 // is not the JS thread.  C++ modules don't use ABI48_0_0RCTNativeModule, so this little
18 // adapter does the work.
19 
20 class DispatchMessageQueueThread : public MessageQueueThread {
21  public:
DispatchMessageQueueThread(ABI48_0_0RCTModuleData * moduleData)22   DispatchMessageQueueThread(ABI48_0_0RCTModuleData *moduleData) : moduleData_(moduleData) {}
23 
runOnQueue(std::function<void ()> && func)24   void runOnQueue(std::function<void()> &&func) override
25   {
26     dispatch_queue_t queue = moduleData_.methodQueue;
27     dispatch_block_t block = [func = std::move(func)] { func(); };
28     ABI48_0_0RCTAssert(block != nullptr, @"Invalid block generated in call to %@", moduleData_);
29     if (queue && block) {
30       dispatch_async(queue, block);
31     }
32   }
runOnQueueSync(std::function<void ()> && __unused func)33   void runOnQueueSync(std::function<void()> &&__unused func) override
34   {
35     LOG(FATAL) << "Unsupported operation";
36   }
quitSynchronous()37   void quitSynchronous() override
38   {
39     LOG(FATAL) << "Unsupported operation";
40   }
41 
42  private:
43   ABI48_0_0RCTModuleData *moduleData_;
44 };
45 
46 }
47 }
48