mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-15 23:03:58 +08:00
Summary: EventBeatBasedExecutor is an executor derived from EventBeat and using EventBeat to ensure proper threading. Why do we need yet another executor? Because otherwise, we have to make it platform specific-dependency that each platform-specific implementation has to implement and provide. We already have all that we need in already provided EventBeat, so we can just convert that into simple executor in a platform-agnostic way. Reviewed By: mdvacca Differential Revision: D9995783 fbshipit-source-id: f8aa72a9744e50ebecbea9ad0e2546f41f5358f2
58 lines
1.2 KiB
C++
58 lines
1.2 KiB
C++
/**
|
|
* Copyright (c) Facebook, Inc. and its 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 <memory>
|
|
#include <mutex>
|
|
#include <vector>
|
|
|
|
#include <fabric/events/EventBeat.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/*
|
|
* General purpose executor that uses EventBeat to ensure proper threading.
|
|
*/
|
|
class EventBeatBasedExecutor {
|
|
|
|
public:
|
|
using Routine = std::function<void()>;
|
|
using Callback = std::function<void()>;
|
|
|
|
struct Task {
|
|
Routine routine;
|
|
Callback callback;
|
|
};
|
|
|
|
enum class Mode {
|
|
Synchronous,
|
|
Asynchronous
|
|
};
|
|
|
|
EventBeatBasedExecutor(std::unique_ptr<EventBeat> eventBeat);
|
|
|
|
/*
|
|
* Executes given routine with given mode.
|
|
*/
|
|
void operator()(Routine routine, Mode mode = Mode::Asynchronous) const;
|
|
|
|
private:
|
|
void onBeat() const;
|
|
void execute(Task task) const;
|
|
|
|
std::unique_ptr<EventBeat> eventBeat_;
|
|
mutable std::vector<Task> tasks_; // Protected by `mutex_`.
|
|
mutable std::mutex mutex_;
|
|
};
|
|
|
|
using EventBeatFactory = std::function<std::unique_ptr<EventBeat>()>;
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|