Files
react-native/ReactCommon/fabric/events/EventTarget.cpp
Valentin Shergin b1814b37aa Fabric: Lock-free events 2/n: Reimagining of EventTarget
Summary:
EventTargetWrapper and EventTarget were merged into one class that controls an `instanceHandle` reference and extracting a strong reference to it.

This diff also decouples the operation of retaining a strong reference (with checking a `enabled` flag) from actual usage of this reference. This allows to wrap into a mutex only first part of this process and avoid possible deadlocks.

Reviewed By: sahrens

Differential Revision: D13616382

fbshipit-source-id: 9907bc12047386fcf027929ae2ae41c0b727cd06
2019-01-16 20:22:39 -08:00

49 lines
1.1 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 "EventTarget.h"
namespace facebook {
namespace react {
using Tag = EventTarget::Tag;
EventTarget::EventTarget(
jsi::Runtime &runtime,
const jsi::Value &instanceHandle,
Tag tag)
: weakInstanceHandle_(
jsi::WeakObject(runtime, instanceHandle.asObject(runtime))),
strongInstanceHandle_(jsi::Value::null()),
tag_(tag) {}
void EventTarget::setEnabled(bool enabled) const {
enabled_ = enabled;
}
void EventTarget::retain(jsi::Runtime &runtime) const {
if (!enabled_) {
return;
}
strongInstanceHandle_ = weakInstanceHandle_.lock(runtime);
}
jsi::Value EventTarget::release(jsi::Runtime &runtime) const {
// The method does not use `jsi::Runtime` reference.
// It takes it only to ensure thread-safety (if the caller has the reference,
// we are on a proper thread).
return std::move(strongInstanceHandle_);
}
Tag EventTarget::getTag() const {
return tag_;
}
} // namespace react
} // namespace facebook