Files
react-native/ReactCommon/react/renderer/timeline/TimelineHandler.cpp
T
Valentin Shergin 336876509c Introducing Timeline: A time-travel debugging tool (cross-platform part)
Summary:
This is a core part of the Timeline feature (aka Time Travel Debugger). With these new primitives, any external library can initiate "saving" all the previous interface changes (commits) and unwind to any previous one (in order to introspect and validate visual side-effects).

The next diff in the stack will implement UI for this feature integrated into Debug menu on iOS.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D25926660

fbshipit-source-id: 2e5f6892351d3053db8f64c1cf6ff445b0867ad7
2021-03-24 11:30:24 -07:00

98 lines
2.3 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.
*/
#include "TimelineHandler.h"
#include <algorithm>
#include <react/renderer/timeline/Timeline.h>
namespace facebook {
namespace react {
TimelineHandler::TimelineHandler(Timeline const &timeline) noexcept
: timeline_(&timeline) {}
TimelineHandler::TimelineHandler(TimelineHandler &&other) noexcept {
this->operator=(std::move(other));
}
TimelineHandler::~TimelineHandler() noexcept {
if (timeline_ != nullptr) {
// Improper deallocation indicates a severe error in application logic:
abort();
}
}
TimelineHandler &TimelineHandler::operator=(TimelineHandler &&other) noexcept {
assert(other.timeline_ && "Moving from an empty `TimelineHandler`.");
timeline_ = other.timeline_;
other.timeline_ = nullptr;
return *this;
}
#pragma mark - Public
void TimelineHandler::pause() const noexcept {
ensureNotEmpty();
timeline_->pause();
}
void TimelineHandler::resume() const noexcept {
ensureNotEmpty();
timeline_->resume();
}
bool TimelineHandler::isPaused() const noexcept {
ensureNotEmpty();
return timeline_->isPaused();
}
TimelineFrame TimelineHandler::getCurrentFrame() const noexcept {
ensureNotEmpty();
return timeline_->getCurrentFrame();
}
TimelineFrame::List TimelineHandler::getFrames() const noexcept {
ensureNotEmpty();
return timeline_->getFrames();
}
void TimelineHandler::rewind(TimelineFrame const &frame) const noexcept {
ensureNotEmpty();
return timeline_->rewind(frame);
}
void TimelineHandler::seek(int delta) const noexcept {
ensureNotEmpty();
auto frames = timeline_->getFrames();
auto currentFrame = timeline_->getCurrentFrame();
auto seekFrameIndex = currentFrame.getIndex() + delta;
seekFrameIndex =
std::min((int)frames.size() - 1, std::max(0, seekFrameIndex));
timeline_->rewind(frames.at(seekFrameIndex));
}
#pragma mark - Private
SurfaceId TimelineHandler::getSurfaceId() const noexcept {
return timeline_->getSurfaceId();
}
void TimelineHandler::release() noexcept {
timeline_ = nullptr;
}
void TimelineHandler::ensureNotEmpty() const noexcept {
if (!timeline_) {
abort();
}
}
} // namespace react
} // namespace facebook