Skip to content

Commit 7e46ffa

Browse files
authored
Start putting the fiber docs together (#24)
1 parent a2ba412 commit 7e46ffa

29 files changed

+1505
-58
lines changed

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"description": "A tiny React renderer to demonstrate how to write a renderer.",
55
"main": "./index.js",
66
"scripts": {
7-
"test": "node ./test"
7+
"flow": "pushd src/fiber; flow; popd;",
8+
"test-stack": "node ./test --stack",
9+
"test-fiber": "node ./test --fiber",
10+
"test": "node ./test && node ./test --fiber && npm run flow"
811
},
912
"repository": {
1013
"type": "git",
@@ -26,9 +29,17 @@
2629
"url": "https://github.com/iamdustan/tiny-render-renderer/issues"
2730
},
2831
"homepage": "https://github.com/iamdustan/tiny-render-renderer",
32+
"babel": {
33+
"plugins": [
34+
"transform-flow-strip-types"
35+
]
36+
},
2937
"dependencies": {
38+
"babel-register": "^6.23.0",
3039
"fbjs": "^0.8.4",
3140
"react": "15.3.x"
3241
},
33-
"devDependencies": {}
42+
"devDependencies": {
43+
"babel-plugin-transform-flow-strip-types": "^6.22.0"
44+
}
3445
}

src/fiber-types/.flowconfig

Whitespace-only changes.

src/fiber-types/React.js.flow

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @flow */
2+
3+
export type ReactComponent = typeof React$Component;
4+
export type ReactElement = typeof React$Element;
5+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ReactCompositeComponentTypes
10+
* @flow
11+
*/
12+
13+
export type CompositeComponentTypes = 0 | 1 | 2;
14+
15+
module.exports = {
16+
ImpureClass: 0,
17+
PureClass: 1,
18+
StatelessFunctional: 2,
19+
};
20+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ReactCoroutine
10+
* @flow
11+
*/
12+
13+
'use strict';
14+
15+
import type { ReactNodeList } from './ReactTypes';
16+
17+
type ReifiedYield = { continuation: Object, props: Object };
18+
type CoroutineHandler<T> = (props: T, yields: Array<ReifiedYield>) => ReactNodeList;
19+
20+
export type ReactCoroutine = {
21+
$$typeof: Symbol | number,
22+
key: null | string,
23+
children: any,
24+
// This should be a more specific CoroutineHandler
25+
handler: (props: any, yields: Array<ReifiedYield>) => ReactNodeList,
26+
props: any,
27+
};
28+
export type ReactYield = {
29+
$$typeof: Symbol | number,
30+
key: null | string,
31+
props: Object,
32+
continuation: mixed
33+
};
34+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2016-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
10+
* @providesModule ReactElementType
11+
*/
12+
13+
'use strict';
14+
15+
export type Source = {
16+
fileName: string,
17+
lineNumber: number,
18+
};
19+
20+
export type ReactElement = {
21+
$$typeof: any,
22+
type: any,
23+
key: any,
24+
ref: any,
25+
props: any,
26+
_owner: any, // ReactInstance or ReactFiber
27+
28+
// __DEV__
29+
_store: {
30+
validated: boolean,
31+
},
32+
_self: ReactElement,
33+
_shadowChildren: any,
34+
_source: Source,
35+
};
36+

src/fiber-types/ReactFiber.js.flow

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ReactFiber
10+
* @flow
11+
*/
12+
import type { Source } from './ReactElementType';
13+
import type { ReactInstance, DebugID } from './ReactInstanceType';
14+
import type { TypeOfWork } from './ReactTypeOfWork';
15+
import type { TypeOfSideEffect } from './ReactTypeOfSideEffect';
16+
import type { PriorityLevel } from './ReactPriorityLevel';
17+
import type { UpdateQueue } from './ReactFiberUpdateQueue';
18+
19+
20+
// A Fiber is work on a Component that needs to be done or was done. There can
21+
// be more than one per component.
22+
export type Fiber = {
23+
// __DEV__ only
24+
_debugID ?: DebugID,
25+
_debugSource ?: Source | null,
26+
_debugOwner ?: Fiber | ReactInstance | null, // Stack compatible
27+
28+
// These first fields are conceptually members of an Instance. This used to
29+
// be split into a separate type and intersected with the other Fiber fields,
30+
// but until Flow fixes its intersection bugs, we've merged them into a
31+
// single type.
32+
33+
// An Instance is shared between all versions of a component. We can easily
34+
// break this out into a separate object to avoid copying so much to the
35+
// alternate versions of the tree. We put this on a single object for now to
36+
// minimize the number of objects created during the initial render.
37+
38+
// Tag identifying the type of fiber.
39+
tag: TypeOfWork,
40+
41+
// Unique identifier of this child.
42+
key: null | string,
43+
44+
// The function/class/module associated with this fiber.
45+
type: any,
46+
47+
// The local state associated with this fiber.
48+
stateNode: any,
49+
50+
// Conceptual aliases
51+
// parent : Instance -> return The parent happens to be the same as the
52+
// return fiber since we've merged the fiber and instance.
53+
54+
// Remaining fields belong to Fiber
55+
56+
// The Fiber to return to after finishing processing this one.
57+
// This is effectively the parent, but there can be multiple parents (two)
58+
// so this is only the parent of the thing we're currently processing.
59+
// It is conceptually the same as the return address of a stack frame.
60+
return: Fiber | null,
61+
62+
// Singly Linked List Tree Structure.
63+
child: Fiber | null,
64+
sibling: Fiber | null,
65+
index: number,
66+
67+
// The ref last used to attach this node.
68+
// I'll avoid adding an owner field for prod and model that as functions.
69+
ref: null | (((handle : mixed) => void) & { _stringRef: ?string }),
70+
71+
// Input is the data coming into process this fiber. Arguments. Props.
72+
pendingProps: any, // This type will be more specific once we overload the tag.
73+
// TODO: I think that there is a way to merge pendingProps and memoizedProps.
74+
memoizedProps: any, // The props used to create the output.
75+
76+
// A queue of state updates and callbacks.
77+
updateQueue: UpdateQueue | null,
78+
79+
// The state used to create the output
80+
memoizedState: any,
81+
82+
// Effect
83+
effectTag: TypeOfSideEffect,
84+
85+
// Singly linked list fast path to the next fiber with side-effects.
86+
nextEffect: Fiber | null,
87+
88+
// The first and last fiber with side-effect within this subtree. This allows
89+
// us to reuse a slice of the linked list when we reuse the work done within
90+
// this fiber.
91+
firstEffect: Fiber | null,
92+
lastEffect: Fiber | null,
93+
94+
// This will be used to quickly determine if a subtree has no pending changes.
95+
pendingWorkPriority: PriorityLevel,
96+
97+
// This value represents the priority level that was last used to process this
98+
// component. This indicates whether it is better to continue from the
99+
// progressed work or if it is better to continue from the current state.
100+
progressedPriority: PriorityLevel,
101+
102+
// If work bails out on a Fiber that already had some work started at a lower
103+
// priority, then we need to store the progressed work somewhere. This holds
104+
// the started child set until we need to get back to working on it. It may
105+
// or may not be the same as the "current" child.
106+
progressedChild: Fiber | null,
107+
108+
// When we reconcile children onto progressedChild it is possible that we have
109+
// to delete some child fibers. We need to keep track of this side-effects so
110+
// that if we continue later on, we have to include those effects. Deletions
111+
// are added in the reverse order from sibling pointers.
112+
progressedFirstDeletion: Fiber | null,
113+
progressedLastDeletion: Fiber | null,
114+
115+
// This is a pooled version of a Fiber. Every fiber that gets updated will
116+
// eventually have a pair. There are cases when we can clean up pairs to save
117+
// memory if we need to.
118+
alternate: Fiber | null,
119+
120+
// Conceptual aliases
121+
// workInProgress : Fiber -> alternate The alternate used for reuse happens
122+
// to be the same as work in progress.
123+
124+
};
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ReactFiberReconciler
10+
* @flow
11+
*/
12+
13+
'use strict';
14+
15+
import type { ReactComponent } from './React';
16+
17+
import type { Fiber } from './ReactFiber';
18+
import type { FiberRoot } from './ReactFiberRoot';
19+
import type { PriorityLevel } from './ReactPriorityLevel';
20+
import type { ReactNodeList } from './ReactTypes';
21+
22+
type Deadline = {
23+
timeRemaining : () => number
24+
};
25+
26+
type OpaqueHandle = Fiber;
27+
type OpaqueRoot = FiberRoot;
28+
29+
export type HostConfig<T, P, I, TI, PI, C, CX, PL> = {
30+
31+
getRootHostContext(rootContainerInstance : C) : CX,
32+
getChildHostContext(parentHostContext : CX, type : T) : CX,
33+
getPublicInstance(instance : I | TI) : PI,
34+
35+
createInstance(
36+
type : T,
37+
props : P,
38+
rootContainerInstance : C,
39+
hostContext : CX,
40+
internalInstanceHandle : OpaqueHandle
41+
) : I,
42+
appendInitialChild(parentInstance : I, child : I | TI) : void,
43+
finalizeInitialChildren(parentInstance : I, type : T, props : P, rootContainerInstance : C) : boolean,
44+
45+
prepareUpdate(
46+
instance : I,
47+
type : T,
48+
oldProps : P,
49+
newProps : P,
50+
rootContainerInstance : C,
51+
hostContext : CX
52+
) : null | PL,
53+
commitUpdate(
54+
instance : I,
55+
updatePayload : PL,
56+
type : T,
57+
oldProps : P,
58+
newProps : P,
59+
internalInstanceHandle : OpaqueHandle
60+
) : void,
61+
commitMount(instance : I, type : T, newProps : P, internalInstanceHandle : OpaqueHandle) : void,
62+
63+
shouldSetTextContent(props : P) : boolean,
64+
resetTextContent(instance : I) : void,
65+
66+
createTextInstance(
67+
text : string,
68+
rootContainerInstance : C,
69+
hostContext : CX,
70+
internalInstanceHandle : OpaqueHandle
71+
) : TI,
72+
commitTextUpdate(textInstance : TI, oldText : string, newText : string) : void,
73+
74+
appendChild(parentInstance : I | C, child : I | TI) : void,
75+
insertBefore(parentInstance : I | C, child : I | TI, beforeChild : I | TI) : void,
76+
removeChild(parentInstance : I | C, child : I | TI) : void,
77+
78+
scheduleAnimationCallback(callback : () => void) : number | void,
79+
scheduleDeferredCallback(callback : (deadline : Deadline) => void) : number | void,
80+
81+
prepareForCommit() : void,
82+
resetAfterCommit() : void,
83+
84+
useSyncScheduling ?: boolean,
85+
};
86+
87+
export type Reconciler<C, I, TI> = {
88+
createContainer(containerInfo : C) : OpaqueRoot,
89+
updateContainer(
90+
element : ReactNodeList,
91+
container : OpaqueRoot,
92+
parentComponent : ?ReactComponent<any, any, any>
93+
) : void,
94+
performWithPriority(priorityLevel : PriorityLevel, fn : Function) : void,
95+
batchedUpdates<A>(fn : () => A) : A,
96+
unbatchedUpdates<A>(fn : () => A) : A,
97+
syncUpdates<A>(fn : () => A) : A,
98+
deferredUpdates<A>(fn : () => A) : A,
99+
100+
// Used to extract the return value from the initial render. Legacy API.
101+
getPublicRootInstance(container : OpaqueRoot) : (ReactComponent<any, any, any> | TI | I | null),
102+
103+
// Use for findDOMNode/findHostNode. Legacy API.
104+
findHostInstance(component : Fiber) : I | TI | null,
105+
};
106+
107+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ReactFiberRoot
10+
* @flow
11+
*/
12+
13+
'use strict';
14+
15+
import type { Fiber } from './ReactFiber';
16+
import type { UpdateQueue } from './ReactFiberUpdateQueue';
17+
18+
export type FiberRoot = {
19+
// Any additional information from the host associated with this root.
20+
containerInfo: any,
21+
// The currently active root fiber. This is the mutable root of the tree.
22+
current: Fiber,
23+
// Determines if this root has already been added to the schedule for work.
24+
isScheduled: boolean,
25+
// The work schedule is a linked list.
26+
nextScheduledRoot: ?FiberRoot,
27+
// Linked list of callbacks to call after updates are committed.
28+
callbackList: ?UpdateQueue,
29+
};
30+

0 commit comments

Comments
 (0)