Implement node.isConnected

Summary:
This implements the `isConnected` method as defined in https://github.com/react-native-community/discussions-and-proposals/pull/607.

This requires a new method in Fabric because we can't derive that from the existing methods.

Changelog: [internal]

bypass-github-export-checks

Reviewed By: sammy-SC

Differential Revision: D44346661

fbshipit-source-id: 1eefa589f2937d1dac2461350ddc8df006f0e873
This commit is contained in:
Rubén Norte
2023-04-13 09:19:00 -07:00
committed by Facebook GitHub Bot
parent be7f2ab4f6
commit 03430ca660
4 changed files with 37 additions and 1 deletions
+7 -1
View File
@@ -47,7 +47,13 @@ export default class ReadOnlyNode {
}
get isConnected(): boolean {
throw new TypeError('Unimplemented');
const shadowNode = getShadowNode(this);
if (shadowNode == null) {
return false;
}
return nullthrows(getFabricUIManager()).isConnected(shadowNode);
}
get lastChild(): ReadOnlyNode | null {
@@ -72,6 +72,7 @@ export type Spec = {|
) => void,
+getParentNode: (node: Node) => ?InternalInstanceHandle,
+getChildNodes: (node: Node) => $ReadOnlyArray<InternalInstanceHandle>,
+isConnected: (node: Node) => boolean,
|};
// This is exposed as a getter because apps using the legacy renderer AND
@@ -285,6 +285,9 @@ const FabricUIManagerMock: FabricUIManager = {
);
},
),
isConnected: jest.fn((node: Node): boolean => {
return getNodeInCurrentTree(node) != null;
}),
};
global.nativeFabricUIManager = FabricUIManagerMock;
@@ -836,6 +836,32 @@ jsi::Value UIManagerBinding::get(
});
}
if (methodName == "isConnected") {
// This is a React Native implementation of `Node.prototype.isConnected`
// (see https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected).
// Indicates whether a version of the given shadow node is present in
// the current revision of an active shadow tree.
// isConnected(shadowNode: ShadowNode): boolean
return jsi::Function::createFromHostFunction(
runtime,
name,
1,
[uiManager](
jsi::Runtime &runtime,
jsi::Value const & /*thisValue*/,
jsi::Value const *arguments,
size_t /*count*/) noexcept -> jsi::Value {
auto shadowNode = shadowNodeFromValue(runtime, arguments[0]);
auto newestCloneOfShadowNode =
uiManager->getNewestCloneOfShadowNode(*shadowNode);
return jsi::Value(newestCloneOfShadowNode != nullptr);
});
}
return jsi::Value::undefined();
}