Skip to content

Commit

Permalink
Add surfaceId to EventTarget (#46254)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46254

Changelog: [internal]

Right now it's very hard to access the surface ID from the target when dispatching events, and we need that to determine if the event we dispatched produced any updates its surface ID.

This adds surfaceId to EventTarget so we can access it without an unnecessary large amount of indirection in the current code.

This is a dependency for #46253 / D61939260, split to simplify reviewing.

Reviewed By: sammy-SC, rshest

Differential Revision: D61939910
  • Loading branch information
rubennorte authored and facebook-github-bot committed Aug 29, 2024
1 parent 8de2cbf commit bf2a50b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
ShadowNodeFamily::Shared createFamily(
const ShadowNodeFamilyFragment& fragment) const override {
auto eventEmitter = std::make_shared<const ConcreteEventEmitter>(
std::make_shared<EventTarget>(fragment.instanceHandle),
std::make_shared<EventTarget>(
fragment.instanceHandle, fragment.surfaceId),
eventDispatcher_);
return std::make_shared<ShadowNodeFamily>(
fragment, std::move(eventEmitter), eventDispatcher_, *this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ namespace facebook::react {

using Tag = EventTarget::Tag;

EventTarget::EventTarget(InstanceHandle::Shared instanceHandle)
EventTarget::EventTarget(
InstanceHandle::Shared instanceHandle,
SurfaceId surfaceId)
: instanceHandle_(std::move(instanceHandle)),
surfaceId_(surfaceId),
strongInstanceHandle_(jsi::Value::null()) {}

void EventTarget::setEnabled(bool enabled) const {
Expand Down Expand Up @@ -64,6 +67,10 @@ jsi::Value EventTarget::getInstanceHandle(jsi::Runtime& runtime) const {
return jsi::Value(runtime, strongInstanceHandle_);
}

SurfaceId EventTarget::getSurfaceId() const {
return surfaceId_;
}

Tag EventTarget::getTag() const {
return instanceHandle_->getTag();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class EventTarget {
/*
* Constructs an EventTarget from a weak instance handler and a tag.
*/
explicit EventTarget(InstanceHandle::Shared instanceHandle);
explicit EventTarget(
InstanceHandle::Shared instanceHandle,
SurfaceId surfaceId);

/*
* Sets the `enabled` flag that allows creating a strong instance handle from
Expand All @@ -59,13 +61,16 @@ class EventTarget {
*/
jsi::Value getInstanceHandle(jsi::Runtime& runtime) const;

SurfaceId getSurfaceId() const;

/*
* Deprecated. Do not use.
*/
Tag getTag() const;

private:
const InstanceHandle::Shared instanceHandle_;
const SurfaceId surfaceId_;
mutable bool enabled_{false}; // Protected by `EventEmitter::DispatchMutex()`.
mutable jsi::Value strongInstanceHandle_; // Protected by `jsi::Runtime &`.
mutable size_t retainCount_{0}; // Protected by `jsi::Runtime &`.
Expand Down

0 comments on commit bf2a50b

Please sign in to comment.