-
Notifications
You must be signed in to change notification settings - Fork 27
/
bindings.ts
342 lines (294 loc) · 7.86 KB
/
bindings.ts
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { HookType } from "../shared/hooks";
import type { Component, VNode } from "preact";
import type {
Component as IComponent,
VNode as IVNode,
} from "preact/src/internal";
import { ComponentHooks, HookState, PreactBindings } from "../shared/bindings";
import { RendererConfig } from "../shared/renderer";
import { getRenderReasonPost } from "./renderReason";
// Mangle accessors
/**
* Get the direct parent of a `vnode`
*/
export function getVNodeParent(vnode: VNode): VNode | null {
return (
(vnode as IVNode)._parent ||
(vnode as any).__ ||
// Older Preact X versions used `__p`
(vnode as any).__p ||
null
);
}
/**
* Check if a `vnode` is the root of a tree
*/
export function isRoot(vnode: VNode, config: RendererConfig): boolean {
return getVNodeParent(vnode) == null && vnode.type === config.Fragment;
}
/**
* Return the component instance of a `vnode` or `hookState`
*/
export function getComponent(node: HookState | VNode): Component | null {
return (node as HookState | IVNode)._component || (node as any).__c || null;
}
/**
* Get a `vnode`'s _dom reference.
*/
export function getDom(vnode: VNode): HTMLElement | Text | null {
return (vnode as IVNode)._dom || (vnode as any).__e || null;
}
export function hasDom(x: any): boolean {
return x != null && ("_dom" in x || "__e" in x);
}
/**
* Check if a `vnode` represents a `Suspense` component
*/
export function isSuspenseVNode(vnode: VNode): boolean {
const c = getComponent(vnode) as any;
// FYI: Mangling of `_childDidSuspend` is not stable in Preact < 10.3.0
return c != null && !!(c._childDidSuspend || c.__c);
}
/**
* Get the internal hooks state of a component
*/
export function getComponentHooks(vnode: VNode): ComponentHooks | null {
const c = getComponent(vnode);
if (!c) return null;
return (c as any).__hooks || (c as any).__H || null;
}
export function getStatefulHooks(vnode: VNode): HookState[] | null {
const hooks = getComponentHooks(vnode);
return hooks !== null
? hooks._list ||
hooks.__ ||
hooks.i || // Preact 10.1.0
null
: null;
}
export function isUseReducerOrState(hookState: HookState): boolean {
return !!hookState._component || !!hookState.__c;
}
export function getStatefulHookValue(hookState: HookState): unknown {
if (hookState !== null) {
const value = hookState._value || hookState.__ || null;
if (value !== null && Array.isArray(value)) {
return value[0];
}
}
return null;
}
export function getPendingHookValue(state: HookState) {
// Preact >= 10.8.1
if (state.__pendingValue !== undefined) {
return state.__pendingValue;
}
// Preact > 10.8.1
else if (state.__V !== undefined) {
return state.__V;
}
// Preact 10.8.1
else if (state.o !== undefined) {
return state.o;
}
return undefined;
}
export function setPendingHookValue(state: HookState, value: unknown) {
// Preact >= 10.8.1
if ("__pendingValue" in state) {
state.__pendingValue = value;
}
// Preact > 10.8.1
else if ("__V" in state) {
state.__V = value;
}
// Preact === 10.8.1
else if ("o" in state) {
state.o = value;
}
}
export function getHookState(
vnode: VNode,
index: number,
type?: HookType,
): unknown {
const c = getComponent(vnode);
if (c === null) return null;
const list = getStatefulHooks(vnode);
if (list && list[index]) {
// useContext
if (type === HookType.useContext) {
const context = list[index]._context || list[index].__c || list[index].c;
const provider = c.context[context._id] || c.context[context.__c];
return provider
? provider.props.value
: context._defaultValue || context.__;
}
let value;
const state = list[index];
// Prefer current value before pending
if ("_value" in state) {
value = state._value;
} else if ("__" in state) {
value = state.__;
} else {
value = getPendingHookValue(list[index]);
}
if (type === HookType.useRef) {
return value.current;
} else if (type === HookType.useErrorBoundary && !value) {
return "__preact_empty__";
}
return value;
}
return [];
}
/**
* Get the diffed children of a `vnode`
*/
export function getActualChildren(
vnode: VNode,
): Array<VNode | null | undefined> {
return (vnode as IVNode)._children || (vnode as any).__k || [];
}
// End Mangle accessors
/**
* Get the root of a `vnode`
*/
export function findRoot(vnode: VNode, config: RendererConfig): VNode {
let next: VNode | null = vnode;
while ((next = getVNodeParent(next)) != null) {
if (isRoot(next, config)) {
return next;
}
}
return vnode;
}
/**
* Get human readable name of the component/dom element
*/
export function getDisplayName(vnode: VNode, config: RendererConfig): string {
const { type } = vnode;
if (type === config.Fragment) return "Fragment";
else if (typeof type === "function") {
// Context is a special case :((
// See: https://reactjs.org/docs/context.html#contextdisplayname
const c = getComponent(vnode)!;
if (c !== null) {
// Consumer
if (c.constructor) {
const ct = (c.constructor as any).contextType;
if (ct && ct.Consumer === type && ct.displayName) {
return `${ct.displayName}.Consumer`;
}
}
// Provider
if ((c as any).sub) {
const ctx = (type as any)._contextRef || (type as any).__;
if (ctx && ctx.displayName) {
return `${ctx.displayName}.Provider`;
}
}
if (isSuspenseVNode(vnode)) {
return "Suspense";
}
// Preact 10.4.1 uses a raw Component as a child for Suspense
// by doing `createElement(Component, ...);`
if (type === config.Component) {
return "Component";
}
}
return type.displayName || type.name || "Anonymous";
} else if (typeof type === "string") return type;
return "#text";
}
export function getNextState<S>(c: Component): S {
return (c as IComponent)._nextState || (c as any).__s || null;
}
export function setNextState<S>(c: Component, value: S): S {
return ((c as IComponent)._nextState = (c as any).__s = value);
}
function getSuspenseStateKey(c: Component<any, any>) {
if ("_suspended" in c.state) {
return "_suspended";
} else if ("__e" in c.state) {
return "__e";
}
// This is a bit whacky, but property name mangling is unsafe in
// Preact <10.4.9
const keys = Object.keys(c.state);
if (keys.length > 0) {
return keys[0];
}
return null;
}
export function getSuspendedState(vnode: VNode) {
const c = getComponent(vnode);
if (c) {
const key = getSuspenseStateKey(c);
if (key) {
return !!(c as any)._nextState[key];
}
}
return null;
}
export function isTextVNode(vnode: VNode) {
return vnode !== null && vnode.type === null;
}
export function createSuspenseState(vnode: VNode, suspended: boolean) {
const c = getComponent(vnode) as Component<any, any>;
const key = getSuspenseStateKey(c);
if (c && key) {
return { [key]: suspended };
}
return {};
}
export function getInstance(vnode: VNode): any {
// For components we use the instance to check refs, otherwise
// we'll use a dom node
if (typeof vnode.type === "function") {
return getComponent(vnode);
}
return getDom(vnode);
}
export function isComponent(vnode: VNode) {
return vnode !== null && typeof vnode.type === "function";
}
export function isVNode(x: any): x is VNode {
return x != null && x.type !== undefined && hasDom(x);
}
export function isElement(vnode: VNode): boolean {
return typeof vnode.type === "string";
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function isPortal(vnode: VNode) {
// TODO: Find a way to detect portals
return false;
}
export const bindingsV10: PreactBindings<VNode> = {
isRoot,
getDisplayName,
getPropsVNodeDisplayName: getDisplayName,
getActualChildren,
getDom,
isTextVNode,
getInstance,
createSuspenseState,
getComponent,
getComponentHooks,
getHookState,
getPendingHookValue,
setPendingHookValue,
getVNodeParent,
isComponent,
isElement,
isSuspenseVNode,
getSuspendedState,
isVNode,
setNextState,
isPortal,
getStatefulHookValue,
getStatefulHooks,
isUseReducerOrState,
getRenderReasonPost,
};