1 | import { options, Fragment } from 'preact';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export function getDisplayName(vnode) {
|
10 | if (vnode.type === Fragment) {
|
11 | return 'Fragment';
|
12 | } else if (typeof vnode.type == 'function') {
|
13 | return vnode.type.displayName || vnode.type.name;
|
14 | } else if (typeof vnode.type == 'string') {
|
15 | return vnode.type;
|
16 | }
|
17 |
|
18 | return '#text';
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | let renderStack = [];
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | let ownerStack = [];
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | export function getCurrentVNode() {
|
52 | return renderStack.length > 0 ? renderStack[renderStack.length - 1] : null;
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | let hasBabelPlugin = false;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function isPossibleOwner(vnode) {
|
68 | return typeof vnode.type == 'function' && vnode.type != Fragment;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | export function getOwnerStack(vnode) {
|
77 | const stack = [vnode];
|
78 | let next = vnode;
|
79 | while (next._owner != null) {
|
80 | stack.push(next._owner);
|
81 | next = next._owner;
|
82 | }
|
83 |
|
84 | return stack.reduce((acc, owner) => {
|
85 | acc += ` in ${getDisplayName(owner)}`;
|
86 |
|
87 | const source = owner.__source;
|
88 | if (source) {
|
89 | acc += ` (at ${source.fileName}:${source.lineNumber})`;
|
90 | } else if (!hasBabelPlugin) {
|
91 | hasBabelPlugin = true;
|
92 | console.warn(
|
93 | 'Add @babel/plugin-transform-react-jsx-source to get a more detailed component stack. Note that you should not add it to production builds of your App for bundle size reasons.'
|
94 | );
|
95 | }
|
96 |
|
97 | return (acc += '\n');
|
98 | }, '');
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | export function setupComponentStack() {
|
107 | let oldDiff = options._diff;
|
108 | let oldDiffed = options.diffed;
|
109 | let oldRoot = options._root;
|
110 | let oldVNode = options.vnode;
|
111 | let oldRender = options._render;
|
112 |
|
113 | options.diffed = vnode => {
|
114 | if (isPossibleOwner(vnode)) {
|
115 | ownerStack.pop();
|
116 | }
|
117 | renderStack.pop();
|
118 | if (oldDiffed) oldDiffed(vnode);
|
119 | };
|
120 |
|
121 | options._diff = vnode => {
|
122 | if (isPossibleOwner(vnode)) {
|
123 | renderStack.push(vnode);
|
124 | }
|
125 | if (oldDiff) oldDiff(vnode);
|
126 | };
|
127 |
|
128 | options._root = (vnode, parent) => {
|
129 | ownerStack = [];
|
130 | if (oldRoot) oldRoot(vnode, parent);
|
131 | };
|
132 |
|
133 | options.vnode = vnode => {
|
134 | vnode._owner =
|
135 | ownerStack.length > 0 ? ownerStack[ownerStack.length - 1] : null;
|
136 | if (oldVNode) oldVNode(vnode);
|
137 | };
|
138 |
|
139 | options._render = vnode => {
|
140 | if (isPossibleOwner(vnode)) {
|
141 | ownerStack.push(vnode);
|
142 | }
|
143 |
|
144 | if (oldRender) oldRender(vnode);
|
145 | };
|
146 | }
|