Skip to content

Commit

Permalink
Clean up eventTarget retaining logic in the pointer event processor (f…
Browse files Browse the repository at this point in the history
…acebook#41471)

Summary:

Changelog: [Internal] - Clean up eventTarget retaining logic in the pointer event processor

This refactors calls to EventTarget::retain/release to occur in the actual methods that require the event target to be retained instead of expecting the caller to manage that which should be more maintainable.

Differential Revision: D51279974
  • Loading branch information
vincentriemer authored and facebook-github-bot committed Nov 14, 2023
1 parent ad0f064 commit 95a9b71
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace facebook::react {
static ShadowNode::Shared getShadowNodeFromEventTarget(
jsi::Runtime& runtime,
const EventTarget& target) {
target.retain(runtime);
auto instanceHandle = target.getInstanceHandle(runtime);
target.release(runtime);
if (instanceHandle.isObject()) {
auto handleObj = instanceHandle.asObject(runtime);
if (handleObj.hasProperty(runtime, "stateNode")) {
Expand Down Expand Up @@ -230,13 +232,11 @@ void PointerEventsProcessor::interceptPointerEvent(
}
}

eventTarget->retain(runtime);
auto shadowNode = getShadowNodeFromEventTarget(runtime, *eventTarget);
if (shadowNode != nullptr &&
shouldEmitPointerEvent(*shadowNode, type, uiManager)) {
eventDispatcher(runtime, eventTarget, type, priority, pointerEvent);
}
eventTarget->release(runtime);

// Implicit pointer capture release
if (overrideTarget != nullptr &&
Expand Down Expand Up @@ -354,7 +354,6 @@ void PointerEventsProcessor::processPendingPointerCapture(
if (hasActiveOverride && activeOverrideTag != pendingOverrideTag) {
auto retargeted = retargetPointerEvent(event, *activeOverride, uiManager);

retargeted.target->retain(runtime);
auto shadowNode = getShadowNodeFromEventTarget(runtime, *retargeted.target);
if (shadowNode != nullptr &&
shouldEmitPointerEvent(
Expand All @@ -366,13 +365,11 @@ void PointerEventsProcessor::processPendingPointerCapture(
ReactEventPriority::Discrete,
retargeted.event);
}
retargeted.target->release(runtime);
}

if (hasPendingOverride && activeOverrideTag != pendingOverrideTag) {
auto retargeted = retargetPointerEvent(event, *pendingOverride, uiManager);

retargeted.target->retain(runtime);
auto shadowNode = getShadowNodeFromEventTarget(runtime, *retargeted.target);
if (shadowNode != nullptr &&
shouldEmitPointerEvent(
Expand All @@ -384,7 +381,6 @@ void PointerEventsProcessor::processPendingPointerCapture(
ReactEventPriority::Discrete,
retargeted.event);
}
retargeted.target->release(runtime);
}

if (!hasPendingOverride) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,19 @@ void UIManagerBinding::dispatchEvent(
const EventPayload& eventPayload) const {
SystraceSection s("UIManagerBinding::dispatchEvent", "type", type);

if (eventPayload.getType() == EventPayloadType::PointerEvent) {
if (eventTarget != nullptr &&
eventPayload.getType() == EventPayloadType::PointerEvent) {
auto pointerEvent = static_cast<const PointerEvent&>(eventPayload);
auto dispatchCallback = [this](
jsi::Runtime& runtime,
const EventTarget* eventTarget,
const std::string& type,
ReactEventPriority priority,
const EventPayload& eventPayload) {
eventTarget->retain(runtime);
this->dispatchEventToJS(
runtime, eventTarget, type, priority, eventPayload);
eventTarget->release(runtime);
};
pointerEventsProcessor_.interceptPointerEvent(
runtime,
Expand Down

0 comments on commit 95a9b71

Please sign in to comment.