Fabric: Farewell EventBeatBasedExecutor
Summary: We don't use it anymore. Reviewed By: JoshuaGross Differential Revision: D17115539 fbshipit-source-id: e1fa5cc023cd27e53307aa0ea8eae0bad5413be3
This commit is contained in:
Родитель
6a10feacda
Коммит
4ffe5b6f6d
|
@ -1,82 +0,0 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "EventBeatBasedExecutor.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
using Mode = EventBeatBasedExecutor::Mode;
|
||||
|
||||
EventBeatBasedExecutor::EventBeatBasedExecutor(
|
||||
std::unique_ptr<EventBeat> eventBeat)
|
||||
: eventBeat_(std::move(eventBeat)) {
|
||||
eventBeat_->setBeatCallback(
|
||||
std::bind(&EventBeatBasedExecutor::onBeat, this, true));
|
||||
eventBeat_->setFailCallback(
|
||||
std::bind(&EventBeatBasedExecutor::onBeat, this, false));
|
||||
}
|
||||
|
||||
void EventBeatBasedExecutor::operator()(Routine routine, Mode mode) const {
|
||||
if (mode == Mode::Asynchronous) {
|
||||
execute({
|
||||
/* .routine = */ std::move(routine),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
std::mutex mutex;
|
||||
mutex.lock();
|
||||
|
||||
execute({
|
||||
/* .routine = */ std::move(routine),
|
||||
/* .callback = */ [&mutex]() { mutex.unlock(); },
|
||||
});
|
||||
|
||||
mutex.lock();
|
||||
}
|
||||
|
||||
void EventBeatBasedExecutor::execute(Task task) const {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
tasks_.push_back(std::move(task));
|
||||
}
|
||||
|
||||
eventBeat_->request();
|
||||
eventBeat_->induce();
|
||||
}
|
||||
|
||||
void EventBeatBasedExecutor::onBeat(bool success) const {
|
||||
std::vector<Task> tasks;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (tasks_.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
tasks = std::move(tasks_);
|
||||
tasks_.clear();
|
||||
}
|
||||
|
||||
for (const auto task : tasks) {
|
||||
if (success) {
|
||||
task.routine();
|
||||
}
|
||||
|
||||
if (task.callback) {
|
||||
task.callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
|
@ -1,53 +0,0 @@
|
|||
/**
|
||||
* 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 <react/core/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(bool success = true) 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
|
Загрузка…
Ссылка в новой задаче