mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Merge pull request #7034 from sebmarkbage/newreconciler
[Fiber] Host Container Fiber and Priority Levels
This commit is contained in:
commit
291f8e30a9
12 files changed
+1023
-201
No files matched your search
@@ -19,6 +19,8 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
import type { Fiber } from 'ReactFiber';
|
||||
|
||||
var ReactFiberReconciler = require('ReactFiberReconciler');
|
||||
|
||||
var scheduledHighPriCallback = null;
|
||||
@@ -38,12 +40,16 @@ var NoopRenderer = ReactFiberReconciler({
|
||||
|
||||
});
|
||||
|
||||
var root = null;
|
||||
|
||||
var ReactNoop = {
|
||||
|
||||
render(element : ReactElement<any>) {
|
||||
|
||||
NoopRenderer.mountNewRoot(element);
|
||||
|
||||
if (!root) {
|
||||
root = NoopRenderer.mountContainer(element, null);
|
||||
} else {
|
||||
NoopRenderer.updateContainer(element, root);
|
||||
}
|
||||
},
|
||||
|
||||
flushHighPri() {
|
||||
@@ -79,6 +85,24 @@ var ReactNoop = {
|
||||
ReactNoop.flushLowPri();
|
||||
},
|
||||
|
||||
// Logs the current state of the tree.
|
||||
dumpTree() {
|
||||
if (!root) {
|
||||
console.log('Nothing rendered yet.');
|
||||
return;
|
||||
}
|
||||
function logFiber(fiber : Fiber, depth) {
|
||||
console.log(' '.repeat(depth) + '- ' + (fiber.type ? fiber.type.name || fiber.type : '[root]'), '[' + fiber.pendingWorkPriority + (fiber.pendingProps ? '*' : '') + ']');
|
||||
if (fiber.child) {
|
||||
logFiber(fiber.child, depth + 1);
|
||||
}
|
||||
if (fiber.sibling) {
|
||||
logFiber(fiber.sibling, depth);
|
||||
}
|
||||
}
|
||||
logFiber((root.stateNode : any).current, 0);
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
module.exports = ReactNoop;
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
import type { ReactCoroutine, ReactYield } from 'ReactCoroutine';
|
||||
import type { Fiber } from 'ReactFiber';
|
||||
import type { PriorityLevel } from 'ReactPriorityLevel';
|
||||
|
||||
import type { ReactNodeList } from 'ReactTypes';
|
||||
|
||||
@@ -28,7 +29,7 @@ var {
|
||||
var ReactFiber = require('ReactFiber');
|
||||
var ReactReifiedYield = require('ReactReifiedYield');
|
||||
|
||||
function createSubsequentChild(parent : Fiber, existingChild : ?Fiber, previousSibling : Fiber, newChildren) : Fiber {
|
||||
function createSubsequentChild(returnFiber : Fiber, existingChild : ?Fiber, previousSibling : Fiber, newChildren, priority : PriorityLevel) : Fiber {
|
||||
if (typeof newChildren !== 'object' || newChildren === null) {
|
||||
return previousSibling;
|
||||
}
|
||||
@@ -41,34 +42,35 @@ function createSubsequentChild(parent : Fiber, existingChild : ?Fiber, previousS
|
||||
element.key === existingChild.key) {
|
||||
// TODO: This is not sufficient since previous siblings could be new.
|
||||
// Will fix reconciliation properly later.
|
||||
const clone = ReactFiber.cloneFiber(existingChild);
|
||||
clone.input = element.props;
|
||||
const clone = ReactFiber.cloneFiber(existingChild, priority);
|
||||
clone.pendingProps = element.props;
|
||||
clone.child = existingChild.child;
|
||||
clone.sibling = null;
|
||||
clone.return = returnFiber;
|
||||
previousSibling.sibling = clone;
|
||||
return clone;
|
||||
}
|
||||
const child = ReactFiber.createFiberFromElement(element);
|
||||
const child = ReactFiber.createFiberFromElement(element, priority);
|
||||
previousSibling.sibling = child;
|
||||
child.parent = parent;
|
||||
child.return = returnFiber;
|
||||
return child;
|
||||
}
|
||||
|
||||
case REACT_COROUTINE_TYPE: {
|
||||
const coroutine = (newChildren : ReactCoroutine);
|
||||
const child = ReactFiber.createFiberFromCoroutine(coroutine);
|
||||
const child = ReactFiber.createFiberFromCoroutine(coroutine, priority);
|
||||
previousSibling.sibling = child;
|
||||
child.parent = parent;
|
||||
child.return = returnFiber;
|
||||
return child;
|
||||
}
|
||||
|
||||
case REACT_YIELD_TYPE: {
|
||||
const yieldNode = (newChildren : ReactYield);
|
||||
const reifiedYield = ReactReifiedYield.createReifiedYield(yieldNode);
|
||||
const child = ReactFiber.createFiberFromYield(yieldNode);
|
||||
const child = ReactFiber.createFiberFromYield(yieldNode, priority);
|
||||
child.output = reifiedYield;
|
||||
previousSibling.sibling = child;
|
||||
child.parent = parent;
|
||||
child.return = returnFiber;
|
||||
return child;
|
||||
}
|
||||
}
|
||||
@@ -77,7 +79,7 @@ function createSubsequentChild(parent : Fiber, existingChild : ?Fiber, previousS
|
||||
let prev : Fiber = previousSibling;
|
||||
let existing : ?Fiber = existingChild;
|
||||
for (var i = 0; i < newChildren.length; i++) {
|
||||
prev = createSubsequentChild(parent, existing, prev, newChildren[i]);
|
||||
prev = createSubsequentChild(returnFiber, existing, prev, newChildren[i], priority);
|
||||
if (prev && existing) {
|
||||
// TODO: This is not correct because there could've been more
|
||||
// than one sibling consumed but I don't want to return a tuple.
|
||||
@@ -91,7 +93,7 @@ function createSubsequentChild(parent : Fiber, existingChild : ?Fiber, previousS
|
||||
}
|
||||
}
|
||||
|
||||
function createFirstChild(parent, existingChild, newChildren) {
|
||||
function createFirstChild(returnFiber, existingChild, newChildren, priority) {
|
||||
if (typeof newChildren !== 'object' || newChildren === null) {
|
||||
return null;
|
||||
}
|
||||
@@ -103,21 +105,22 @@ function createFirstChild(parent, existingChild, newChildren) {
|
||||
element.type === existingChild.type &&
|
||||
element.key === existingChild.key) {
|
||||
// Get the clone of the existing fiber.
|
||||
const clone = ReactFiber.cloneFiber(existingChild);
|
||||
clone.input = element.props;
|
||||
const clone = ReactFiber.cloneFiber(existingChild, priority);
|
||||
clone.pendingProps = element.props;
|
||||
clone.child = existingChild.child;
|
||||
clone.sibling = null;
|
||||
clone.return = returnFiber;
|
||||
return clone;
|
||||
}
|
||||
const child = ReactFiber.createFiberFromElement(element);
|
||||
child.parent = parent;
|
||||
const child = ReactFiber.createFiberFromElement(element, priority);
|
||||
child.return = returnFiber;
|
||||
return child;
|
||||
}
|
||||
|
||||
case REACT_COROUTINE_TYPE: {
|
||||
const coroutine = (newChildren : ReactCoroutine);
|
||||
const child = ReactFiber.createFiberFromCoroutine(coroutine);
|
||||
child.parent = parent;
|
||||
const child = ReactFiber.createFiberFromCoroutine(coroutine, priority);
|
||||
child.return = returnFiber;
|
||||
return child;
|
||||
}
|
||||
|
||||
@@ -127,9 +130,9 @@ function createFirstChild(parent, existingChild, newChildren) {
|
||||
// the fragment.
|
||||
const yieldNode = (newChildren : ReactYield);
|
||||
const reifiedYield = ReactReifiedYield.createReifiedYield(yieldNode);
|
||||
const child = ReactFiber.createFiberFromYield(yieldNode);
|
||||
const child = ReactFiber.createFiberFromYield(yieldNode, priority);
|
||||
child.output = reifiedYield;
|
||||
child.parent = parent;
|
||||
child.return = returnFiber;
|
||||
return child;
|
||||
}
|
||||
}
|
||||
@@ -140,10 +143,10 @@ function createFirstChild(parent, existingChild, newChildren) {
|
||||
var existing : ?Fiber = existingChild;
|
||||
for (var i = 0; i < newChildren.length; i++) {
|
||||
if (prev == null) {
|
||||
prev = createFirstChild(parent, existing, newChildren[i]);
|
||||
prev = createFirstChild(returnFiber, existing, newChildren[i], priority);
|
||||
first = prev;
|
||||
} else {
|
||||
prev = createSubsequentChild(parent, existing, prev, newChildren[i]);
|
||||
prev = createSubsequentChild(returnFiber, existing, prev, newChildren[i], priority);
|
||||
}
|
||||
if (prev && existing) {
|
||||
// TODO: This is not correct because there could've been more
|
||||
@@ -158,6 +161,8 @@ function createFirstChild(parent, existingChild, newChildren) {
|
||||
}
|
||||
}
|
||||
|
||||
exports.reconcileChildFibers = function(parent : Fiber, currentFirstChild : ?Fiber, newChildren : ReactNodeList) : ?Fiber {
|
||||
return createFirstChild(parent, currentFirstChild, newChildren);
|
||||
// TODO: This API won't work because we'll need to transfer the side-effects of
|
||||
// unmounting children to the returnFiber.
|
||||
exports.reconcileChildFibers = function(returnFiber : Fiber, currentFirstChild : ?Fiber, newChildren : ReactNodeList, priority : PriorityLevel) : ?Fiber {
|
||||
return createFirstChild(returnFiber, currentFirstChild, newChildren, priority);
|
||||
};
|
||||
@@ -12,18 +12,25 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var ReactTypesOfWork = require('ReactTypesOfWork');
|
||||
import type { ReactCoroutine, ReactYield } from 'ReactCoroutine';
|
||||
import type { TypeOfWork } from 'ReactTypeOfWork';
|
||||
import type { PriorityLevel } from 'ReactPriorityLevel';
|
||||
|
||||
var ReactTypeOfWork = require('ReactTypeOfWork');
|
||||
var {
|
||||
IndeterminateComponent,
|
||||
ClassComponent,
|
||||
HostContainer,
|
||||
HostComponent,
|
||||
CoroutineComponent,
|
||||
YieldComponent,
|
||||
} = ReactTypesOfWork;
|
||||
} = ReactTypeOfWork;
|
||||
|
||||
var ReactElement = require('ReactElement');
|
||||
|
||||
import type { ReactCoroutine, ReactYield } from 'ReactCoroutine';
|
||||
var {
|
||||
NoWork,
|
||||
} = require('ReactPriorityLevel');
|
||||
|
||||
// An Instance is shared between all versions of a component. We can easily
|
||||
// break this out into a separate object to avoid copying so much to the
|
||||
@@ -32,12 +39,7 @@ import type { ReactCoroutine, ReactYield } from 'ReactCoroutine';
|
||||
type Instance = {
|
||||
|
||||
// Tag identifying the type of fiber.
|
||||
tag: number,
|
||||
|
||||
// The parent Fiber used to create this one. The type is constrained to the
|
||||
// Instance part of the Fiber since it is not safe to traverse the tree from
|
||||
// the instance.
|
||||
parent: ?Instance, // Consider a regenerated temporary parent stack instead.
|
||||
tag: TypeOfWork,
|
||||
|
||||
// Unique identifier of this child.
|
||||
key: null | string,
|
||||
@@ -48,12 +50,22 @@ type Instance = {
|
||||
// The local state associated with this fiber.
|
||||
stateNode: ?Object,
|
||||
|
||||
// Conceptual aliases
|
||||
// parent : Instance -> return The parent happens to be the same as the
|
||||
// return fiber since we've merged the fiber and instance.
|
||||
|
||||
};
|
||||
|
||||
// A Fiber is work on a Component that needs to be done or was done. There can
|
||||
// be more than one per component.
|
||||
export type Fiber = Instance & {
|
||||
|
||||
// The Fiber to return to after finishing processing this one.
|
||||
// This is effectively the parent, but there can be multiple parents (two)
|
||||
// so this is only the parent of the thing we're currently processing.
|
||||
// It is conceptually the same as the return address of a stack frame.
|
||||
return: ?Fiber,
|
||||
|
||||
// Singly Linked List Tree Structure.
|
||||
child: ?Fiber,
|
||||
sibling: ?Fiber,
|
||||
@@ -63,32 +75,41 @@ export type Fiber = Instance & {
|
||||
ref: null | (handle : ?Object) => void,
|
||||
|
||||
// Input is the data coming into process this fiber. Arguments. Props.
|
||||
input: any, // This type will be more specific once we overload the tag.
|
||||
// TODO: I think that there is a way to merge input and memoizedInput somehow.
|
||||
memoizedInput: any, // The input used to create the output.
|
||||
pendingProps: any, // This type will be more specific once we overload the tag.
|
||||
// TODO: I think that there is a way to merge pendingProps and memoizedProps.
|
||||
memoizedProps: any, // The props used to create the output.
|
||||
// Output is the return value of this fiber, or a linked list of return values
|
||||
// if this returns multiple values. Such as a fragment.
|
||||
output: any, // This type will be more specific once we overload the tag.
|
||||
|
||||
// This will be used to quickly determine if a subtree has no pending changes.
|
||||
hasPendingChanges: bool,
|
||||
pendingWorkPriority: PriorityLevel,
|
||||
|
||||
// This is a pooled version of a Fiber. Every fiber that gets updated will
|
||||
// eventually have a pair. There are cases when we can clean up pairs to save
|
||||
// memory if we need to.
|
||||
alternate: ?Fiber,
|
||||
|
||||
// Keeps track if we've completed this node or if we're currently in the
|
||||
// middle of processing it. We really should know this based on pendingProps
|
||||
// or something else. We could also reuse the tag for this purpose. However,
|
||||
// I'm not really sure so I'll use a flag for now.
|
||||
// TODO: Find another way to infer this flag.
|
||||
hasWorkInProgress: bool,
|
||||
|
||||
// Conceptual aliases
|
||||
// workInProgress : Fiber -> alternate The alternate used for reuse happens
|
||||
// to be the same as work in progress.
|
||||
|
||||
};
|
||||
|
||||
var createFiber = function(tag : number, key : null | string) : Fiber {
|
||||
var createFiber = function(tag : TypeOfWork, key : null | string) : Fiber {
|
||||
return {
|
||||
|
||||
// Instance
|
||||
|
||||
tag: tag,
|
||||
|
||||
parent: null,
|
||||
|
||||
key: key,
|
||||
|
||||
type: null,
|
||||
@@ -97,16 +118,20 @@ var createFiber = function(tag : number, key : null | string) : Fiber {
|
||||
|
||||
// Fiber
|
||||
|
||||
return: null,
|
||||
|
||||
child: null,
|
||||
sibling: null,
|
||||
|
||||
ref: null,
|
||||
|
||||
input: null,
|
||||
memoizedInput: null,
|
||||
pendingProps: null,
|
||||
memoizedProps: null,
|
||||
output: null,
|
||||
|
||||
hasPendingChanges: true,
|
||||
pendingWorkPriority: NoWork,
|
||||
|
||||
hasWorkInProgress: false,
|
||||
|
||||
alternate: null,
|
||||
|
||||
@@ -118,35 +143,46 @@ function shouldConstruct(Component) {
|
||||
}
|
||||
|
||||
// This is used to create an alternate fiber to do work on.
|
||||
exports.cloneFiber = function(fiber : Fiber) : Fiber {
|
||||
exports.cloneFiber = function(fiber : Fiber, priorityLevel : PriorityLevel) : Fiber {
|
||||
// We use a double buffering pooling technique because we know that we'll only
|
||||
// ever need at most two versions of a tree. We pool the "other" unused node
|
||||
// that we're free to reuse. This is lazily created to avoid allocating extra
|
||||
// objects for things that are never updated. It also allow us to reclaim the
|
||||
// extra memory if needed.
|
||||
if (fiber.alternate) {
|
||||
return fiber.alternate;
|
||||
let alt = fiber.alternate;
|
||||
if (alt) {
|
||||
alt.stateNode = fiber.stateNode;
|
||||
alt.child = fiber.child;
|
||||
alt.sibling = fiber.sibling;
|
||||
alt.ref = alt.ref;
|
||||
alt.pendingProps = fiber.pendingProps;
|
||||
alt.pendingWorkPriority = priorityLevel;
|
||||
return alt;
|
||||
}
|
||||
|
||||
// This should not have an alternate already
|
||||
var alt = createFiber(fiber.tag, fiber.key);
|
||||
|
||||
if (fiber.parent) {
|
||||
// TODO: This assumes the parent's alternate is already created.
|
||||
// Stop using the alternates of parents once we have a parent stack.
|
||||
// $FlowFixMe: This downcast is not safe. It is intentionally an error.
|
||||
alt.parent = fiber.parent.alternate;
|
||||
}
|
||||
|
||||
alt = createFiber(fiber.tag, fiber.key);
|
||||
alt.type = fiber.type;
|
||||
alt.stateNode = fiber.stateNode;
|
||||
alt.child = fiber.child;
|
||||
alt.sibling = fiber.sibling;
|
||||
alt.ref = alt.ref;
|
||||
alt.pendingWorkPriority = priorityLevel;
|
||||
|
||||
alt.alternate = fiber;
|
||||
fiber.alternate = alt;
|
||||
return alt;
|
||||
};
|
||||
|
||||
exports.createFiberFromElement = function(element : ReactElement) {
|
||||
exports.createHostContainerFiber = function() {
|
||||
const fiber = createFiber(HostContainer, null);
|
||||
return fiber;
|
||||
};
|
||||
|
||||
exports.createFiberFromElement = function(element : ReactElement, priorityLevel : PriorityLevel) {
|
||||
const fiber = exports.createFiberFromElementType(element.type, element.key);
|
||||
fiber.input = element.props;
|
||||
fiber.pendingProps = element.props;
|
||||
fiber.pendingWorkPriority = priorityLevel;
|
||||
return fiber;
|
||||
};
|
||||
|
||||
@@ -169,14 +205,16 @@ exports.createFiberFromElementType = function(type : mixed, key : null | string)
|
||||
return fiber;
|
||||
};
|
||||
|
||||
exports.createFiberFromCoroutine = function(coroutine : ReactCoroutine) {
|
||||
exports.createFiberFromCoroutine = function(coroutine : ReactCoroutine, priorityLevel : PriorityLevel) {
|
||||
const fiber = createFiber(CoroutineComponent, coroutine.key);
|
||||
fiber.type = coroutine.handler;
|
||||
fiber.input = coroutine;
|
||||
fiber.pendingProps = coroutine;
|
||||
fiber.pendingWorkPriority = priorityLevel;
|
||||
return fiber;
|
||||
};
|
||||
|
||||
exports.createFiberFromYield = function(yieldNode : ReactYield) {
|
||||
exports.createFiberFromYield = function(yieldNode : ReactYield, priorityLevel : PriorityLevel) {
|
||||
const fiber = createFiber(YieldComponent, yieldNode.key);
|
||||
fiber.pendingProps = {};
|
||||
return fiber;
|
||||
};
|
||||
@@ -16,63 +16,123 @@ import type { ReactCoroutine } from 'ReactCoroutine';
|
||||
import type { Fiber } from 'ReactFiber';
|
||||
|
||||
var ReactChildFiber = require('ReactChildFiber');
|
||||
var ReactTypesOfWork = require('ReactTypesOfWork');
|
||||
var ReactTypeOfWork = require('ReactTypeOfWork');
|
||||
var {
|
||||
IndeterminateComponent,
|
||||
FunctionalComponent,
|
||||
ClassComponent,
|
||||
HostContainer,
|
||||
HostComponent,
|
||||
CoroutineComponent,
|
||||
CoroutineHandlerPhase,
|
||||
YieldComponent,
|
||||
} = ReactTypesOfWork;
|
||||
} = ReactTypeOfWork;
|
||||
var {
|
||||
NoWork,
|
||||
OffscreenPriority,
|
||||
} = require('ReactPriorityLevel');
|
||||
var { findNextUnitOfWorkAtPriority } = require('ReactFiberPendingWork');
|
||||
|
||||
function reconcileChildren(current, workInProgress, nextChildren) {
|
||||
const priority = workInProgress.pendingWorkPriority;
|
||||
workInProgress.child = ReactChildFiber.reconcileChildFibers(
|
||||
workInProgress,
|
||||
current ? current.child : null,
|
||||
nextChildren
|
||||
nextChildren,
|
||||
priority
|
||||
);
|
||||
}
|
||||
|
||||
function updateFunctionalComponent(current, workInProgress) {
|
||||
var fn = workInProgress.type;
|
||||
var props = workInProgress.input;
|
||||
var props = workInProgress.pendingProps;
|
||||
console.log('update fn:', fn.name);
|
||||
var nextChildren = fn(props);
|
||||
reconcileChildren(current, workInProgress, nextChildren);
|
||||
workInProgress.pendingWorkPriority = NoWork;
|
||||
}
|
||||
|
||||
function updateHostComponent(current, workInProgress) {
|
||||
console.log('host component', workInProgress.type, typeof workInProgress.input.children === 'string' ? workInProgress.input.children : '');
|
||||
console.log('host component', workInProgress.type, typeof workInProgress.pendingProps.children === 'string' ? workInProgress.pendingProps.children : '');
|
||||
|
||||
var nextChildren = workInProgress.input.children;
|
||||
reconcileChildren(current, workInProgress, nextChildren);
|
||||
var nextChildren = workInProgress.pendingProps.children;
|
||||
|
||||
let priority = workInProgress.pendingWorkPriority;
|
||||
if (workInProgress.pendingProps.hidden && priority !== OffscreenPriority) {
|
||||
// If this host component is hidden, we can reconcile its children at
|
||||
// the lowest priority and bail out from this particular pass. Unless, we're
|
||||
// currently reconciling the lowest priority.
|
||||
workInProgress.child = ReactChildFiber.reconcileChildFibers(
|
||||
workInProgress,
|
||||
current ? current.child : null,
|
||||
nextChildren,
|
||||
OffscreenPriority
|
||||
);
|
||||
workInProgress.pendingWorkPriority = OffscreenPriority;
|
||||
return null;
|
||||
} else {
|
||||
workInProgress.child = ReactChildFiber.reconcileChildFibers(
|
||||
workInProgress,
|
||||
current ? current.child : null,
|
||||
nextChildren,
|
||||
priority
|
||||
);
|
||||
workInProgress.pendingWorkPriority = NoWork;
|
||||
return workInProgress.child;
|
||||
}
|
||||
}
|
||||
|
||||
function mountIndeterminateComponent(current, workInProgress) {
|
||||
var fn = workInProgress.type;
|
||||
var props = workInProgress.input;
|
||||
var props = workInProgress.pendingProps;
|
||||
var value = fn(props);
|
||||
if (typeof value === 'object' && value && typeof value.render === 'function') {
|
||||
console.log('performed work on class:', fn.name);
|
||||
// Proceed under the assumption that this is a class instance
|
||||
workInProgress.tag = ClassComponent;
|
||||
if (workInProgress.alternate) {
|
||||
workInProgress.alternate.tag = ClassComponent;
|
||||
}
|
||||
} else {
|
||||
console.log('performed work on fn:', fn.name);
|
||||
// Proceed under the assumption that this is a functional component
|
||||
workInProgress.tag = FunctionalComponent;
|
||||
if (workInProgress.alternate) {
|
||||
workInProgress.alternate.tag = FunctionalComponent;
|
||||
}
|
||||
}
|
||||
reconcileChildren(current, workInProgress, value);
|
||||
workInProgress.pendingWorkPriority = NoWork;
|
||||
}
|
||||
|
||||
function updateCoroutineComponent(current, workInProgress) {
|
||||
var coroutine = (workInProgress.input : ?ReactCoroutine);
|
||||
var coroutine = (workInProgress.pendingProps : ?ReactCoroutine);
|
||||
if (!coroutine) {
|
||||
throw new Error('Should be resolved by now');
|
||||
}
|
||||
console.log('begin coroutine', workInProgress.type.name);
|
||||
reconcileChildren(current, workInProgress, coroutine.children);
|
||||
workInProgress.pendingWorkPriority = NoWork;
|
||||
}
|
||||
|
||||
function reuseChildren(returnFiber : Fiber, firstChild : Fiber) {
|
||||
// TODO: None of this should be necessary if structured better.
|
||||
// The returnFiber pointer only needs to be updated when we walk into this child
|
||||
// which we don't do right now. If the pending work priority indicated only
|
||||
// if a child has work rather than if the node has work, then we would know
|
||||
// by a single lookup on workInProgress rather than having to go through
|
||||
// each child.
|
||||
let child = firstChild;
|
||||
do {
|
||||
// Update the returnFiber of the child to the newest fiber.
|
||||
child.return = returnFiber;
|
||||
// Retain the priority if there's any work left to do in the children.
|
||||
if (child.pendingWorkPriority !== NoWork &&
|
||||
(returnFiber.pendingWorkPriority === NoWork ||
|
||||
returnFiber.pendingWorkPriority > child.pendingWorkPriority)) {
|
||||
returnFiber.pendingWorkPriority = child.pendingWorkPriority;
|
||||
}
|
||||
} while (child = child.sibling);
|
||||
}
|
||||
|
||||
function beginWork(current : ?Fiber, workInProgress : Fiber) : ?Fiber {
|
||||
@@ -80,20 +140,51 @@ function beginWork(current : ?Fiber, workInProgress : Fiber) : ?Fiber {
|
||||
// Ideally nothing should rely on this, but relying on it here
|
||||
// means that we don't need an additional field on the work in
|
||||
// progress.
|
||||
if (current && workInProgress.input === current.memoizedInput) {
|
||||
if (current && workInProgress.pendingProps === current.memoizedProps) {
|
||||
// The most likely scenario is that the previous copy of the tree contains
|
||||
// the same input as the new one. In that case, we can just copy the output
|
||||
// the same props as the new one. In that case, we can just copy the output
|
||||
// and children from that node.
|
||||
workInProgress.memoizedProps = workInProgress.pendingProps;
|
||||
workInProgress.output = current.output;
|
||||
workInProgress.child = current.child;
|
||||
const priorityLevel = workInProgress.pendingWorkPriority;
|
||||
workInProgress.pendingProps = null;
|
||||
workInProgress.pendingWorkPriority = NoWork;
|
||||
workInProgress.stateNode = current.stateNode;
|
||||
if (current.child) {
|
||||
// If we bail out but still has work with the current priority in this
|
||||
// subtree, we need to go find it right now. If we don't, we won't flush
|
||||
// it until the next tick.
|
||||
workInProgress.child = current.child;
|
||||
reuseChildren(workInProgress, workInProgress.child);
|
||||
if (workInProgress.pendingWorkPriority <= priorityLevel) {
|
||||
// TODO: This passes the current node and reads the priority level and
|
||||
// pending props from that. We want it to read our priority level and
|
||||
// pending props from the work in progress. Needs restructuring.
|
||||
return findNextUnitOfWorkAtPriority(workInProgress.alternate, priorityLevel);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
workInProgress.child = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!workInProgress.hasWorkInProgress &&
|
||||
workInProgress.pendingProps === workInProgress.memoizedProps &&
|
||||
workInProgress.pendingWorkPriority === NoWork) {
|
||||
// If we started this work before, and finished it, or if we're in a
|
||||
// ping-pong update scenario, this version could already be what we're
|
||||
// looking for. In that case, we should be able to just bail out.
|
||||
workInProgress.pendingProps = null;
|
||||
// TODO: We should be able to bail out if there is remaining work at a lower
|
||||
// priority too. However, I don't know if that is safe or even better since
|
||||
// the other tree could've potentially finished that work.
|
||||
return null;
|
||||
}
|
||||
if (workInProgress.input === workInProgress.memoizedInput) {
|
||||
// In a ping-pong scenario, this version could actually contain the
|
||||
// old input. In that case, we can just bail out.
|
||||
return null;
|
||||
}
|
||||
|
||||
workInProgress.hasWorkInProgress = true;
|
||||
|
||||
switch (workInProgress.tag) {
|
||||
case IndeterminateComponent:
|
||||
mountIndeterminateComponent(current, workInProgress);
|
||||
@@ -102,11 +193,22 @@ function beginWork(current : ?Fiber, workInProgress : Fiber) : ?Fiber {
|
||||
updateFunctionalComponent(current, workInProgress);
|
||||
return workInProgress.child;
|
||||
case ClassComponent:
|
||||
console.log('class component', workInProgress.input.type.name);
|
||||
console.log('class component', workInProgress.pendingProps.type.name);
|
||||
return workInProgress.child;
|
||||
case HostContainer:
|
||||
reconcileChildren(current, workInProgress, workInProgress.pendingProps);
|
||||
// A yield component is just a placeholder, we can just run through the
|
||||
// next one immediately.
|
||||
workInProgress.pendingWorkPriority = NoWork;
|
||||
if (workInProgress.child) {
|
||||
return beginWork(
|
||||
workInProgress.child.alternate,
|
||||
workInProgress.child
|
||||
);
|
||||
}
|
||||
return null;
|
||||
case HostComponent:
|
||||
updateHostComponent(current, workInProgress);
|
||||
return workInProgress.child;
|
||||
return updateHostComponent(current, workInProgress);
|
||||
case CoroutineHandlerPhase:
|
||||
// This is a restart. Reset the tag to the initial phase.
|
||||
workInProgress.tag = CoroutineComponent;
|
||||
@@ -125,6 +227,7 @@ function beginWork(current : ?Fiber, workInProgress : Fiber) : ?Fiber {
|
||||
case YieldComponent:
|
||||
// A yield component is just a placeholder, we can just run through the
|
||||
// next one immediately.
|
||||
workInProgress.pendingWorkPriority = NoWork;
|
||||
if (workInProgress.sibling) {
|
||||
return beginWork(
|
||||
workInProgress.sibling.alternate,
|
||||
|
||||
@@ -18,23 +18,24 @@ import type { Fiber } from 'ReactFiber';
|
||||
import type { ReifiedYield } from 'ReactReifiedYield';
|
||||
|
||||
var ReactChildFiber = require('ReactChildFiber');
|
||||
var ReactTypesOfWork = require('ReactTypesOfWork');
|
||||
var ReactTypeOfWork = require('ReactTypeOfWork');
|
||||
var {
|
||||
IndeterminateComponent,
|
||||
FunctionalComponent,
|
||||
ClassComponent,
|
||||
HostContainer,
|
||||
HostComponent,
|
||||
CoroutineComponent,
|
||||
CoroutineHandlerPhase,
|
||||
YieldComponent,
|
||||
} = ReactTypesOfWork;
|
||||
} = ReactTypeOfWork;
|
||||
|
||||
function transferOutput(child : ?Fiber, parent : Fiber) {
|
||||
function transferOutput(child : ?Fiber, returnFiber : Fiber) {
|
||||
// If we have a single result, we just pass that through as the output to
|
||||
// avoid unnecessary traversal. When we have multiple output, we just pass
|
||||
// the linked list of fibers that has the individual output values.
|
||||
parent.output = (child && !child.sibling) ? child.output : child;
|
||||
parent.memoizedInput = parent.input;
|
||||
returnFiber.output = (child && !child.sibling) ? child.output : child;
|
||||
returnFiber.memoizedProps = returnFiber.pendingProps;
|
||||
}
|
||||
|
||||
function recursivelyFillYields(yields, output : ?Fiber | ?ReifiedYield) {
|
||||
@@ -55,7 +56,7 @@ function recursivelyFillYields(yields, output : ?Fiber | ?ReifiedYield) {
|
||||
}
|
||||
|
||||
function moveCoroutineToHandlerPhase(current : ?Fiber, workInProgress : Fiber) {
|
||||
var coroutine = (workInProgress.input : ?ReactCoroutine);
|
||||
var coroutine = (workInProgress.pendingProps : ?ReactCoroutine);
|
||||
if (!coroutine) {
|
||||
throw new Error('Should be resolved by now');
|
||||
}
|
||||
@@ -82,10 +83,13 @@ function moveCoroutineToHandlerPhase(current : ?Fiber, workInProgress : Fiber) {
|
||||
var nextChildren = fn(props, yields);
|
||||
|
||||
var currentFirstChild = current ? current.stateNode : null;
|
||||
// Inherit the priority of the returnFiber.
|
||||
const priority = workInProgress.pendingWorkPriority;
|
||||
workInProgress.stateNode = ReactChildFiber.reconcileChildFibers(
|
||||
workInProgress,
|
||||
currentFirstChild,
|
||||
nextChildren
|
||||
nextChildren,
|
||||
priority
|
||||
);
|
||||
return workInProgress.stateNode;
|
||||
}
|
||||
@@ -95,25 +99,28 @@ exports.completeWork = function(current : ?Fiber, workInProgress : Fiber) : ?Fib
|
||||
case FunctionalComponent:
|
||||
console.log('/functional component', workInProgress.type.name);
|
||||
transferOutput(workInProgress.child, workInProgress);
|
||||
break;
|
||||
return null;
|
||||
case ClassComponent:
|
||||
console.log('/class component', workInProgress.type.name);
|
||||
transferOutput(workInProgress.child, workInProgress);
|
||||
break;
|
||||
return null;
|
||||
case HostContainer:
|
||||
return null;
|
||||
case HostComponent:
|
||||
transferOutput(workInProgress.child, workInProgress);
|
||||
console.log('/host component', workInProgress.type);
|
||||
break;
|
||||
return null;
|
||||
case CoroutineComponent:
|
||||
console.log('/coroutine component', workInProgress.input.handler.name);
|
||||
console.log('/coroutine component', workInProgress.pendingProps.handler.name);
|
||||
return moveCoroutineToHandlerPhase(current, workInProgress);
|
||||
case CoroutineHandlerPhase:
|
||||
transferOutput(workInProgress.stateNode, workInProgress);
|
||||
// Reset the tag to now be a first phase coroutine.
|
||||
workInProgress.tag = CoroutineComponent;
|
||||
break;
|
||||
return null;
|
||||
case YieldComponent:
|
||||
// Does nothing.
|
||||
break;
|
||||
return null;
|
||||
|
||||
// Error cases
|
||||
case IndeterminateComponent:
|
||||
@@ -121,5 +128,4 @@ exports.completeWork = function(current : ?Fiber, workInProgress : Fiber) : ?Fib
|
||||
default:
|
||||
throw new Error('Unknown unit of work tag');
|
||||
}
|
||||
return null;
|
||||
};
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* 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 ReactFiberPendingWork
|
||||
* @flow
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type { Fiber } from 'ReactFiber';
|
||||
import type { PriorityLevel } from 'ReactPriorityLevel';
|
||||
|
||||
var ReactFiber = require('ReactFiber');
|
||||
|
||||
var {
|
||||
NoWork,
|
||||
} = require('ReactPriorityLevel');
|
||||
|
||||
function cloneSiblings(current : Fiber, workInProgress : Fiber, returnFiber : Fiber) {
|
||||
while (current.sibling) {
|
||||
current = current.sibling;
|
||||
workInProgress = workInProgress.sibling = ReactFiber.cloneFiber(
|
||||
current,
|
||||
current.pendingWorkPriority
|
||||
);
|
||||
workInProgress.return = returnFiber;
|
||||
}
|
||||
workInProgress.sibling = null;
|
||||
}
|
||||
|
||||
exports.findNextUnitOfWorkAtPriority = function(currentRoot : Fiber, priorityLevel : PriorityLevel) : ?Fiber {
|
||||
let current = currentRoot;
|
||||
while (current) {
|
||||
if (current.pendingWorkPriority !== NoWork &&
|
||||
current.pendingWorkPriority <= priorityLevel) {
|
||||
// This node has work to do that fits our priority level criteria.
|
||||
if (current.pendingProps !== null) {
|
||||
// We found some work to do. We need to return the "work in progress"
|
||||
// of this node which will be the alternate.
|
||||
const workInProgress = current.alternate;
|
||||
if (!workInProgress) {
|
||||
throw new Error('Should have wip now');
|
||||
}
|
||||
workInProgress.pendingProps = current.pendingProps;
|
||||
return workInProgress;
|
||||
}
|
||||
// If we have a child let's see if any of our children has work to do.
|
||||
// Only bother doing this at all if the current priority level matches
|
||||
// because it is the highest priority for the whole subtree.
|
||||
// TODO: Coroutines can have work in their stateNode which is another
|
||||
// type of child that needs to be searched for work.
|
||||
if (current.child) {
|
||||
// Ensure we have a work in progress copy to backtrack through.
|
||||
let currentChild = current.child;
|
||||
let workInProgress = current.alternate;
|
||||
if (!workInProgress) {
|
||||
throw new Error('Should have wip now');
|
||||
}
|
||||
workInProgress.pendingWorkPriority = current.pendingWorkPriority;
|
||||
workInProgress.child = ReactFiber.cloneFiber(currentChild, NoWork);
|
||||
workInProgress.child.return = workInProgress;
|
||||
cloneSiblings(currentChild, workInProgress.child, workInProgress);
|
||||
current = current.child;
|
||||
continue;
|
||||
}
|
||||
// If we match the priority but has no child and no work to do,
|
||||
// then we can safely reset the flag.
|
||||
current.pendingWorkPriority = NoWork;
|
||||
}
|
||||
if (current === currentRoot) {
|
||||
return null;
|
||||
}
|
||||
while (!current.sibling) {
|
||||
current = current.return;
|
||||
if (!current) {
|
||||
return null;
|
||||
}
|
||||
if (current.pendingWorkPriority <= priorityLevel) {
|
||||
// If this subtree had work left to do, we would have returned it by
|
||||
// now. This could happen if a child with pending work gets cleaned up
|
||||
// but we don't clear the flag then. It is safe to reset it now.
|
||||
current.pendingWorkPriority = NoWork;
|
||||
}
|
||||
}
|
||||
current = current.sibling;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
@@ -13,9 +13,14 @@
|
||||
'use strict';
|
||||
|
||||
import type { Fiber } from 'ReactFiber';
|
||||
var ReactFiber = require('ReactFiber');
|
||||
var { beginWork } = require('ReactFiberBeginWork');
|
||||
var { completeWork } = require('ReactFiberCompleteWork');
|
||||
import type { FiberRoot } from 'ReactFiberRoot';
|
||||
|
||||
var { createFiberRoot } = require('ReactFiberRoot');
|
||||
var ReactFiberScheduler = require('ReactFiberScheduler');
|
||||
|
||||
var {
|
||||
LowPriority,
|
||||
} = require('ReactPriorityLevel');
|
||||
|
||||
type ReactHostElement<T, P> = {
|
||||
type: T,
|
||||
@@ -26,8 +31,6 @@ type Deadline = {
|
||||
timeRemaining : () => number
|
||||
};
|
||||
|
||||
var timeHeuristicForUnitOfWork = 1;
|
||||
|
||||
export type HostConfig<T, P, I> = {
|
||||
|
||||
createHostInstance(element : ReactHostElement<T, P>) : I,
|
||||
@@ -36,112 +39,62 @@ export type HostConfig<T, P, I> = {
|
||||
|
||||
};
|
||||
|
||||
type OpaqueID = {};
|
||||
type OpaqueNode = Fiber;
|
||||
|
||||
export type Reconciler = {
|
||||
mountNewRoot(element : ReactElement<any>) : OpaqueID;
|
||||
mountContainer(element : ReactElement<any>, containerInfo : ?Object) : OpaqueNode,
|
||||
updateContainer(element : ReactElement<any>, container : OpaqueNode) : void,
|
||||
unmountContainer(container : OpaqueNode) : void,
|
||||
|
||||
// Used to extract the return value from the initial render. Legacy API.
|
||||
getPublicRootInstance(container : OpaqueNode) : ?Object,
|
||||
};
|
||||
|
||||
module.exports = function<T, P, I>(config : HostConfig<T, P, I>) : Reconciler {
|
||||
|
||||
// const scheduleHighPriCallback = config.scheduleHighPriCallback;
|
||||
const scheduleLowPriCallback = config.scheduleLowPriCallback;
|
||||
|
||||
let nextUnitOfWork : ?Fiber = null;
|
||||
|
||||
function completeUnitOfWork(workInProgress : Fiber) : ?Fiber {
|
||||
while (true) {
|
||||
// The current, flushed, state of this fiber is the alternate.
|
||||
// Ideally nothing should rely on this, but relying on it here
|
||||
// means that we don't need an additional field on the work in
|
||||
// progress.
|
||||
const current = workInProgress.alternate;
|
||||
const next = completeWork(current, workInProgress);
|
||||
if (next) {
|
||||
// If completing this work spawned new work, do that next.
|
||||
return next;
|
||||
} else if (workInProgress.sibling) {
|
||||
// If there is more work to do in this parent, do that next.
|
||||
return workInProgress.sibling;
|
||||
} else if (workInProgress.parent) {
|
||||
// If there's no more work in this parent. Complete the parent.
|
||||
// TODO: Stop using the parent for this purpose. I think this will break
|
||||
// down in edge cases because when nodes are reused during bailouts, we
|
||||
// don't know which of two parents was used. Instead we should maintain
|
||||
// a temporary manual stack.
|
||||
// $FlowFixMe: This downcast is not safe. It is intentionally an error.
|
||||
workInProgress = workInProgress.parent;
|
||||
} else {
|
||||
// If we're at the root, there's no more work to do.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function performUnitOfWork(workInProgress : Fiber) : ?Fiber {
|
||||
// The current, flushed, state of this fiber is the alternate.
|
||||
// Ideally nothing should rely on this, but relying on it here
|
||||
// means that we don't need an additional field on the work in
|
||||
// progress.
|
||||
const current = workInProgress.alternate;
|
||||
const next = beginWork(current, workInProgress);
|
||||
if (next) {
|
||||
// If this spawns new work, do that next.
|
||||
return next;
|
||||
} else {
|
||||
// Otherwise, complete the current work.
|
||||
return completeUnitOfWork(workInProgress);
|
||||
}
|
||||
}
|
||||
|
||||
function performLowPriWork(deadline : Deadline) {
|
||||
while (nextUnitOfWork) {
|
||||
if (deadline.timeRemaining() > timeHeuristicForUnitOfWork) {
|
||||
nextUnitOfWork = performUnitOfWork(nextUnitOfWork);
|
||||
} else {
|
||||
scheduleLowPriCallback(performLowPriWork);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function ensureLowPriIsScheduled() {
|
||||
if (nextUnitOfWork) {
|
||||
return;
|
||||
}
|
||||
scheduleLowPriCallback(performLowPriWork);
|
||||
}
|
||||
|
||||
/*
|
||||
function performHighPriWork() {
|
||||
// There is no such thing as high pri work yet.
|
||||
}
|
||||
|
||||
function ensureHighPriIsScheduled() {
|
||||
scheduleHighPriCallback(performHighPriWork);
|
||||
}
|
||||
*/
|
||||
|
||||
let rootFiber : ?Fiber = null;
|
||||
var { scheduleLowPriWork } = ReactFiberScheduler(config);
|
||||
|
||||
return {
|
||||
|
||||
mountNewRoot(element : ReactElement<any>) : OpaqueID {
|
||||
mountContainer(element : ReactElement<any>, containerInfo : ?Object) : OpaqueNode {
|
||||
const root = createFiberRoot(containerInfo);
|
||||
const container = root.current;
|
||||
// TODO: Use pending work/state instead of props.
|
||||
container.pendingProps = element;
|
||||
container.pendingWorkPriority = LowPriority;
|
||||
|
||||
ensureLowPriIsScheduled();
|
||||
scheduleLowPriWork(root);
|
||||
|
||||
// TODO: Unify this with ReactChildFiber. We can't now because the parent
|
||||
// is passed. Should be doable though. Might require a wrapper don't know.
|
||||
if (rootFiber && rootFiber.type === element.type && rootFiber.key === element.key) {
|
||||
nextUnitOfWork = ReactFiber.cloneFiber(rootFiber);
|
||||
nextUnitOfWork.input = element.props;
|
||||
return {};
|
||||
}
|
||||
// It may seem strange that we don't return the root here, but that will
|
||||
// allow us to have containers that are in the middle of the tree instead
|
||||
// of being roots.
|
||||
return container;
|
||||
},
|
||||
|
||||
nextUnitOfWork = rootFiber = ReactFiber.createFiberFromElement(element);
|
||||
updateContainer(element : ReactElement<any>, container : OpaqueNode) : void {
|
||||
// TODO: If this is a nested container, this won't be the root.
|
||||
const root : FiberRoot = (container.stateNode : any);
|
||||
// TODO: Use pending work/state instead of props.
|
||||
root.current.pendingProps = element;
|
||||
root.current.pendingWorkPriority = LowPriority;
|
||||
|
||||
return {};
|
||||
scheduleLowPriWork(root);
|
||||
},
|
||||
|
||||
unmountContainer(container : OpaqueNode) : void {
|
||||
// TODO: If this is a nested container, this won't be the root.
|
||||
const root : FiberRoot = (container.stateNode : any);
|
||||
// TODO: Use pending work/state instead of props.
|
||||
root.current.pendingProps = [];
|
||||
root.current.pendingWorkPriority = LowPriority;
|
||||
|
||||
scheduleLowPriWork(root);
|
||||
},
|
||||
|
||||
getPublicRootInstance(container : OpaqueNode) : ?Object {
|
||||
return null;
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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 ReactFiberRoot
|
||||
* @flow
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type { Fiber } from 'ReactFiber';
|
||||
|
||||
const { createHostContainerFiber } = require('ReactFiber');
|
||||
|
||||
export type FiberRoot = {
|
||||
// Any additional information from the host associated with this root.
|
||||
containerInfo: ?Object,
|
||||
// The currently active root fiber. This is the mutable root of the tree.
|
||||
current: Fiber,
|
||||
// Determines if this root has already been added to the schedule for work.
|
||||
isScheduled: bool,
|
||||
// The work schedule is a linked list.
|
||||
nextScheduledRoot: ?FiberRoot,
|
||||
};
|
||||
|
||||
exports.createFiberRoot = function(containerInfo : ?Object) : FiberRoot {
|
||||
// Cyclic construction. This cheats the type system right now because
|
||||
// stateNode is any.
|
||||
const uninitializedFiber = createHostContainerFiber();
|
||||
const root = {
|
||||
current: uninitializedFiber,
|
||||
containerInfo: containerInfo,
|
||||
isScheduled: false,
|
||||
nextScheduledRoot: null,
|
||||
};
|
||||
uninitializedFiber.stateNode = root;
|
||||
return root;
|
||||
};
|
||||
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* 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 ReactFiberScheduler
|
||||
* @flow
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type { Fiber } from 'ReactFiber';
|
||||
import type { FiberRoot } from 'ReactFiberRoot';
|
||||
import type { HostConfig } from 'ReactFiberReconciler';
|
||||
|
||||
var { cloneFiber } = require('ReactFiber');
|
||||
var { beginWork } = require('ReactFiberBeginWork');
|
||||
var { completeWork } = require('ReactFiberCompleteWork');
|
||||
var { findNextUnitOfWorkAtPriority } = require('ReactFiberPendingWork');
|
||||
|
||||
var {
|
||||
NoWork,
|
||||
HighPriority,
|
||||
LowPriority,
|
||||
OffscreenPriority,
|
||||
} = require('ReactPriorityLevel');
|
||||
|
||||
var timeHeuristicForUnitOfWork = 1;
|
||||
|
||||
module.exports = function<T, P, I>(config : HostConfig<T, P, I>) {
|
||||
|
||||
// const scheduleHighPriCallback = config.scheduleHighPriCallback;
|
||||
const scheduleLowPriCallback = config.scheduleLowPriCallback;
|
||||
|
||||
// The next work in progress fiber that we're currently working on.
|
||||
let nextUnitOfWork : ?Fiber = null;
|
||||
|
||||
// Linked list of roots with scheduled work on them.
|
||||
let nextScheduledRoot : ?FiberRoot = null;
|
||||
let lastScheduledRoot : ?FiberRoot = null;
|
||||
|
||||
function findNextUnitOfWork() {
|
||||
// Clear out roots with no more work on them.
|
||||
while (nextScheduledRoot && nextScheduledRoot.current.pendingWorkPriority === NoWork) {
|
||||
nextScheduledRoot.isScheduled = false;
|
||||
if (nextScheduledRoot === lastScheduledRoot) {
|
||||
nextScheduledRoot = null;
|
||||
lastScheduledRoot = null;
|
||||
return null;
|
||||
}
|
||||
nextScheduledRoot = nextScheduledRoot.nextScheduledRoot;
|
||||
}
|
||||
let root = nextScheduledRoot;
|
||||
while (root) {
|
||||
cloneFiber(root.current, root.current.pendingWorkPriority);
|
||||
// Find the highest possible priority work to do.
|
||||
// This loop is unrolled just to satisfy Flow's enum constraint.
|
||||
// We could make arbitrary many idle priority levels but having
|
||||
// too many just means flushing changes too often.
|
||||
let work = findNextUnitOfWorkAtPriority(root.current, HighPriority);
|
||||
if (work) {
|
||||
return work;
|
||||
}
|
||||
work = findNextUnitOfWorkAtPriority(root.current, LowPriority);
|
||||
if (work) {
|
||||
return work;
|
||||
}
|
||||
work = findNextUnitOfWorkAtPriority(root.current, OffscreenPriority);
|
||||
if (work) {
|
||||
return work;
|
||||
}
|
||||
// We didn't find anything to do in this root, so let's try the next one.
|
||||
root = root.nextScheduledRoot;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function completeUnitOfWork(workInProgress : Fiber) : ?Fiber {
|
||||
while (true) {
|
||||
// The current, flushed, state of this fiber is the alternate.
|
||||
// Ideally nothing should rely on this, but relying on it here
|
||||
// means that we don't need an additional field on the work in
|
||||
// progress.
|
||||
const current = workInProgress.alternate;
|
||||
const next = completeWork(current, workInProgress);
|
||||
|
||||
// The work is now done. We don't need this anymore. This flags
|
||||
// to the system not to redo any work here.
|
||||
workInProgress.pendingProps = null;
|
||||
if (workInProgress.pendingWorkPriority === NoWork) {
|
||||
workInProgress.hasWorkInProgress = false;
|
||||
}
|
||||
|
||||
const returnFiber = workInProgress.return;
|
||||
|
||||
// Ensure that remaining work priority bubbles up.
|
||||
if (returnFiber && workInProgress.pendingWorkPriority !== NoWork &&
|
||||
(returnFiber.pendingWorkPriority === NoWork ||
|
||||
returnFiber.pendingWorkPriority > workInProgress.pendingWorkPriority)) {
|
||||
returnFiber.pendingWorkPriority = workInProgress.pendingWorkPriority;
|
||||
}
|
||||
|
||||
if (next) {
|
||||
// If completing this work spawned new work, do that next.
|
||||
return next;
|
||||
} else if (workInProgress.sibling) {
|
||||
// If there is more work to do in this returnFiber, do that next.
|
||||
return workInProgress.sibling;
|
||||
} else if (returnFiber) {
|
||||
// If there's no more work in this returnFiber. Complete the returnFiber.
|
||||
workInProgress = returnFiber;
|
||||
} else {
|
||||
// If we're at the root, there's no more work to do. We can flush it.
|
||||
const root : FiberRoot = (workInProgress.stateNode : any);
|
||||
root.current = workInProgress;
|
||||
// TODO: We can be smarter here and only look for more work in the
|
||||
// "next" scheduled work since we've already scanned passed. That
|
||||
// also ensures that work scheduled during reconciliation gets deferred.
|
||||
// const hasMoreWork = workInProgress.pendingWorkPriority !== NoWork;
|
||||
console.log('----- COMPLETED with remaining work:', workInProgress.pendingWorkPriority);
|
||||
const nextWork = findNextUnitOfWork();
|
||||
// if (!nextWork && hasMoreWork) {
|
||||
// TODO: This can happen when some deep work completes and we don't
|
||||
// know if this was the last one. We should be able to keep track of
|
||||
// the highest priority still in the tree for one pass. But if we
|
||||
// terminate an update we don't know.
|
||||
// throw new Error('FiberRoots should not have flagged more work if there is none.');
|
||||
// }
|
||||
return nextWork;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function performUnitOfWork(workInProgress : Fiber) : ?Fiber {
|
||||
// Ignore work if there is nothing to do.
|
||||
if (workInProgress.pendingProps === null) {
|
||||
return completeUnitOfWork(workInProgress);
|
||||
}
|
||||
// The current, flushed, state of this fiber is the alternate.
|
||||
// Ideally nothing should rely on this, but relying on it here
|
||||
// means that we don't need an additional field on the work in
|
||||
// progress.
|
||||
const current = workInProgress.alternate;
|
||||
const next = beginWork(current, workInProgress);
|
||||
if (next) {
|
||||
// If this spawns new work, do that next.
|
||||
return next;
|
||||
} else {
|
||||
// Otherwise, complete the current work.
|
||||
return completeUnitOfWork(workInProgress);
|
||||
}
|
||||
}
|
||||
|
||||
function performLowPriWork(deadline) {
|
||||
if (!nextUnitOfWork) {
|
||||
nextUnitOfWork = findNextUnitOfWork();
|
||||
}
|
||||
while (nextUnitOfWork) {
|
||||
if (deadline.timeRemaining() > timeHeuristicForUnitOfWork) {
|
||||
nextUnitOfWork = performUnitOfWork(nextUnitOfWork);
|
||||
if (!nextUnitOfWork) {
|
||||
// Find more work. We might have time to complete some more.
|
||||
nextUnitOfWork = findNextUnitOfWork();
|
||||
}
|
||||
} else {
|
||||
scheduleLowPriCallback(performLowPriWork);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleLowPriWork(root : FiberRoot) {
|
||||
// We must reset the current unit of work pointer so that we restart the
|
||||
// search from the root during the next tick, in case there is now higher
|
||||
// priority work somewhere earlier than before.
|
||||
nextUnitOfWork = null;
|
||||
|
||||
if (root.isScheduled) {
|
||||
// If we're already scheduled, we can bail out.
|
||||
return;
|
||||
}
|
||||
root.isScheduled = true;
|
||||
if (lastScheduledRoot) {
|
||||
// Schedule ourselves to the end.
|
||||
lastScheduledRoot.nextScheduledRoot = root;
|
||||
lastScheduledRoot = root;
|
||||
} else {
|
||||
// We're the only work scheduled.
|
||||
nextScheduledRoot = root;
|
||||
lastScheduledRoot = root;
|
||||
scheduleLowPriCallback(performLowPriWork);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
function performHighPriWork() {
|
||||
// There is no such thing as high pri work yet.
|
||||
}
|
||||
|
||||
function ensureHighPriIsScheduled() {
|
||||
scheduleHighPriCallback(performHighPriWork);
|
||||
}
|
||||
*/
|
||||
|
||||
return {
|
||||
scheduleLowPriWork: scheduleLowPriWork,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* 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 ReactPriorityLevel
|
||||
* @flow
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
export type PriorityLevel = 0 | 1 | 2 | 3 | 4 | 5;
|
||||
|
||||
module.exports = {
|
||||
NoWork: 0, // No work is pending.
|
||||
SynchronousPriority: 1, // For controlled text inputs. Synchronous side-effects.
|
||||
AnimationPriority: 2, // Needs to complete before the next frame.
|
||||
HighPriority: 3, // Interaction that needs to complete pretty soon to feel responsive.
|
||||
LowPriority: 4, // Data fetching, or result from updating stores.
|
||||
OffscreenPriority: 5, // Won't be visible but do the work in case it becomes visible.
|
||||
};
|
||||
+9
-8
@@ -6,20 +6,21 @@
|
||||
* 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 ReactTypesOfWork
|
||||
* @providesModule ReactTypeOfWork
|
||||
* @flow
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var TypesOfWork = {
|
||||
export type TypeOfWork = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
|
||||
|
||||
module.exports = {
|
||||
IndeterminateComponent: 0, // Before we know whether it is functional or class
|
||||
FunctionalComponent: 1,
|
||||
ClassComponent: 2,
|
||||
HostComponent: 3,
|
||||
CoroutineComponent: 4,
|
||||
CoroutineHandlerPhase: 5,
|
||||
YieldComponent: 6,
|
||||
HostContainer: 3, // Root of a host tree. Could be nested inside another node.
|
||||
HostComponent: 4,
|
||||
CoroutineComponent: 5,
|
||||
CoroutineHandlerPhase: 6,
|
||||
YieldComponent: 7,
|
||||
};
|
||||
|
||||
module.exports = TypesOfWork;
|
||||
@@ -109,10 +109,332 @@ describe('ReactIncremental', function() {
|
||||
ReactNoop.render(<Foo text="bar" />);
|
||||
ReactNoop.flush();
|
||||
|
||||
// TODO: Test bail out of host components. This is currently unobservable.
|
||||
|
||||
// Since this is an update, it should bail out and reuse the work from
|
||||
// Header and Content.
|
||||
expect(ops).toEqual(['Foo', 'Content']);
|
||||
|
||||
});
|
||||
|
||||
it('can cancel partially rendered work and restart', function() {
|
||||
|
||||
var ops = [];
|
||||
|
||||
function Bar(props) {
|
||||
ops.push('Bar');
|
||||
return <div>{props.children}</div>;
|
||||
}
|
||||
|
||||
function Foo(props) {
|
||||
ops.push('Foo');
|
||||
return (
|
||||
<div>
|
||||
<Bar>{props.text}</Bar>
|
||||
<Bar>{props.text}</Bar>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Init
|
||||
ReactNoop.render(<Foo text="foo" />);
|
||||
ReactNoop.flush();
|
||||
|
||||
ops = [];
|
||||
|
||||
ReactNoop.render(<Foo text="bar" />);
|
||||
// Flush part of the work
|
||||
ReactNoop.flushLowPri(20);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// This will abort the previous work and restart
|
||||
ReactNoop.render(<Foo text="baz" />);
|
||||
|
||||
// Flush part of the new work
|
||||
ReactNoop.flushLowPri(20);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar']);
|
||||
|
||||
// Flush the rest of the work which now includes the low priority
|
||||
ReactNoop.flush(20);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar', 'Bar']);
|
||||
|
||||
});
|
||||
|
||||
it('can deprioritize unfinished work and resume it later', function() {
|
||||
|
||||
var ops = [];
|
||||
|
||||
function Bar(props) {
|
||||
ops.push('Bar');
|
||||
return <div>{props.children}</div>;
|
||||
}
|
||||
|
||||
function Middle(props) {
|
||||
ops.push('Middle');
|
||||
return <span>{props.children}</span>;
|
||||
}
|
||||
|
||||
function Foo(props) {
|
||||
ops.push('Foo');
|
||||
return (
|
||||
<div>
|
||||
<Bar>{props.text}</Bar>
|
||||
<section hidden={true}>
|
||||
<Middle>{props.text}</Middle>
|
||||
</section>
|
||||
<Bar>{props.text}</Bar>
|
||||
<footer hidden={true}>
|
||||
<Middle>Footer</Middle>
|
||||
</footer>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Init
|
||||
ReactNoop.render(<Foo text="foo" />);
|
||||
ReactNoop.flush();
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar', 'Bar', 'Middle', 'Middle']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// Render part of the work. This should be enough to flush everything except
|
||||
// the middle which has lower priority.
|
||||
ReactNoop.render(<Foo text="bar" />);
|
||||
ReactNoop.flushLowPri(40);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar', 'Bar']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// Flush only the remaining work
|
||||
ReactNoop.flush();
|
||||
|
||||
expect(ops).toEqual(['Middle', 'Middle']);
|
||||
|
||||
});
|
||||
|
||||
it('can resume work in a subtree even when a parent bails out', function() {
|
||||
|
||||
var ops = [];
|
||||
|
||||
function Bar(props) {
|
||||
ops.push('Bar');
|
||||
return <div>{props.children}</div>;
|
||||
}
|
||||
|
||||
function Tester() {
|
||||
// This component is just here to ensure that the bail out is
|
||||
// in fact in effect in the expected place for this test.
|
||||
ops.push('Tester');
|
||||
return <div />;
|
||||
}
|
||||
|
||||
function Middle(props) {
|
||||
ops.push('Middle');
|
||||
return <span>{props.children}</span>;
|
||||
}
|
||||
|
||||
var middleContent = (
|
||||
<aaa>
|
||||
<Tester />
|
||||
<bbb hidden={true}>
|
||||
<ccc>
|
||||
<Middle>Hi</Middle>
|
||||
</ccc>
|
||||
</bbb>
|
||||
</aaa>
|
||||
);
|
||||
|
||||
function Foo(props) {
|
||||
ops.push('Foo');
|
||||
return (
|
||||
<div>
|
||||
<Bar>{props.text}</Bar>
|
||||
{middleContent}
|
||||
<Bar>{props.text}</Bar>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Init
|
||||
ReactNoop.render(<Foo text="foo" />);
|
||||
ReactNoop.flushLowPri(52);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar', 'Tester', 'Bar']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// We're now rendering an update that will bail out on updating middle.
|
||||
ReactNoop.render(<Foo text="bar" />);
|
||||
ReactNoop.flushLowPri(45);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar', 'Bar']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// Flush the rest to make sure that the bailout didn't block this work.
|
||||
ReactNoop.flush();
|
||||
expect(ops).toEqual(['Middle']);
|
||||
});
|
||||
|
||||
it('can resume work in a bailed subtree within one pass', function() {
|
||||
|
||||
var ops = [];
|
||||
|
||||
function Bar(props) {
|
||||
ops.push('Bar');
|
||||
return <div>{props.children}</div>;
|
||||
}
|
||||
|
||||
function Tester() {
|
||||
// This component is just here to ensure that the bail out is
|
||||
// in fact in effect in the expected place for this test.
|
||||
ops.push('Tester');
|
||||
return <div />;
|
||||
}
|
||||
|
||||
function Middle(props) {
|
||||
ops.push('Middle');
|
||||
return <span>{props.children}</span>;
|
||||
}
|
||||
|
||||
var middleContent = (
|
||||
<aaa>
|
||||
<Tester />
|
||||
<bbb hidden={true}>
|
||||
<ccc>
|
||||
<Middle>Hi</Middle>
|
||||
</ccc>
|
||||
</bbb>
|
||||
</aaa>
|
||||
);
|
||||
|
||||
function Foo(props) {
|
||||
ops.push('Foo');
|
||||
return (
|
||||
<div hidden={props.text === 'bar'}>
|
||||
<Bar>{props.text}</Bar>
|
||||
{middleContent}
|
||||
<Bar>{props.text}</Bar>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Init
|
||||
ReactNoop.render(<Foo text="foo" />);
|
||||
ReactNoop.flushLowPri(52);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar', 'Tester', 'Bar']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// Make a quick update which will create a low pri tree on top of the
|
||||
// already low pri tree.
|
||||
ReactNoop.render(<Foo text="bar" />);
|
||||
ReactNoop.flushLowPri(15);
|
||||
|
||||
expect(ops).toEqual(['Foo']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// At this point, middle will bail out but it has not yet fully rendered.
|
||||
// Since that is the same priority as its parent tree. This should render
|
||||
// as a single batch. Therefore, it is correct that Middle should be in the
|
||||
// middle. If it occurs after the two "Bar" components then it was flushed
|
||||
// after them which is not correct.
|
||||
ReactNoop.flush();
|
||||
expect(ops).toEqual(['Bar', 'Middle', 'Bar']);
|
||||
|
||||
});
|
||||
|
||||
it('can reuse work done after being preempted', function() {
|
||||
|
||||
var ops = [];
|
||||
|
||||
function Bar(props) {
|
||||
ops.push('Bar');
|
||||
return <div>{props.children}</div>;
|
||||
}
|
||||
|
||||
function Middle(props) {
|
||||
ops.push('Middle');
|
||||
return <span>{props.children}</span>;
|
||||
}
|
||||
|
||||
var middleContent = (
|
||||
<div>
|
||||
<Middle>Hello</Middle>
|
||||
<Bar>-</Bar>
|
||||
<Middle>World</Middle>
|
||||
</div>
|
||||
);
|
||||
|
||||
function Foo(props) {
|
||||
ops.push('Foo');
|
||||
return (
|
||||
<div>
|
||||
<Bar>{props.text}</Bar>
|
||||
<div hidden={true}>
|
||||
{
|
||||
props.step === 0 ?
|
||||
<div>
|
||||
<Middle>Hi</Middle>
|
||||
<Bar>{props.text}</Bar>
|
||||
<Middle>There</Middle>
|
||||
</div>
|
||||
: middleContent
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Start rendering an update
|
||||
|
||||
// Init
|
||||
ReactNoop.render(<Foo text="foo" step={0} />);
|
||||
ReactNoop.flush();
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar', 'Middle', 'Bar', 'Middle']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// Make a quick update which will schedule low priority work to
|
||||
// update the middle content.
|
||||
ReactNoop.render(<Foo text="bar" step={1} />);
|
||||
ReactNoop.flushLowPri(30);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// The middle content is now pending rendering...
|
||||
ReactNoop.flushLowPri(30);
|
||||
expect(ops).toEqual(['Middle', 'Bar']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// but we'll interupt it to render some higher priority work.
|
||||
// The middle content will bailout so it remains untouched.
|
||||
ReactNoop.render(<Foo text="foo" step={1} />);
|
||||
ReactNoop.flushLowPri(30);
|
||||
|
||||
expect(ops).toEqual(['Foo', 'Bar']);
|
||||
|
||||
ops = [];
|
||||
|
||||
// Since we did nothing to the middle subtree during the interuption,
|
||||
// we should be able to reuse the reconciliation work that we already did
|
||||
// without restarting.
|
||||
ReactNoop.flush();
|
||||
expect(ops).toEqual(['Middle']);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user