Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit fcb7c7b

Browse files
committed
Update to newer descriptor proposal
Factory instead of type property in plain mode Inline instead of fast mode Pooled mode updated to more powerful model (pooled mode is still unclear how reclaiming can work in certain scenarios)
1 parent 9554af5 commit fcb7c7b

File tree

1 file changed

+62
-36
lines changed

1 file changed

+62
-36
lines changed

Diff for: 01 - Core/05 - Descriptors.js

+62-36
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
{
1313
type: Button,
1414
props: {
15-
...Button.defaultProps, // optional
16-
foo: 'bar',
15+
foo: bar,
1716
children: [
1817
{ type: 'span', props: { children: a } },
1918
{ type: 'span', props: { children: b } }
@@ -22,57 +21,68 @@
2221

2322
// optional
2423
key: 'mybutton',
25-
ref: myButtonRef,
26-
27-
// optional
28-
owner: React.currentOwner,
29-
context: React.currentContext
24+
ref: myButtonRef
3025
}
3126

3227
/**
3328
* JSX
3429
*/
3530

36-
<Button foo="bar" key="mybutton" ref={myButtonRef}>
31+
<Button foo={bar} key="mybutton" ref={myButtonRef}>
3732
<span>{a}</span>
3833
<span>{b}</span>
3934
</Button>
4035

4136
/**
4237
* PLAIN JS
38+
* __DEV__ MODE
4339
*
4440
* This helper function ensures that your static children don't get the key
4541
* warning. It creates a descriptor for you with the current owner/context.
4642
* The props object is cloned and key/ref moved onto the descriptor.
4743
*/
4844

49-
var C = React.createDescriptor;
45+
var _Button = React.createFactory(Button);
46+
var _span = React.createFactory('span');
5047

51-
C({ type: Button, foo: 'bar', key: 'mybutton', ref: myButtonRef },
52-
C({ type: 'span' }, a),
53-
C({ type: 'span' }, b)
48+
_Button({ foo: bar, key: 'mybutton', ref: myButtonRef },
49+
_span(null, a),
50+
_span(null, b)
5451
)
5552

5653
/**
57-
* If you use JSX or statically can analyze that the C(...) call belongs to
54+
* If you use JSX, or can statically analyze that the Factory calls belongs to
5855
* React, then you can chose to opt-in to one of the optimizations modes.
5956
*/
6057

6158
/**
62-
* FAST MODE
59+
* INLINE
60+
* PRODUCTION MODE
6361
*
64-
* Fast mode allocates a props object inline instead of cloning a temporary
65-
* object. type, key and ref are separate arguments. Default props gets merged
66-
* in by mutating the props object. If they can be statically inferred, they
67-
* could also be inlined instead. This should not be typed in user code.
62+
* Inline mode simply creates the descriptor objects inline in the code, with
63+
* a lookup for current owner/context as well as resolving default props.
64+
* If defaults aren't known statically, then we create a factory that can help
65+
* assign defaults quickly on the newly created object.
6866
*/
6967

70-
var F = React.createDescriptorFast;
68+
var Button_assignDefaults = React.createDefaultsFactory(Button);
69+
70+
{
71+
type: Button,
72+
props: Button_assignDefaults({
73+
foo: bar,
74+
children: [
75+
{ type: 'span', props: { children: a }, key: null, ref: null, _owner: React._currentOwner, _context: React._currentContext },
76+
{ type: 'span', props: { children: b }, key: null, ref: null, _owner: React._currentOwner, _context: React._currentContext }
77+
]
78+
}),
79+
80+
key: 'mybutton',
81+
ref: myButtonRef,
7182

72-
F(Button, 'mybutton', myButtonRef, { foo: 'bar', children: [
73-
F('span', null, null, { children: a }),
74-
F('span', null, null, { children: b })
75-
]})
83+
_owner: React._currentOwner,
84+
_context: React._currentContext
85+
}
7686

7787
/**
7888
* POOLED MODE
@@ -81,14 +91,34 @@ F(Button, 'mybutton', myButtonRef, { foo: 'bar', children: [
8191
* from an pool and reuses them. It overrides the props on the pooled object.
8292
*/
8393

84-
var P = React.getPooledDescriptor;
85-
var A = React.getPooledArray;
86-
var t1, t2;
87-
88-
(t1 = P(Button, 'mybutton', myButtonRef), t1.props.foo = 'bar', t1.props.children = A(2),
89-
t1.props.children[0] = (t2 = P('span'), t2.props.children = a, t2),
90-
t1.props.children[1] = (t2 = P('span'), t2.props.children = b, t2),
91-
, t1)
94+
var P1 = React.createDescriptorPool({
95+
type: Button,
96+
key: 'mybutton',
97+
props: {
98+
foo: null,
99+
children: null
100+
}
101+
});
102+
var P2 = React.createDescriptorPool({
103+
type: 'span',
104+
props: {
105+
children: null
106+
}
107+
});
108+
var A2 = React.createArrayPool(2); // Number of items in the array
109+
var t1, t1p, t1c, t2;
110+
111+
(
112+
t1 = P1(),
113+
t1.ref = myButtonRef,
114+
t1p = t1.props,
115+
t1p.foo = bar,
116+
t1p.children = A2(),
117+
t1c = t1p.children,
118+
t1c[0] = (t2 = P2(), t2.props.children = a, t2),
119+
t1c[1] = (t2 = P2(), t2.props.children = b, t2),
120+
t1
121+
)
92122

93123
/**
94124
* NATIVE COMPONENTS
@@ -97,10 +127,6 @@ var t1, t2;
97127
* just strings. JSX will convert any lower-case tag name, or if it has a dash,
98128
* into a string value instead of a scope reference. This makes them compatible
99129
* with custom tags (Web Components).
100-
*
101-
* There's no direct dependency between a component and the React runtime.
102-
*
103-
* A tree of just native components should be JSON serializable.
104130
*/
105131

106132
/**
@@ -112,7 +138,7 @@ var t1, t2;
112138
*/
113139

114140
X`
115-
<my-button foo="bar" key="mybutton" ref=${myButtonRef}>
141+
<my-button foo=${bar} key="mybutton" ref=${myButtonRef}>
116142
<span>${a}</span>
117143
<span>${b}</span>
118144
</my-button>

0 commit comments

Comments
 (0)