-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
191 lines (166 loc) · 4.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// @flow
import { assign, getChildren } from "./util";
import { options, Fragment, Component, h } from "preact";
import { Suspense } from "preact/compat";
const createContextDefaultValue = "__p";
const createContextDefaultValueNew = "__";
const _skipEffects = "__s";
const _children = "__k"
const _parent = "__"
const _diff = "__b"
/*::
type VNode = {
type: string | Function,
props: Object,
__c: typeof Component,
__: any,
__k: any,
};
type VNodes = VNode | Array<VNode>;
type Options = {
render: (vnode: VNode) => void;
};
*/
export default function prepass(
vnode /*: VNode */,
visitor /*: ?(vnode: VNode, component: typeof Component) => ?Promise<any> */,
context /*: ?Object */,
parent /*: ?VNode */,
) /*: Promise<any|Array<any>> */ {
// null, boolean, text, number "vnodes" need to prepassing...
if (vnode == null || typeof vnode !== "object") {
return Promise.resolve();
}
let nodeName = vnode.type,
props = vnode.props,
children = [];
context = context || {};
if (!parent) {
parent = h(Fragment, null);
parent[_children] = parent[vnode];
}
vnode[_parent] = parent;
if (
typeof nodeName === "function" &&
nodeName !== Fragment &&
nodeName !== Suspense // We're handling Suspense the same way as we do fragments as we do not want something to catch promises during prepass
) {
let doRender /* : () => Promise<void> */;
let c = (vnode.__c = new Component(props, context));
// initialize components in dirty state so setState() doesn't enqueue re-rendering:
c.__d = true;
c.__v = vnode;
/* istanbul ignore else */
if (c.state === undefined) {
c.state = {};
}
let isClassComponent = false;
// Necessary for createContext api. Setting this property will pass
// the context value as `this.context` just for this component.
let cxType = nodeName.contextType;
let provider = cxType && context[cxType.__c];
let cctx =
cxType != null
? provider
? provider.props.value
: cxType[createContextDefaultValue] ||
cxType[createContextDefaultValueNew]
: context;
vnode[_parent] = parent
if (
!nodeName.prototype ||
typeof nodeName.prototype.render !== "function"
) {
// stateless functional components
doRender = () => {
try {
const previousSkipEffects = options[_skipEffects];
options[_skipEffects] = true;
// options.render was renamed to _render (mangled to __r)
if (options.render) options.render(vnode);
if (options.__r) {
options.__r(vnode);
}
const renderResult = Promise.resolve(
nodeName.call(vnode.__c, props, cctx)
);
options[_skipEffects] = previousSkipEffects;
return renderResult;
} catch (e) {
if (e && e.then) {
return e.then(doRender, doRender);
}
return Promise.reject(e);
}
};
} else {
isClassComponent = true;
// class-based components
// c = new nodeName(props, context);
c = vnode.__c = new nodeName(props, cctx);
// initialize components in dirty state so setState() doesn't enqueue re-rendering:
c.__d = true;
c.__v = vnode;
c.props = props;
c.context = cctx;
if (c.state === undefined) {
c.state = {};
}
// TODO: does react-ssr-prepass call the visitor before lifecycle hooks?
if (nodeName.getDerivedStateFromProps)
c.state = assign(
assign({}, c.state),
nodeName.getDerivedStateFromProps(c.props, c.state)
);
else if (c.componentWillMount) c.componentWillMount();
doRender = () => {
try {
// options.render was renamed to _render (mangled to __r)
if (options.render) options.render(vnode);
if (options.__r) options.__r(vnode);
return Promise.resolve(c.render(c.props, c.state, c.context));
} catch (e) {
if (e && e.then) {
return e.then(doRender, doRender);
}
return Promise.reject(e);
}
};
}
if (options[_diff]) {
options[_diff](vnode)
}
return (visitor
? (
visitor(vnode, isClassComponent ? c : undefined) || Promise.resolve()
).then(doRender)
: doRender()
).then((rendered) => {
if (c.getChildContext) {
context = assign(assign({}, context), c.getChildContext());
}
if (Array.isArray(rendered)) {
vnode[_children] = [];
return Promise.all(
rendered.map((node) => {
vnode[_children].push(node);
return prepass(node, visitor, context, vnode)
})
);
}
return prepass(rendered, visitor, context, vnode);
});
} else {
if (options[_diff]) options[_diff](vnode)
}
if (props && getChildren((children = []), props.children).length) {
vnode[_children] = [];
return Promise.all(
children.map((child) => {
vnode[_children].push(child);
return prepass(child, visitor, context, vnode)
})
);
}
return Promise.resolve();
}