From aa14d89c7da229e1bf162414bff87fd020d7218b Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Thu, 26 May 2016 23:16:55 -0700 Subject: [PATCH 1/2] Transfer everything from Element onto the Fiber This has a few benefits: 1) This allows the element to always remain on the young generation. 2) The key can be accessed on the fiber which is easier to keep as the same class and is directly accessible in the child reconciliation of every object. 3) This conveniently means that we don't have to create a fake element for continuations which was really hacky. We can still do the quick bailout of rerendered things using the props object which is also unique. --- src/renderers/shared/fiber/ReactFiber.js | 41 ++++++++++++------- .../shared/fiber/ReactFiberBeginWork.js | 25 ++++------- .../shared/fiber/ReactFiberCompleteWork.js | 6 +-- .../shared/fiber/ReactReifiedYield.js | 7 ++-- .../shared/fiber/isomorphic/ReactCoroutine.js | 4 +- 5 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiber.js b/src/renderers/shared/fiber/ReactFiber.js index eb17068b6c..bc8c6d6180 100644 --- a/src/renderers/shared/fiber/ReactFiber.js +++ b/src/renderers/shared/fiber/ReactFiber.js @@ -35,7 +35,17 @@ export type Fiber = { child: ?Fiber, sibling: ?Fiber, - // Input is the data coming into process this fiber. Arguments. + // Unique identifier of this child. + key: ?string, + + // The function/class/module associated with this fiber. + type: any, + + // The ref last used to attach this node. + // I'll avoid adding an owner field for prod and model that as functions. + 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. // Output is the return value of this fiber, or a linked list of return values // if this returns multiple values. Such as a fragment. @@ -52,7 +62,7 @@ export type Fiber = { }; -var createFiber = function(tag : number) : Fiber { +var createFiber = function(tag : number, key : null | string) : Fiber { return { tag: tag, @@ -61,6 +71,10 @@ var createFiber = function(tag : number) : Fiber { child: null, sibling: null, + key: key, + type: null, + ref: null, + input: null, output: null, @@ -78,23 +92,21 @@ function shouldConstruct(Component) { } exports.createFiberFromElement = function(element : ReactElement) { - const fiber = exports.createFiberFromElementType(element.type); - if (typeof element.type === 'object') { - // Hacky McHack - element = ReactElement(fiber.input, null, element.ref, null, null, null, element.props); - } - fiber.input = element; + const fiber = exports.createFiberFromElementType(element.type, element.key); + fiber.input = element.props; return fiber; }; -exports.createFiberFromElementType = function(type : mixed) { +exports.createFiberFromElementType = function(type : mixed, key : null | string) { let fiber; if (typeof type === 'function') { fiber = shouldConstruct(type) ? - createFiber(ClassComponent) : - createFiber(IndeterminateComponent); + createFiber(ClassComponent, key) : + createFiber(IndeterminateComponent, key); + fiber.type = type; } else if (typeof type === 'string') { - fiber = createFiber(HostComponent); + fiber = createFiber(HostComponent, key); + fiber.type = type; } else if (typeof type === 'object' && type !== null) { // Currently assumed to be a continuation and therefore is a fiber already. fiber = type; @@ -105,12 +117,13 @@ exports.createFiberFromElementType = function(type : mixed) { }; exports.createFiberFromCoroutine = function(coroutine : ReactCoroutine) { - const fiber = createFiber(CoroutineComponent); + const fiber = createFiber(CoroutineComponent, coroutine.key); + fiber.type = coroutine.handler; fiber.input = coroutine; return fiber; }; exports.createFiberFromYield = function(yieldNode : ReactYield) { - const fiber = createFiber(YieldComponent); + const fiber = createFiber(YieldComponent, yieldNode.key); return fiber; }; diff --git a/src/renderers/shared/fiber/ReactFiberBeginWork.js b/src/renderers/shared/fiber/ReactFiberBeginWork.js index 19026242f9..abdb43cd96 100644 --- a/src/renderers/shared/fiber/ReactFiberBeginWork.js +++ b/src/renderers/shared/fiber/ReactFiberBeginWork.js @@ -26,18 +26,9 @@ var { YieldComponent, } = ReactTypesOfWork; -function getElement(unitOfWork) : ReactElement { - var element = unitOfWork.input; - if (!element) { - throw new Error('Should be resolved by now'); - } - return (element : ReactElement); -} - function updateFunctionalComponent(unitOfWork) { - var element = getElement(unitOfWork); - var fn = element.type; - var props = element.props; + var fn = unitOfWork.type; + var props = unitOfWork.input; console.log('perform work on:', fn.name); var nextChildren = fn(props); @@ -49,10 +40,9 @@ function updateFunctionalComponent(unitOfWork) { } function updateHostComponent(unitOfWork) { - var element = getElement(unitOfWork); - console.log('host component', element.type, typeof element.props.children === 'string' ? element.props.children : ''); + console.log('host component', unitOfWork.type, typeof unitOfWork.input.children === 'string' ? unitOfWork.input.children : ''); - var nextChildren = element.props.children; + var nextChildren = unitOfWork.input.children; unitOfWork.child = ReactChildFiber.reconcileChildFibers( unitOfWork, unitOfWork.child, @@ -61,9 +51,8 @@ function updateHostComponent(unitOfWork) { } function mountIndeterminateComponent(unitOfWork) { - var element = getElement(unitOfWork); - var fn = element.type; - var props = element.props; + var fn = unitOfWork.type; + var props = unitOfWork.input; var value = fn(props); if (typeof value === 'object' && value && typeof value.render === 'function') { console.log('performed work on class:', fn.name); @@ -86,7 +75,7 @@ function updateCoroutineComponent(unitOfWork) { if (!coroutine) { throw new Error('Should be resolved by now'); } - console.log('begin coroutine', coroutine.handler.name); + console.log('begin coroutine', unitOfWork.type.name); unitOfWork.child = ReactChildFiber.reconcileChildFibers( unitOfWork, unitOfWork.child, diff --git a/src/renderers/shared/fiber/ReactFiberCompleteWork.js b/src/renderers/shared/fiber/ReactFiberCompleteWork.js index 827075f004..311860aa4e 100644 --- a/src/renderers/shared/fiber/ReactFiberCompleteWork.js +++ b/src/renderers/shared/fiber/ReactFiberCompleteWork.js @@ -94,15 +94,15 @@ function handleCoroutine(unitOfWork : Fiber) { exports.completeWork = function(unitOfWork : Fiber) : ?Fiber { switch (unitOfWork.tag) { case FunctionalComponent: - console.log('/functional component', unitOfWork.input.type.name); + console.log('/functional component', unitOfWork.type.name); transferOutput(unitOfWork.child, unitOfWork); break; case ClassComponent: - console.log('/class component', unitOfWork.input.type.name); + console.log('/class component', unitOfWork.type.name); transferOutput(unitOfWork.child, unitOfWork); break; case HostComponent: - console.log('/host component', unitOfWork.input.type); + console.log('/host component', unitOfWork.type); break; case CoroutineComponent: console.log('/coroutine component', unitOfWork.input.handler.name); diff --git a/src/renderers/shared/fiber/ReactReifiedYield.js b/src/renderers/shared/fiber/ReactReifiedYield.js index a47887bc56..3f0e8a62e5 100644 --- a/src/renderers/shared/fiber/ReactReifiedYield.js +++ b/src/renderers/shared/fiber/ReactReifiedYield.js @@ -20,9 +20,10 @@ var ReactFiber = require('ReactFiber'); export type ReifiedYield = { continuation: Fiber, props: Object }; exports.createReifiedYield = function(yieldNode : ReactYield) : ReifiedYield { - var fiber = ReactFiber.createFiberFromElementType(yieldNode.continuation); - // Hacky way to store the continuation - fiber.input = yieldNode.continuation; + var fiber = ReactFiber.createFiberFromElementType( + yieldNode.continuation, + yieldNode.key + ); return { continuation: fiber, props: yieldNode.props, diff --git a/src/renderers/shared/fiber/isomorphic/ReactCoroutine.js b/src/renderers/shared/fiber/isomorphic/ReactCoroutine.js index 36728c4596..0218269a9e 100644 --- a/src/renderers/shared/fiber/isomorphic/ReactCoroutine.js +++ b/src/renderers/shared/fiber/isomorphic/ReactCoroutine.js @@ -29,7 +29,7 @@ type CoroutineHandler = (props: T, yields: Array) => ReactNodeL export type ReactCoroutine = { $$typeof: Symbol | number, - key: ?string, + key: null | string, children: any, // This should be a more specific CoroutineHandler handler: (props: any, yields: Array) => ReactNodeList, @@ -37,7 +37,7 @@ export type ReactCoroutine = { }; export type ReactYield = { $$typeof: Symbol | number, - key: ?string, + key: null | string, props: Object, continuation: mixed }; From 811084d74e7fefe09383e4ff39ed721b16d6a26f Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Tue, 31 May 2016 13:33:47 -0700 Subject: [PATCH 2/2] Use the tag instead of stage field for coroutine stages This gets rid of a field that we only need for coroutines. We might need this if we have multi phase handlers in the future but then maybe we can just use multiple tags. --- src/renderers/shared/fiber/ReactFiber.js | 5 -- .../shared/fiber/ReactFiberBeginWork.js | 7 ++- .../shared/fiber/ReactFiberCompleteWork.js | 62 +++++++++---------- .../shared/fiber/ReactTypesOfWork.js | 3 +- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiber.js b/src/renderers/shared/fiber/ReactFiber.js index bc8c6d6180..918ef87dd4 100644 --- a/src/renderers/shared/fiber/ReactFiber.js +++ b/src/renderers/shared/fiber/ReactFiber.js @@ -51,9 +51,6 @@ export type Fiber = { // if this returns multiple values. Such as a fragment. output: any, // This type will be more specific once we overload the tag. - // Used by multi-stage coroutines. - stage: number, // Consider reusing the tag field instead. - // This will be used to quickly determine if a subtree has no pending changes. hasPendingChanges: bool, @@ -78,8 +75,6 @@ var createFiber = function(tag : number, key : null | string) : Fiber { input: null, output: null, - stage: 0, - hasPendingChanges: true, stateNode: null, diff --git a/src/renderers/shared/fiber/ReactFiberBeginWork.js b/src/renderers/shared/fiber/ReactFiberBeginWork.js index abdb43cd96..91ee35a2f0 100644 --- a/src/renderers/shared/fiber/ReactFiberBeginWork.js +++ b/src/renderers/shared/fiber/ReactFiberBeginWork.js @@ -23,6 +23,7 @@ var { ClassComponent, HostComponent, CoroutineComponent, + CoroutineHandlerPhase, YieldComponent, } = ReactTypesOfWork; @@ -97,9 +98,11 @@ function beginWork(unitOfWork : Fiber) : ?Fiber { case HostComponent: updateHostComponent(unitOfWork); break; + case CoroutineHandlerPhase: + // This is a restart. Reset the tag to the initial phase. + unitOfWork.tag = CoroutineComponent; + // Intentionally fall through since this is now the same. case CoroutineComponent: - // Reset the stage to zero. - unitOfWork.stage = 0; updateCoroutineComponent(unitOfWork); // This doesn't take arbitrary time so we could synchronously just begin // eagerly do the work of unitOfWork.child as an optimization. diff --git a/src/renderers/shared/fiber/ReactFiberCompleteWork.js b/src/renderers/shared/fiber/ReactFiberCompleteWork.js index 311860aa4e..acc757241a 100644 --- a/src/renderers/shared/fiber/ReactFiberCompleteWork.js +++ b/src/renderers/shared/fiber/ReactFiberCompleteWork.js @@ -25,6 +25,7 @@ var { ClassComponent, HostComponent, CoroutineComponent, + CoroutineHandlerPhase, YieldComponent, } = ReactTypesOfWork; @@ -52,43 +53,37 @@ function recursivelyFillYields(yields, output : ?Fiber | ?ReifiedYield) { } } -function handleCoroutine(unitOfWork : Fiber) { +function moveCoroutineToHandlerPhase(unitOfWork : Fiber) { var coroutine = (unitOfWork.input : ?ReactCoroutine); if (!coroutine) { throw new Error('Should be resolved by now'); } - if (unitOfWork.stage === 0) { - // First step of the coroutine has completed. Now we need to do the second. - // TODO: It would be nice to have a multi stage coroutine represented by a - // single component, or at least tail call optimize nested ones. - // TODO: If we end up not using multi stage coroutines, we could also reuse - // the tag field to switch between the two stages. - unitOfWork.stage = 1; + // First step of the coroutine has completed. Now we need to do the second. + // TODO: It would be nice to have a multi stage coroutine represented by a + // single component, or at least tail call optimize nested ones. Currently + // that requires additional fields that we don't want to add to the fiber. + // So this requires nested handlers. + unitOfWork.tag = CoroutineHandlerPhase; - // Build up the yields. - // TODO: Compare this to a generator or opaque helpers like Children. - var yields : Array = []; - var child = unitOfWork.child; - while (child) { - recursivelyFillYields(yields, child.output); - child = child.sibling; - } - var fn = coroutine.handler; - var props = coroutine.props; - var nextChildren = fn(props, yields); - - unitOfWork.stateNode = ReactChildFiber.reconcileChildFibers( - unitOfWork, - unitOfWork.stateNode, - nextChildren - ); - return unitOfWork.stateNode; - } else { - // The coroutine is now complete. - transferOutput(unitOfWork.stateNode, unitOfWork); - return null; + // Build up the yields. + // TODO: Compare this to a generator or opaque helpers like Children. + var yields : Array = []; + var child = unitOfWork.child; + while (child) { + recursivelyFillYields(yields, child.output); + child = child.sibling; } + var fn = coroutine.handler; + var props = coroutine.props; + var nextChildren = fn(props, yields); + + unitOfWork.stateNode = ReactChildFiber.reconcileChildFibers( + unitOfWork, + unitOfWork.stateNode, + nextChildren + ); + return unitOfWork.stateNode; } exports.completeWork = function(unitOfWork : Fiber) : ?Fiber { @@ -106,7 +101,12 @@ exports.completeWork = function(unitOfWork : Fiber) : ?Fiber { break; case CoroutineComponent: console.log('/coroutine component', unitOfWork.input.handler.name); - return handleCoroutine(unitOfWork); + return moveCoroutineToHandlerPhase(unitOfWork); + case CoroutineHandlerPhase: + transferOutput(unitOfWork.stateNode, unitOfWork); + // Reset the tag to now be a first phase coroutine. + unitOfWork.tag = CoroutineComponent; + break; case YieldComponent: // Does nothing. break; diff --git a/src/renderers/shared/fiber/ReactTypesOfWork.js b/src/renderers/shared/fiber/ReactTypesOfWork.js index 5009cfa6bc..d54dc16807 100644 --- a/src/renderers/shared/fiber/ReactTypesOfWork.js +++ b/src/renderers/shared/fiber/ReactTypesOfWork.js @@ -18,7 +18,8 @@ var TypesOfWork = { ClassComponent: 2, HostComponent: 3, CoroutineComponent: 4, - YieldComponent: 5, + CoroutineHandlerPhase: 5, + YieldComponent: 6, }; module.exports = TypesOfWork;