/* * 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. */ #pragma once #include #include #include #include namespace facebook::react { class BufferedRuntimeExecutor { public: using Work = std::function; // A utility structure to track pending work in the order of when they arrive. struct BufferedWork { uint64_t index_; Work work_; bool operator<(const BufferedWork& rhs) const { // Higher index has lower priority, so this inverted comparison puts // the smaller index on top of the queue. return index_ > rhs.index_; } }; BufferedRuntimeExecutor(RuntimeExecutor runtimeExecutor); void execute(Work&& callback); // Flush buffered JS calls and then diable JS buffering void flush(); private: // Perform flushing without locking mechanism void unsafeFlush(); RuntimeExecutor runtimeExecutor_; std::atomic isBufferingEnabled_; std::mutex lock_; std::atomic lastIndex_; std::priority_queue queue_; }; } // namespace facebook::react