/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

// [Windows] Brought forward from react-native - switch to using react-native/ReactCxxPlatform/react/threading version
// once we integrate that far

#include "MessageQueueThreadImpl.h"

#include <functional>

namespace facebook::react {

void MessageQueueThreadImpl::runOnQueue(std::function<void()> &&runnable) {
  if (!taskDispatchThread_.isRunning()) {
    return;
  }
  taskDispatchThread_.runAsync([runnable = std::move(runnable)]() noexcept { runnable(); });
}

void MessageQueueThreadImpl::runOnQueueSync(std::function<void()> &&runnable) {
  if (!taskDispatchThread_.isRunning()) {
    return;
  }
  if (taskDispatchThread_.isOnThread()) {
    runnable();
  } else {
    taskDispatchThread_.runSync([runnable = std::move(runnable)]() noexcept { runnable(); });
  }
}

void MessageQueueThreadImpl::quitSynchronous() {
  taskDispatchThread_.quit();
}

} // namespace facebook::react
