mirror of
https://github.com/status-im/react-native.git
synced 2025-02-08 23:53:27 +00:00
Summary: public Native view recycling implementation based on limited pools of views. In this diff I introduced new UIManager method: dropViews. Instead of removing views from tag->view maps when they are detached we keep them there until we get a call to dropViews with the appropriate tag. JS may keep a pool of object and selectively decide not to enqueue drop for certain views. Then instead of removing those views it may decide to reuse tag that has been previously allocated for a view that is no longer in use. Special handling is required for layout-only nodes as they only can transition from layout-only to non-layout-only (reverse transition hasn't been implemented). Because of that we'd loose benefits of view flattening if we decide to recycle existing non-layout-only view as a layout-only one. This diff provides only a simple and manual method for configuring pools by calling `ReactNativeViewPool.configure` with a dict from native view name to the view count. Note that we may not want recycle all the views (e.g. when we render mapview we don't want to keep it in memory after it's detached) Reviewed By: davidaurelio Differential Revision: D2677289 fb-gh-sync-id: 29f44ce5b01db3ec353522af051b6a50924614a2
280 lines
8.9 KiB
JavaScript
280 lines
8.9 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule ReactNativeMount
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
var RCTUIManager = require('NativeModules').UIManager;
|
|
|
|
var ReactElement = require('ReactElement');
|
|
var ReactNativeTagHandles = require('ReactNativeTagHandles');
|
|
var ReactNativeViewPool = require('ReactNativeViewPool');
|
|
var ReactPerf = require('ReactPerf');
|
|
var ReactReconciler = require('ReactReconciler');
|
|
var ReactUpdateQueue = require('ReactUpdateQueue');
|
|
var ReactUpdates = require('ReactUpdates');
|
|
|
|
var emptyObject = require('emptyObject');
|
|
var instantiateReactComponent = require('instantiateReactComponent');
|
|
var shouldUpdateReactComponent = require('shouldUpdateReactComponent');
|
|
|
|
function instanceNumberToChildRootID(rootNodeID, instanceNumber) {
|
|
return rootNodeID + '[' + instanceNumber + ']';
|
|
}
|
|
|
|
/**
|
|
* Temporary (?) hack so that we can store all top-level pending updates on
|
|
* composites instead of having to worry about different types of components
|
|
* here.
|
|
*/
|
|
var TopLevelWrapper = function() {};
|
|
TopLevelWrapper.prototype.isReactComponent = {};
|
|
if (__DEV__) {
|
|
TopLevelWrapper.displayName = 'TopLevelWrapper';
|
|
}
|
|
TopLevelWrapper.prototype.render = function() {
|
|
// this.props is actually a ReactElement
|
|
return this.props;
|
|
};
|
|
|
|
/**
|
|
* Mounts this component and inserts it into the DOM.
|
|
*
|
|
* @param {ReactComponent} componentInstance The instance to mount.
|
|
* @param {number} rootID ID of the root node.
|
|
* @param {number} container container element to mount into.
|
|
* @param {ReactReconcileTransaction} transaction
|
|
*/
|
|
function mountComponentIntoNode(
|
|
componentInstance,
|
|
rootID,
|
|
container,
|
|
transaction) {
|
|
var markup = ReactReconciler.mountComponent(
|
|
componentInstance, rootID, transaction, emptyObject
|
|
);
|
|
componentInstance._renderedComponent._topLevelWrapper = componentInstance;
|
|
ReactNativeMount._mountImageIntoNode(markup, container);
|
|
}
|
|
|
|
/**
|
|
* Batched mount.
|
|
*
|
|
* @param {ReactComponent} componentInstance The instance to mount.
|
|
* @param {number} rootID ID of the root node.
|
|
* @param {number} container container element to mount into.
|
|
*/
|
|
function batchedMountComponentIntoNode(
|
|
componentInstance,
|
|
rootID,
|
|
container) {
|
|
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled();
|
|
transaction.perform(
|
|
mountComponentIntoNode,
|
|
null,
|
|
componentInstance,
|
|
rootID,
|
|
container,
|
|
transaction
|
|
);
|
|
ReactUpdates.ReactReconcileTransaction.release(transaction);
|
|
}
|
|
|
|
/**
|
|
* As soon as `ReactMount` is refactored to not rely on the DOM, we can share
|
|
* code between the two. For now, we'll hard code the ID logic.
|
|
*/
|
|
var ReactNativeMount = {
|
|
instanceCount: 0,
|
|
|
|
_instancesByContainerID: {},
|
|
|
|
// these two functions are needed by React Devtools
|
|
findNodeHandle: require('findNodeHandle'),
|
|
nativeTagToRootNodeID: function (nativeTag: number): string {
|
|
return ReactNativeTagHandles.tagToRootNodeID[nativeTag];
|
|
},
|
|
|
|
/**
|
|
* @param {ReactComponent} instance Instance to render.
|
|
* @param {containerTag} containerView Handle to native view tag
|
|
*/
|
|
renderComponent: function(
|
|
nextElement: ReactElement,
|
|
containerTag: number,
|
|
callback?: ?(() => void)
|
|
): ?ReactComponent {
|
|
var nextWrappedElement = new ReactElement(
|
|
TopLevelWrapper,
|
|
null,
|
|
null,
|
|
null,
|
|
null,
|
|
null,
|
|
nextElement
|
|
);
|
|
|
|
var topRootNodeID = ReactNativeTagHandles.tagToRootNodeID[containerTag];
|
|
if (topRootNodeID) {
|
|
var prevComponent = ReactNativeMount._instancesByContainerID[topRootNodeID];
|
|
if (prevComponent) {
|
|
var prevWrappedElement = prevComponent._currentElement;
|
|
var prevElement = prevWrappedElement.props;
|
|
if (shouldUpdateReactComponent(prevElement, nextElement)) {
|
|
ReactUpdateQueue.enqueueElementInternal(prevComponent, nextWrappedElement);
|
|
if (callback) {
|
|
ReactUpdateQueue.enqueueCallbackInternal(prevComponent, callback);
|
|
}
|
|
return prevComponent;
|
|
} else {
|
|
ReactNativeMount.unmountComponentAtNode(containerTag);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!ReactNativeTagHandles.reactTagIsNativeTopRootID(containerTag)) {
|
|
console.error('You cannot render into anything but a top root');
|
|
return;
|
|
}
|
|
|
|
var topRootNodeID = ReactNativeTagHandles.allocateRootNodeIDForTag(containerTag);
|
|
ReactNativeTagHandles.associateRootNodeIDWithMountedNodeHandle(
|
|
topRootNodeID,
|
|
containerTag
|
|
);
|
|
|
|
var instance = instantiateReactComponent(nextWrappedElement);
|
|
ReactNativeMount._instancesByContainerID[topRootNodeID] = instance;
|
|
|
|
var childRootNodeID = instanceNumberToChildRootID(
|
|
topRootNodeID,
|
|
ReactNativeMount.instanceCount++
|
|
);
|
|
|
|
// The initial render is synchronous but any updates that happen during
|
|
// rendering, in componentWillMount or componentDidMount, will be batched
|
|
// according to the current batching strategy.
|
|
|
|
ReactUpdates.batchedUpdates(
|
|
batchedMountComponentIntoNode,
|
|
instance,
|
|
childRootNodeID,
|
|
topRootNodeID
|
|
);
|
|
var component = instance.getPublicInstance();
|
|
if (callback) {
|
|
callback.call(component);
|
|
}
|
|
return component;
|
|
},
|
|
|
|
/**
|
|
* @param {View} view View tree image.
|
|
* @param {number} containerViewID View to insert sub-view into.
|
|
*/
|
|
_mountImageIntoNode: ReactPerf.measure(
|
|
// FIXME(frantic): #4441289 Hack to avoid modifying react-tools
|
|
'ReactComponentBrowserEnvironment',
|
|
'mountImageIntoNode',
|
|
function(mountImage, containerID) {
|
|
// Since we now know that the `mountImage` has been mounted, we can
|
|
// mark it as such.
|
|
ReactNativeTagHandles.associateRootNodeIDWithMountedNodeHandle(
|
|
mountImage.rootNodeID,
|
|
mountImage.tag
|
|
);
|
|
var addChildTags = [mountImage.tag];
|
|
var addAtIndices = [0];
|
|
RCTUIManager.manageChildren(
|
|
ReactNativeTagHandles.mostRecentMountedNodeHandleForRootNodeID(containerID),
|
|
null, // moveFromIndices
|
|
null, // moveToIndices
|
|
addChildTags,
|
|
addAtIndices,
|
|
null // removeAtIndices
|
|
);
|
|
}
|
|
),
|
|
|
|
/**
|
|
* Standard unmounting of the component that is rendered into `containerID`,
|
|
* but will also execute a command to remove the actual container view
|
|
* itself. This is useful when a client is cleaning up a React tree, and also
|
|
* knows that the container will no longer be needed. When executing
|
|
* asynchronously, it's easier to just have this method be the one that calls
|
|
* for removal of the view.
|
|
*/
|
|
unmountComponentAtNodeAndRemoveContainer: function(
|
|
containerTag: number
|
|
) {
|
|
ReactNativeMount.unmountComponentAtNode(containerTag);
|
|
// call back into native to remove all of the subviews from this container
|
|
RCTUIManager.removeRootView(containerTag);
|
|
ReactNativeViewPool.clearPoolForRootView(containerTag);
|
|
},
|
|
|
|
/**
|
|
* Unmount component at container ID by iterating through each child component
|
|
* that has been rendered and unmounting it. There should just be one child
|
|
* component at this time.
|
|
*/
|
|
unmountComponentAtNode: function(containerTag: number): boolean {
|
|
if (!ReactNativeTagHandles.reactTagIsNativeTopRootID(containerTag)) {
|
|
console.error('You cannot render into anything but a top root');
|
|
return false;
|
|
}
|
|
|
|
var containerID = ReactNativeTagHandles.tagToRootNodeID[containerTag];
|
|
var instance = ReactNativeMount._instancesByContainerID[containerID];
|
|
if (!instance) {
|
|
return false;
|
|
}
|
|
ReactNativeMount.unmountComponentFromNode(instance, containerID);
|
|
delete ReactNativeMount._instancesByContainerID[containerID];
|
|
return true;
|
|
},
|
|
|
|
/**
|
|
* Unmounts a component and sends messages back to iOS to remove its subviews.
|
|
*
|
|
* @param {ReactComponent} instance React component instance.
|
|
* @param {string} containerID ID of container we're removing from.
|
|
* @final
|
|
* @internal
|
|
* @see {ReactNativeMount.unmountComponentAtNode}
|
|
*/
|
|
unmountComponentFromNode: function(
|
|
instance: ReactComponent,
|
|
containerID: string
|
|
) {
|
|
// Call back into native to remove all of the subviews from this container
|
|
ReactReconciler.unmountComponent(instance);
|
|
var containerTag =
|
|
ReactNativeTagHandles.mostRecentMountedNodeHandleForRootNodeID(containerID);
|
|
RCTUIManager.removeSubviewsFromContainerWithID(containerTag);
|
|
},
|
|
|
|
getNode: function(rootNodeID: string): number {
|
|
return ReactNativeTagHandles.rootNodeIDToTag[rootNodeID];
|
|
},
|
|
|
|
getID: function(nativeTag: number): string {
|
|
return ReactNativeTagHandles.tagToRootNodeID[nativeTag];
|
|
}
|
|
};
|
|
|
|
ReactNativeMount.renderComponent = ReactPerf.measure(
|
|
'ReactMount',
|
|
'_renderNewRootComponent',
|
|
ReactNativeMount.renderComponent
|
|
);
|
|
|
|
module.exports = ReactNativeMount;
|