MCPcopy Create free account
hub / github.com/danielstocks/react-sortable / reconcileChildrenArray

Function reconcileChildrenArray

bundle.js:7129–7168  ·  view source on GitHub ↗
(returnFiber,currentFirstChild,newChildren,priority)

Source from the content-addressed store, hash-verified

7127var _matchedFiber3=existingChildren.get(newIdx)||null;return updateYield(returnFiber,_matchedFiber3,newChild,priority);}case REACT_PORTAL_TYPE:{var _matchedFiber4=existingChildren.get(newChild.key===null?newIdx:newChild.key)||null;return updatePortal(returnFiber,_matchedFiber4,newChild,priority);}}if(isArray(newChild)||getIteratorFn(newChild)){var _matchedFiber5=existingChildren.get(newIdx)||null;return updateFragment(returnFiber,_matchedFiber5,newChild,priority);}throwOnInvalidObjectType(returnFiber,newChild);}{if(typeof newChild==='function'){warnOnFunctionType();}}return null;}/**
7128 * Warns if there is a duplicate or missing key
7129 */function warnOnInvalidKey(child,knownKeys){{if((typeof child==='undefined'?'undefined':_typeof(child))!=='object'||child===null){return knownKeys;}switch(child.$$typeof){case REACT_ELEMENT_TYPE:case REACT_COROUTINE_TYPE:case REACT_PORTAL_TYPE:warnForMissingKey(child);var key=child.key;if(typeof key!=='string'){break;}if(knownKeys===null){knownKeys=new Set();knownKeys.add(key);break;}if(!knownKeys.has(key)){knownKeys.add(key);break;}warning$24(false,'Encountered two children with the same key, `%s`. '+'Keys should be unique so that components maintain their identity '+'across updates. Non-unique keys may cause children to be '+'duplicated and/or omitted — the behavior is unsupported and '+'could change in a future version.%s',key,getCurrentFiberStackAddendum$5());break;default:break;}}return knownKeys;}function reconcileChildrenArray(returnFiber,currentFirstChild,newChildren,priority){// This algorithm can't optimize by searching from boths ends since we
7130// don't have backpointers on fibers. I'm trying to see how far we can get
7131// with that model. If it ends up not being worth the tradeoffs, we can
7132// add it later.
7133// Even with a two ended optimization, we'd want to optimize for the case
7134// where there are few changes and brute force the comparison instead of
7135// going for the Map. It'd like to explore hitting that path first in
7136// forward-only mode and only go for the Map once we notice that we need
7137// lots of look ahead. This doesn't handle reversal as well as two ended
7138// search but that's unusual. Besides, for the two ended optimization to
7139// work on Iterables, we'd need to copy the whole set.
7140// In this first iteration, we'll just live with hitting the bad case
7141// (adding everything to a Map) in for every insert/move.
7142// If you change this code, also update reconcileChildrenIterator() which
7143// uses the same algorithm.
7144{// First, validate keys.
7145var knownKeys=null;for(var i=0;i<newChildren.length;i++){var child=newChildren[i];knownKeys=warnOnInvalidKey(child,knownKeys);}}var resultingFirstChild=null;var previousNewFiber=null;var oldFiber=currentFirstChild;var lastPlacedIndex=0;var newIdx=0;var nextOldFiber=null;for(;oldFiber!==null&&newIdx<newChildren.length;newIdx++){if(oldFiber.index>newIdx){nextOldFiber=oldFiber;oldFiber=null;}else{nextOldFiber=oldFiber.sibling;}var newFiber=updateSlot(returnFiber,oldFiber,newChildren[newIdx],priority);if(newFiber===null){// TODO: This breaks on empty slots like null children. That's
7146// unfortunate because it triggers the slow path all the time. We need
7147// a better way to communicate whether this was a miss or null,
7148// boolean, undefined, etc.
7149if(oldFiber===null){oldFiber=nextOldFiber;}break;}if(shouldTrackSideEffects){if(oldFiber&&newFiber.alternate===null){// We matched the slot, but we didn't reuse the existing fiber, so we
7150// need to delete the existing child.
7151deleteChild(returnFiber,oldFiber);}}lastPlacedIndex=placeChild(newFiber,lastPlacedIndex,newIdx);if(previousNewFiber===null){// TODO: Move out of the loop. This only happens for the first run.
7152resultingFirstChild=newFiber;}else{// TODO: Defer siblings if we're not at the right index for this slot.
7153// I.e. if we had null values before, then we want to defer this
7154// for each null value. However, we also don't want to call updateSlot
7155// with the previous one.
7156previousNewFiber.sibling=newFiber;}previousNewFiber=newFiber;oldFiber=nextOldFiber;}if(newIdx===newChildren.length){// We've reached the end of the new children. We can delete the rest.
7157deleteRemainingChildren(returnFiber,oldFiber);return resultingFirstChild;}if(oldFiber===null){// If we don't have any more existing children we can choose a fast path
7158// since the rest will all be insertions.
7159for(;newIdx<newChildren.length;newIdx++){var _newFiber=createChild(returnFiber,newChildren[newIdx],priority);if(!_newFiber){continue;}lastPlacedIndex=placeChild(_newFiber,lastPlacedIndex,newIdx);if(previousNewFiber===null){// TODO: Move out of the loop. This only happens for the first run.
7160resultingFirstChild=_newFiber;}else{previousNewFiber.sibling=_newFiber;}previousNewFiber=_newFiber;}return resultingFirstChild;}// Add all children to a key map for quick lookups.
7161var existingChildren=mapRemainingChildren(returnFiber,oldFiber);// Keep scanning and use the map to restore deleted items as moves.
7162for(;newIdx<newChildren.length;newIdx++){var _newFiber2=updateFromMap(existingChildren,returnFiber,newIdx,newChildren[newIdx],priority);if(_newFiber2){if(shouldTrackSideEffects){if(_newFiber2.alternate!==null){// The new fiber is a work in progress, but if there exists a
7163// current, that means that we reused the fiber. We need to delete
7164// it from the child list so that we don't add it to the deletion
7165// list.
7166existingChildren['delete'](_newFiber2.key===null?newIdx:_newFiber2.key);}}lastPlacedIndex=placeChild(_newFiber2,lastPlacedIndex,newIdx);if(previousNewFiber===null){resultingFirstChild=_newFiber2;}else{previousNewFiber.sibling=_newFiber2;}previousNewFiber=_newFiber2;}}if(shouldTrackSideEffects){// Any existing children that weren't consumed above were deleted. We need
7167// to add them to the deletion list.
7168existingChildren.forEach(function(child){return deleteChild(returnFiber,child);});}return resultingFirstChild;}function reconcileChildrenIterator(returnFiber,currentFirstChild,newChildrenIterable,priority){// This is the same implementation as reconcileChildrenArray(),
7169// but using the iterator instead.
7170var iteratorFn=getIteratorFn(newChildrenIterable);!(typeof iteratorFn==='function')?invariant(false,'An object is not an iterable. This error is likely caused by a bug in React. Please file an issue.'):void 0;{// Warn about using Maps as children
7171if(typeof newChildrenIterable.entries==='function'){var possibleMap=newChildrenIterable;if(possibleMap.entries===iteratorFn){warning$24(didWarnAboutMaps,'Using Maps as children is unsupported and will likely yield '+'unexpected results. Convert it to a sequence/iterable of keyed '+'ReactElements instead.%s',getCurrentFiberStackAddendum$5());didWarnAboutMaps=true;}}// First, validate keys.

Callers 1

reconcileChildFibersFunction · 0.85

Calls 8

warnOnInvalidKeyFunction · 0.85
updateSlotFunction · 0.85
deleteChildFunction · 0.85
placeChildFunction · 0.85
deleteRemainingChildrenFunction · 0.85
createChildFunction · 0.85
mapRemainingChildrenFunction · 0.85
updateFromMapFunction · 0.85

Tested by

no test coverage detected