Permalink
Browse files

Introducting `UIManager.setLocalData()`, the way to provide environme…

…ntal data to ShadowNode

Summary:
In some cases we need a way to provide some peice of data to shadow node
to improve layout (or do something similar), `setLocalData` allows to do this.

Reviewed By: AaaChiuuu

Differential Revision: D5828368

fbshipit-source-id: bf6a04f460dc7695a16269426d365b78909bc8eb
  • Loading branch information...
shergin authored and facebook-github-bot committed Oct 2, 2017
1 parent abed3cf commit d0790fea39ccc755abb635ff72819831264cff29
@@ -181,6 +181,18 @@
boolean isDescendantOf(T ancestorNode);
/*
* In some cases we need a way to specify some environmental data to shadow node
* to improve layout (or do something similar), so {@code localData} serves these needs.
* For example, any stateful embedded native views may benefit from this.
* Have in mind that this data is not supposed to interfere with the state of
* the shadow node.
* Please respect one-directional data flow of React.
* Use {@link UIManagerModule#setViewLocalData} to set this property
* (to provide local/environmental data for a shadow node) from the main thread.
*/
public void setLocalData(Object data);
/**
* Returns the offset within the native children owned by all layout-only nodes in the subtree
* rooted at this node for the given child. Put another way, this returns the number of native
@@ -518,6 +518,18 @@ public boolean isDescendantOf(ReactShadowNodeImpl ancestorNode) {
return isDescendant;
}
/*
* In some cases we need a way to specify some environmental data to shadow node
* to improve layout (or do something similar), so {@code localData} serves these needs.
* For example, any stateful embedded native views may benefit from this.
* Have in mind that this data is not supposed to interfere with the state of
* the shadow node.
* Please respect one-directional data flow of React.
* Use {@link ReactUIManagerModule#setViewLocalData} to set this property
* (to provide local/environmental data for a shadow node) from the main thread.
*/
public void setLocalData(Object data) {}
/**
* Returns the offset within the native children owned by all layout-only nodes in the subtree
* rooted at this node for the given child. Put another way, this returns the number of native
@@ -236,12 +236,20 @@ public void updateNodeSize(
cssNode.setStyleWidth(newWidth);
cssNode.setStyleHeight(newHeight);
// If we're in the middle of a batch, the change will automatically be dispatched at the end of
// the batch. As all batches are executed as a single runnable on the event queue this should
// always be empty, but that calling architecture is an implementation detail.
if (mOperationsQueue.isEmpty()) {
dispatchViewUpdates(-1); // -1 = no associated batch id
dispatchViewUpdatesIfNeeded();
}
public void setViewLocalData(int tag, Object data) {
ReactShadowNode shadowNode = mShadowNodeRegistry.getNode(tag);
if (shadowNode == null) {
throw new IllegalViewOperationException(
"Trying to set local data for view with unknown tag: " + tag);
}
shadowNode.setLocalData(data);
dispatchViewUpdatesIfNeeded();
}
public void profileNextBatch() {
@@ -641,6 +649,17 @@ public void dispatchViewUpdates(int batchId) {
}
}
private void dispatchViewUpdatesIfNeeded() {
// If we are in the middle of a batch update, any additional changes
// will automatically be dispatched at the end of the batch.
// If we are not, we have to initiate new batch update.
// As all batches are executed as a single runnable on the event queue
// this should always be empty, but that calling architecture is an implementation detail.
if (mOperationsQueue.isEmpty()) {
dispatchViewUpdates(-1); // "-1" means "no associated batch id"
}
}
protected void updateViewHierarchy() {
Systrace.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
@@ -324,6 +324,29 @@ public void updateNodeSize(int nodeViewTag, int newWidth, int newHeight) {
mUIImplementation.updateNodeSize(nodeViewTag, newWidth, newHeight);
}
/**
* Sets local data for a shadow node corresponded with given tag.
* In some cases we need a way to specify some environmental data to shadow node
* to improve layout (or do something similar), so {@code localData} serves these needs.
* For example, any stateful embedded native views may benefit from this.
* Have in mind that this data is not supposed to interfere with the state of
* the shadow view.
* Please respect one-directional data flow of React.
*/
public void setViewLocalData(final int tag, final Object data) {
final ReactApplicationContext reactApplicationContext = getReactApplicationContext();
reactApplicationContext.assertOnUiQueueThread();
reactApplicationContext.runUIBackgroundRunnable(
new GuardedRunnable(reactApplicationContext) {
@Override
public void runGuarded() {
mUIImplementation.setViewLocalData(tag, data);
}
});
}
@ReactMethod
public void createView(int tag, String className, int rootViewTag, ReadableMap props) {
if (DEBUG) {

0 comments on commit d0790fe

Please sign in to comment.