Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void NativeDOM::setNativeProps(
std::shared_ptr<const ShadowNode> shadowNode,
jsi::Value updatePayload) {
getUIManagerFromRuntime(rt).setNativeProps_DEPRECATED(
shadowNode, RawProps(rt, updatePayload));
rt, shadowNode, RawProps(rt, updatePayload));
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void UIManager::dispatchCommand(
}
}

void UIManager::setNativeProps_DEPRECATED(
ShadowTree::CommitStatus UIManager::setNativeProps_DEPRECATED(
const std::shared_ptr<const ShadowNode>& shadowNode,
RawProps rawProps) const {
auto& family = shadowNode->getFamily();
Expand All @@ -484,13 +484,14 @@ void UIManager::setNativeProps_DEPRECATED(
}
}

auto commitStatus = ShadowTree::CommitStatus::Cancelled;
shadowTreeRegistry_.visit(
family.getSurfaceId(), [&](const ShadowTree& shadowTree) {
// The lambda passed to `commit` may be executed multiple times.
// We need to create fresh copy of the `RawProps` object each time.
auto ancestorShadowNode =
shadowTree.getCurrentRevision().rootShadowNode;
shadowTree.commit(
commitStatus = shadowTree.commit(
[&](const RootShadowNode& oldRootShadowNode) {
auto rootNode = oldRootShadowNode.cloneTree(
family, [&](const ShadowNode& oldShadowNode) {
Expand All @@ -512,6 +513,20 @@ void UIManager::setNativeProps_DEPRECATED(
},
{/* default commit options */});
});
return commitStatus;
}

void UIManager::setNativeProps_DEPRECATED(
jsi::Runtime& /*runtime*/,
const std::shared_ptr<const ShadowNode>& shadowNode,
RawProps rawProps) const {
if (setNativeProps_DEPRECATED(shadowNode, std::move(rawProps)) ==
ShadowTree::CommitStatus::Succeeded) {
// As `completeSurface` does, so that reads later in the task see these
// props even if the task read this surface before the call.
lazyShadowTreeRevisionConsistencyManager_->updateCurrentRevision(
shadowNode->getSurfaceId());
}
}

void UIManager::sendAccessibilityEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,19 @@ class UIManager final : public ShadowTreeDelegate {
const std::string &commandName,
const folly::dynamic &args) const;

void setNativeProps_DEPRECATED(const std::shared_ptr<const ShadowNode> &shadowNode, RawProps rawProps) const;
ShadowTree::CommitStatus setNativeProps_DEPRECATED(
const std::shared_ptr<const ShadowNode> &shadowNode,
RawProps rawProps) const;

/*
* `setNativeProps` called from JS, which takes the runtime only to ensure it
* runs on the JS thread. Refreshes the revision that JS reads, as
* `completeSurface` does.
*/
void setNativeProps_DEPRECATED(
jsi::Runtime &runtime,
const std::shared_ptr<const ShadowNode> &shadowNode,
RawProps rawProps) const;

void sendAccessibilityEvent(const std::shared_ptr<const ShadowNode> &shadowNode, const std::string &eventType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ jsi::Value UIManagerBinding::get(
validateArgumentCount(runtime, methodName, paramCount, count);

uiManager->setNativeProps_DEPRECATED(
runtime,
Bridging<std::shared_ptr<const ShadowNode>>::fromJs(
runtime, arguments[0]),
RawProps(runtime, arguments[1]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace facebook::react {
/**
* This class implements UI consistency for the JavaScript thread.
* This implementation forces JavaScript to see a stable revision of the shadow
* tree for a given surface ID, only updating it when React commits a new tree
* or between JS tasks.
* tree for a given surface ID, only updating it when JS commits a new tree or
* between JS tasks.
*/
class LazyShadowTreeRevisionConsistencyManager : public ShadowTreeRevisionConsistencyManager,
public ShadowTreeRevisionProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';

import type {HostInstance} from 'react-native';

import {getFabricUIManager} from '../../../../../Libraries/ReactNative/FabricUIManager';
import * as Fantom from '@react-native/fantom';
import nullthrows from 'nullthrows';
import * as React from 'react';
import {useLayoutEffect} from 'react';
import {ScrollView, Text} from 'react-native';
import {ScrollView, Text, View} from 'react-native';
import {getNodeFromPublicInstance} from 'react-native/react-private-interface';

const UIManager = nullthrows(getFabricUIManager());

describe('UIConsistency', () => {
it('should provide consistent data from the tree within the same synchronous function', () => {
Expand Down Expand Up @@ -146,4 +150,29 @@ describe('UIConsistency', () => {
);
});
});

it('should provide up-to-date data after setNativeProps in the same task', () => {
const root = Fantom.createRoot();

const viewRef = React.createRef<HostInstance>();

Fantom.runTask(() => {
root.render(<View ref={viewRef} style={{width: 50, height: 10}} />);
});

const view = nullthrows(viewRef.current);
const widths: Array<number> = [];

Fantom.runTask(() => {
widths.push(view.getBoundingClientRect().width);
view.setNativeProps({style: {width: 100}});
widths.push(view.getBoundingClientRect().width);
UIManager.setNativeProps(nullthrows(getNodeFromPublicInstance(view)), {
width: 150,
});
widths.push(view.getBoundingClientRect().width);
});

expect(widths).toEqual([50, 100, 150]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,53 @@ describe('ResizeObserver', () => {
const [secondEntries] = callback.mock.calls[1];
expect(secondEntries.map(entry => entry.target)).toEqual([nodeB]);
});

it('should deliver a deeper target resized with setNativeProps from a callback on a follow-up loop round', () => {
const parentRef = createRef<HostInstance>();
const childRef = createRef<HostInstance>();
const root = Fantom.createRoot();

Fantom.runTask(() => {
root.render(
<View style={{width: 100, height: 100}} ref={parentRef}>
<View style={{width: 50, height: 50}} ref={childRef} />
</View>,
);
});

const parent = nullthrows(parentRef.current);
const child = nullthrows(childRef.current);
const events: Array<string> = [];

Fantom.runTask(() => {
observer = new ResizeObserver(entries => {
for (const entry of entries) {
const width = entry.contentRect.width;
if (entry.target === parent) {
events.push(`parent ${width}`);
if (width === 120) {
child.setNativeProps({style: {width: 80}});
}
} else {
events.push(`child ${width}`);
}
}
});
observer.observe(parent);
observer.observe(child);
});
expect(events).toEqual(['parent 100', 'child 50']);

events.length = 0;
Fantom.runTask(() => {
parent.setNativeProps({style: {width: 120}});
Fantom.scheduleTask(() => {
events.push('next task');
});
});

expect(events).toEqual(['parent 120', 'child 80', 'next task']);
});
});

describe('resize loop error', () => {
Expand Down Expand Up @@ -2437,6 +2484,34 @@ describe('ResizeObserver', () => {
expect(callback).toHaveBeenCalledTimes(2);
expectNoResizeLoopError();
});

it('should report a loop error when a callback resizes its own target with setNativeProps', () => {
const nodeRef = createRef<HostInstance>();
const root = Fantom.createRoot();

Fantom.runTask(() => {
root.render(<View style={{width: 100, height: 50}} ref={nodeRef} />);
});

const node = nullthrows(nodeRef.current);
const callback: ResizeObserverMockCallback = jest.fn(entries => {
if (entries[0].contentRect.width === 100) {
node.setNativeProps({style: {width: 120}});
}
});

Fantom.runTask(() => {
observer = new ResizeObserver(callback);
observer.observe(node);
});

// The new size is at the depth just delivered, so it's skipped.
expect(callback).toHaveBeenCalledTimes(1);
expect(consoleErrorMock).toHaveBeenCalledTimes(1);
expect(consoleErrorMock.mock.lastCall[0]).toBe(
'ResizeObserver loop completed with undelivered notifications.',
);
});
});

describe('state updates scheduled from callbacks', () => {
Expand Down
Loading