UNPKG

1.95 kBtext/x-cView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#include "EventTarget.h"
9
10namespace facebook {
11namespace react {
12
13using Tag = EventTarget::Tag;
14
15EventTarget::EventTarget(
16 jsi::Runtime &runtime,
17 const jsi::Value &instanceHandle,
18 Tag tag)
19 : weakInstanceHandle_(
20 jsi::WeakObject(runtime, instanceHandle.asObject(runtime))),
21 strongInstanceHandle_(jsi::Value::null()),
22 tag_(tag) {}
23
24void EventTarget::setEnabled(bool enabled) const {
25 enabled_ = enabled;
26}
27
28void EventTarget::retain(jsi::Runtime &runtime) const {
29 if (!enabled_) {
30 return;
31 }
32
33 strongInstanceHandle_ = weakInstanceHandle_.lock(runtime);
34
35 // Having a `null` or `undefined` object here indicates that
36 // `weakInstanceHandle_` was already deallocated. This should *not* happen by
37 // design, and if it happens it's a severe problem. This basically means that
38 // particular implementation of JSI was able to detect this inconsistency and
39 // dealt with it, but some JSI implementation may not support this feature and
40 // that case will lead to a crash in those environments.
41 assert(!strongInstanceHandle_.isNull());
42 assert(!strongInstanceHandle_.isUndefined());
43}
44
45void EventTarget::release(jsi::Runtime &runtime) const {
46 // The method does not use `jsi::Runtime` reference.
47 // It takes it only to ensure thread-safety (if the caller has the reference,
48 // we are on a proper thread).
49 strongInstanceHandle_ = jsi::Value::null();
50}
51
52jsi::Value EventTarget::getInstanceHandle(jsi::Runtime &runtime) const {
53 if (strongInstanceHandle_.isNull()) {
54 // The `instanceHandle` is not retained.
55 return jsi::Value::null();
56 }
57
58 return jsi::Value(runtime, strongInstanceHandle_);
59}
60
61Tag EventTarget::getTag() const {
62 return tag_;
63}
64
65} // namespace react
66} // namespace facebook