Skip to content

Commit

Permalink
chore: move to weakThis strongThis convention
Browse files Browse the repository at this point in the history
  • Loading branch information
tjzel committed Jan 22, 2025
1 parent 171079c commit 896409f
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ void CSSTransitionsRegistry::scheduleOrActivateTransition(
}

PropsObserver CSSTransitionsRegistry::createPropsObserver(const Tag viewTag) {
return [weakCSSTransitionsRegistry = weak_from_this(), viewTag](
return [weakThis = weak_from_this(), viewTag](
jsi::Runtime &rt,
const jsi::Value &oldProps,
const jsi::Value &newProps) {
auto cssTransitionsRegistry = weakCSSTransitionsRegistry.lock();
if (!cssTransitionsRegistry) {
auto strongThis = weakThis.lock();
if (!strongThis) {
return;
}

const auto &transition = cssTransitionsRegistry->registry_.at(viewTag);
const auto &transition = strongThis->registry_.at(viewTag);
const auto changedProps = getChangedProps(
rt,
oldProps,
Expand All @@ -140,15 +140,14 @@ PropsObserver CSSTransitionsRegistry::createPropsObserver(const Tag viewTag) {
}

{
std::lock_guard<std::mutex> lock{cssTransitionsRegistry->mutex_};
std::lock_guard<std::mutex> lock{strongThis->mutex_};

const auto &initialProps = transition->run(
rt, changedProps, cssTransitionsRegistry->getCurrentTimestamp_());
const auto &initialProps =
transition->run(rt, changedProps, strongThis->getCurrentTimestamp_());
const auto &shadowNode = transition->getShadowNode();

cssTransitionsRegistry->setInUpdatesRegistry(
rt, shadowNode, initialProps);
cssTransitionsRegistry->scheduleOrActivateTransition(transition);
strongThis->setInUpdatesRegistry(rt, shadowNode, initialProps);
strongThis->scheduleOrActivateTransition(transition);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

namespace reanimated {

class CSSTransitionsRegistry : public UpdatesRegistry, public std::enable_shared_from_this<CSSTransitionsRegistry> {
class CSSTransitionsRegistry
: public UpdatesRegistry,
public std::enable_shared_from_this<CSSTransitionsRegistry> {
public:
CSSTransitionsRegistry(
const std::shared_ptr<StaticPropsRegistry> &staticPropsRegistry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,20 @@ void ReanimatedCommitHook::maybeInitializeLayoutAnimations(
// when a new surfaceId is observed we call setMountingOverrideDelegate
// for all yet unseen surfaces
uiManager_->getShadowTreeRegistry().enumerate(
[weakReanimatedCommitHook = weak_from_this()](
[weakThis = weak_from_this()](
const ShadowTree &shadowTree, bool &stop) {
auto reanimatedCommitHook = weakReanimatedCommitHook.lock();
if (!reanimatedCommitHook) {
auto strongThis = weakThis.lock();
if (!strongThis) {
return;
}

if (shadowTree.getSurfaceId() <=
reanimatedCommitHook->currentMaxSurfaceId_) {
if (shadowTree.getSurfaceId() <= strongThis->currentMaxSurfaceId_) {
// the set function actually adds our delegate to a list, so we
// shouldn't invoke it twice for the same surface
return;
}
shadowTree.getMountingCoordinator()->setMountingOverrideDelegate(
reanimatedCommitHook->layoutAnimationsProxy_);
strongThis->layoutAnimationsProxy_);
});
currentMaxSurfaceId_ = surfaceId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,30 +618,30 @@ void LayoutAnimationsProxy::startEnteringAnimation(
static_cast<const ViewProps &>(*mutation.newChildShadowView.props);
auto opacity = viewProps.opacity;

uiScheduler_->scheduleOnUI([weakLayoutAnimationsProxy = weak_from_this(),
uiScheduler_->scheduleOnUI([weakThis = weak_from_this(),
finalView,
current,
parent,
mutation,
opacity,
tag]() {
auto layoutAnimationsProxy = weakLayoutAnimationsProxy.lock();
if (!layoutAnimationsProxy) {
auto strongThis = weakThis.lock();
if (!strongThis) {
return;
}

Rect window{};
{
auto &mutex = layoutAnimationsProxy->mutex;
auto &mutex = strongThis->mutex;
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
layoutAnimationsProxy->layoutAnimations_.insert_or_assign(
strongThis->layoutAnimations_.insert_or_assign(
tag, LayoutAnimation{finalView, current, parent, opacity});
window = layoutAnimationsProxy->surfaceManager.getWindow(
window = strongThis->surfaceManager.getWindow(
mutation.newChildShadowView.surfaceId);
}

Snapshot values(mutation.newChildShadowView, window);
auto &uiRuntime = layoutAnimationsProxy->uiRuntime_;
auto &uiRuntime = strongThis->uiRuntime_;
jsi::Object yogaValues(uiRuntime);
yogaValues.setProperty(uiRuntime, "targetOriginX", values.x);
yogaValues.setProperty(uiRuntime, "targetGlobalOriginX", values.x);
Expand All @@ -651,7 +651,7 @@ void LayoutAnimationsProxy::startEnteringAnimation(
yogaValues.setProperty(uiRuntime, "targetHeight", values.height);
yogaValues.setProperty(uiRuntime, "windowWidth", values.windowWidth);
yogaValues.setProperty(uiRuntime, "windowHeight", values.windowHeight);
layoutAnimationsProxy->layoutAnimationsManager_->startLayoutAnimation(
strongThis->layoutAnimationsManager_->startLayoutAnimation(
uiRuntime, tag, LayoutAnimationType::ENTERING, yogaValues);
});
}
Expand All @@ -664,42 +664,38 @@ void LayoutAnimationsProxy::startExitingAnimation(
#endif
auto surfaceId = mutation.oldChildShadowView.surfaceId;

uiScheduler_->scheduleOnUI([weakLayoutAnimationsProxy = weak_from_this(),
tag,
mutation,
surfaceId]() {
auto layoutAnimationsProxy = weakLayoutAnimationsProxy.lock();
if (!layoutAnimationsProxy) {
return;
}

auto oldView = mutation.oldChildShadowView;
Rect window{};
{
auto &mutex = layoutAnimationsProxy->mutex;
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
layoutAnimationsProxy->createLayoutAnimation(
mutation, oldView, surfaceId, tag);
window = layoutAnimationsProxy->surfaceManager.getWindow(surfaceId);
}
uiScheduler_->scheduleOnUI(
[weakThis = weak_from_this(), tag, mutation, surfaceId]() {
auto strongThis = weakThis.lock();
if (!strongThis) {
return;
}

Snapshot values(oldView, window);
auto oldView = mutation.oldChildShadowView;
Rect window{};
{
auto &mutex = strongThis->mutex;
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
strongThis->createLayoutAnimation(mutation, oldView, surfaceId, tag);
window = strongThis->surfaceManager.getWindow(surfaceId);
}

auto &uiRuntime = layoutAnimationsProxy->uiRuntime_;
jsi::Object yogaValues(uiRuntime);
yogaValues.setProperty(uiRuntime, "currentOriginX", values.x);
yogaValues.setProperty(uiRuntime, "currentGlobalOriginX", values.x);
yogaValues.setProperty(uiRuntime, "currentOriginY", values.y);
yogaValues.setProperty(uiRuntime, "currentGlobalOriginY", values.y);
yogaValues.setProperty(uiRuntime, "currentWidth", values.width);
yogaValues.setProperty(uiRuntime, "currentHeight", values.height);
yogaValues.setProperty(uiRuntime, "windowWidth", values.windowWidth);
yogaValues.setProperty(uiRuntime, "windowHeight", values.windowHeight);
layoutAnimationsProxy->layoutAnimationsManager_->startLayoutAnimation(
uiRuntime, tag, LayoutAnimationType::EXITING, yogaValues);
layoutAnimationsProxy->layoutAnimationsManager_->clearLayoutAnimationConfig(
tag);
});
Snapshot values(oldView, window);

auto &uiRuntime = strongThis->uiRuntime_;
jsi::Object yogaValues(uiRuntime);
yogaValues.setProperty(uiRuntime, "currentOriginX", values.x);
yogaValues.setProperty(uiRuntime, "currentGlobalOriginX", values.x);
yogaValues.setProperty(uiRuntime, "currentOriginY", values.y);
yogaValues.setProperty(uiRuntime, "currentGlobalOriginY", values.y);
yogaValues.setProperty(uiRuntime, "currentWidth", values.width);
yogaValues.setProperty(uiRuntime, "currentHeight", values.height);
yogaValues.setProperty(uiRuntime, "windowWidth", values.windowWidth);
yogaValues.setProperty(uiRuntime, "windowHeight", values.windowHeight);
strongThis->layoutAnimationsManager_->startLayoutAnimation(
uiRuntime, tag, LayoutAnimationType::EXITING, yogaValues);
strongThis->layoutAnimationsManager_->clearLayoutAnimationConfig(tag);
});
}

void LayoutAnimationsProxy::startLayoutAnimation(
Expand All @@ -710,29 +706,28 @@ void LayoutAnimationsProxy::startLayoutAnimation(
#endif
auto surfaceId = mutation.oldChildShadowView.surfaceId;

uiScheduler_->scheduleOnUI([weakLayoutAnimationsProxy = weak_from_this(),
uiScheduler_->scheduleOnUI([weakThis = weak_from_this(),
mutation,
surfaceId,
tag]() {
auto layoutAnimationsProxy = weakLayoutAnimationsProxy.lock();
if (!layoutAnimationsProxy) {
auto strongThis = weakThis.lock();
if (!strongThis) {
return;
}

auto oldView = mutation.oldChildShadowView;
Rect window{};
{
auto &mutex = layoutAnimationsProxy->mutex;
auto &mutex = strongThis->mutex;
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
layoutAnimationsProxy->createLayoutAnimation(
mutation, oldView, surfaceId, tag);
window = layoutAnimationsProxy->surfaceManager.getWindow(surfaceId);
strongThis->createLayoutAnimation(mutation, oldView, surfaceId, tag);
window = strongThis->surfaceManager.getWindow(surfaceId);
}

Snapshot currentValues(oldView, window);
Snapshot targetValues(mutation.newChildShadowView, window);

auto &uiRuntime = layoutAnimationsProxy->uiRuntime_;
auto &uiRuntime = strongThis->uiRuntime_;
jsi::Object yogaValues(uiRuntime);
yogaValues.setProperty(uiRuntime, "currentOriginX", currentValues.x);
yogaValues.setProperty(uiRuntime, "currentGlobalOriginX", currentValues.x);
Expand All @@ -749,7 +744,7 @@ void LayoutAnimationsProxy::startLayoutAnimation(
yogaValues.setProperty(uiRuntime, "windowWidth", targetValues.windowWidth);
yogaValues.setProperty(
uiRuntime, "windowHeight", targetValues.windowHeight);
layoutAnimationsProxy->layoutAnimationsManager_->startLayoutAnimation(
strongThis->layoutAnimationsManager_->startLayoutAnimation(
uiRuntime, tag, LayoutAnimationType::LAYOUT, yogaValues);
});
}
Expand All @@ -766,17 +761,15 @@ void LayoutAnimationsProxy::maybeCancelAnimation(const int tag) const {
return;
}
layoutAnimations_.erase(tag);
uiScheduler_->scheduleOnUI(
[weakLayoutAnimationsProxy = weak_from_this(), tag]() {
auto layoutAnimationsProxy = weakLayoutAnimationsProxy.lock();
if (!layoutAnimationsProxy) {
return;
}
uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), tag]() {
auto strongThis = weakThis.lock();
if (!strongThis) {
return;
}

auto &uiRuntime = layoutAnimationsProxy->uiRuntime_;
layoutAnimationsProxy->layoutAnimationsManager_->cancelLayoutAnimation(
uiRuntime, tag);
});
auto &uiRuntime = strongThis->uiRuntime_;
strongThis->layoutAnimationsManager_->cancelLayoutAnimation(uiRuntime, tag);
});
}

void LayoutAnimationsProxy::transferConfigFromNativeID(
Expand Down
Loading

0 comments on commit 896409f

Please sign in to comment.