forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build up a native component cache for event dispatching
Changes to event overloading structure
- Loading branch information
1 parent
75cec60
commit 3287d93
Showing
8 changed files
with
217 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/renderers/native/ReactNative/ReactNativeComponentTree.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright 2013-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 ReactNativeComponentTree | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var invariant = require('invariant'); | ||
|
||
var instanceCache = {}; | ||
|
||
/** | ||
* Drill down (through composites and empty components) until we get a native or | ||
* native text component. | ||
* | ||
* This is pretty polymorphic but unavoidable with the current structure we have | ||
* for `_renderedChildren`. | ||
*/ | ||
function getRenderedNativeOrTextFromComponent(component) { | ||
var rendered; | ||
while ((rendered = component._renderedComponent)) { | ||
component = rendered; | ||
} | ||
return component; | ||
} | ||
|
||
/** | ||
* Populate `_nativeNode` on the rendered native/text component with the given | ||
* DOM node. The passed `inst` can be a composite. | ||
*/ | ||
function precacheNode(inst, tag) { | ||
var nativeInst = getRenderedNativeOrTextFromComponent(inst); | ||
instanceCache[tag] = nativeInst; | ||
} | ||
|
||
function uncacheNode(inst) { | ||
var tag = inst._rootNodeID; | ||
if (tag) { | ||
delete instanceCache[tag]; | ||
} | ||
} | ||
|
||
function getInstanceFromTag(tag) { | ||
return instanceCache[tag] || null; | ||
} | ||
|
||
function getTagFromInstance(inst) { | ||
invariant(inst._rootNodeID, 'All native instances should have a tag.'); | ||
return inst._rootNodeID; | ||
} | ||
|
||
var ReactNativeComponentTree = { | ||
getClosestInstanceFromNode: getInstanceFromTag, | ||
getInstanceFromNode: getInstanceFromTag, | ||
getNodeFromInstance: getTagFromInstance, | ||
precacheNode: precacheNode, | ||
uncacheNode: uncacheNode, | ||
}; | ||
|
||
module.exports = ReactNativeComponentTree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/renderers/native/ReactNative/ReactNativeTreeTraversal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* Copyright 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 ReactNativeTreeTraversal | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// Same as ReactDOMTreeTraversal without the invariants. | ||
|
||
/** | ||
* Return the lowest common ancestor of A and B, or null if they are in | ||
* different trees. | ||
*/ | ||
function getLowestCommonAncestor(instA, instB) { | ||
var depthA = 0; | ||
for (var tempA = instA; tempA; tempA = tempA._nativeParent) { | ||
depthA++; | ||
} | ||
var depthB = 0; | ||
for (var tempB = instB; tempB; tempB = tempB._nativeParent) { | ||
depthB++; | ||
} | ||
|
||
// If A is deeper, crawl up. | ||
while (depthA - depthB > 0) { | ||
instA = instA._nativeParent; | ||
depthA--; | ||
} | ||
|
||
// If B is deeper, crawl up. | ||
while (depthB - depthA > 0) { | ||
instB = instB._nativeParent; | ||
depthB--; | ||
} | ||
|
||
// Walk in lockstep until we find a match. | ||
var depth = depthA; | ||
while (depth--) { | ||
if (instA === instB) { | ||
return instA; | ||
} | ||
instA = instA._nativeParent; | ||
instB = instB._nativeParent; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Return if A is an ancestor of B. | ||
*/ | ||
function isAncestor(instA, instB) { | ||
while (instB) { | ||
if (instB === instA) { | ||
return true; | ||
} | ||
instB = instB._nativeParent; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Return the parent instance of the passed-in instance. | ||
*/ | ||
function getParentInstance(inst) { | ||
return inst._nativeParent; | ||
} | ||
|
||
/** | ||
* Simulates the traversal of a two-phase, capture/bubble event dispatch. | ||
*/ | ||
function traverseTwoPhase(inst, fn, arg) { | ||
var path = []; | ||
while (inst) { | ||
path.push(inst); | ||
inst = inst._nativeParent; | ||
} | ||
var i; | ||
for (i = path.length; i-- > 0;) { | ||
fn(path[i], false, arg); | ||
} | ||
for (i = 0; i < path.length; i++) { | ||
fn(path[i], true, arg); | ||
} | ||
} | ||
|
||
/** | ||
* Traverses the ID hierarchy and invokes the supplied `cb` on any IDs that | ||
* should would receive a `mouseEnter` or `mouseLeave` event. | ||
* | ||
* Does not invoke the callback on the nearest common ancestor because nothing | ||
* "entered" or "left" that element. | ||
*/ | ||
function traverseEnterLeave(from, to, fn, argFrom, argTo) { | ||
var common = from && to ? getLowestCommonAncestor(from, to) : null; | ||
var pathFrom = []; | ||
while (from && from !== common) { | ||
pathFrom.push(from); | ||
from = from._nativeParent; | ||
} | ||
var pathTo = []; | ||
while (to && to !== common) { | ||
pathTo.push(to); | ||
to = to._nativeParent; | ||
} | ||
var i; | ||
for (i = 0; i < pathFrom.length; i++) { | ||
fn(pathFrom[i], true, argFrom); | ||
} | ||
for (i = pathTo.length; i-- > 0;) { | ||
fn(pathTo[i], false, argTo); | ||
} | ||
} | ||
|
||
module.exports = { | ||
isAncestor: isAncestor, | ||
getLowestCommonAncestor: getLowestCommonAncestor, | ||
getParentInstance: getParentInstance, | ||
traverseTwoPhase: traverseTwoPhase, | ||
traverseEnterLeave: traverseEnterLeave, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters