-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathindex.js
212 lines (190 loc) · 6.36 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Options for Preact.
import './runtime/catchError';
import './runtime/debounceRendering';
import './runtime/vnode';
import './runtime/unmount';
import { Component } from 'preact';
import {
VNODE_COMPONENT,
NAMESPACE,
HOOKS_LIST,
EFFECTS_LIST,
COMPONENT_HOOKS,
HOOK_ARGS,
HOOK_VALUE,
HOOK_CLEANUP,
} from './constants';
import { computeKey } from './computeKey';
import { vnodesForComponent, mappedVNodes } from './runtime/vnodesForComponent';
import { signaturesForType } from './runtime/signaturesForType';
let typesById = new Map();
let pendingUpdates = [];
function sign(type, key, forceReset, getCustomHooks, status) {
if (type) {
let signature = signaturesForType.get(type);
if (status === 'begin') {
signaturesForType.set(type, {
type,
key,
forceReset,
getCustomHooks: getCustomHooks || (() => []),
});
return 'needsHooks';
} else if (status === 'needsHooks') {
signature.fullKey = computeKey(signature);
}
}
}
function replaceComponent(OldType, NewType, resetHookState) {
const vnodes = vnodesForComponent.get(OldType);
if (!vnodes) return;
// migrate the list to our new constructor reference
vnodesForComponent.delete(OldType);
vnodesForComponent.set(NewType, vnodes);
mappedVNodes.set(OldType, NewType);
pendingUpdates = pendingUpdates.filter(p => p[0] !== OldType);
vnodes.forEach(vnode => {
if (!vnode.__c || !vnode.__c.__P) return;
// update the type in-place to reference the new component
vnode.type = NewType;
if (vnode[VNODE_COMPONENT]) {
vnode[VNODE_COMPONENT].constructor = vnode.type;
try {
if (vnode[VNODE_COMPONENT] instanceof OldType) {
const oldInst = vnode[VNODE_COMPONENT];
const newInst = new NewType(
vnode[VNODE_COMPONENT].props,
vnode[VNODE_COMPONENT].context
);
vnode[VNODE_COMPONENT] = newInst;
// copy old properties onto the new instance.
// - Objects (including refs) in the new instance are updated with their old values
// - Missing or null properties are restored to their old values
// - Updated Functions are not reverted
// - Scalars are copied
for (let i in oldInst) {
const type = typeof oldInst[i];
if (!(i in newInst)) {
newInst[i] = oldInst[i];
} else if (type !== 'function' && typeof newInst[i] === type) {
if (
type === 'object' &&
newInst[i] != null &&
newInst[i].constructor === oldInst[i].constructor
) {
Object.assign(newInst[i], oldInst[i]);
} else {
newInst[i] = oldInst[i];
}
}
}
}
} catch (e) {
/* Functional component */
vnode[VNODE_COMPONENT].constructor = NewType;
}
vnode.type = NewType
vnode[VNODE_COMPONENT].__v = vnode;
vnode[VNODE_COMPONENT].__v.type = NewType;
if (resetHookState) {
if (
vnode[VNODE_COMPONENT][COMPONENT_HOOKS] &&
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST] &&
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].length
) {
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].forEach(
possibleEffect => {
if (
possibleEffect[HOOK_CLEANUP] &&
typeof possibleEffect[HOOK_CLEANUP] === 'function'
) {
possibleEffect[HOOK_CLEANUP]();
possibleEffect[HOOK_CLEANUP] = undefined;
} else if (
possibleEffect[HOOK_ARGS] &&
possibleEffect[HOOK_VALUE] &&
Object.keys(possibleEffect).length === 3
) {
const cleanupKey = Object.keys(possibleEffect).find(
key => key !== HOOK_ARGS && key !== HOOK_VALUE
);
if (
cleanupKey &&
typeof possibleEffect[cleanupKey] == 'function'
) {
possibleEffect[cleanupKey]();
possibleEffect[cleanupKey] = undefined;
}
}
}
);
}
vnode[VNODE_COMPONENT][COMPONENT_HOOKS] = {
[HOOKS_LIST]: [],
[EFFECTS_LIST]: [],
};
} else if (
vnode[VNODE_COMPONENT][COMPONENT_HOOKS] &&
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST] &&
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].length
) {
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].forEach(
possibleEffect => {
if (
possibleEffect[HOOK_CLEANUP] &&
typeof possibleEffect[HOOK_CLEANUP] === 'function'
) {
possibleEffect[HOOK_CLEANUP]();
possibleEffect[HOOK_CLEANUP] = undefined;
} else if (
possibleEffect[HOOK_ARGS] &&
possibleEffect[HOOK_VALUE] &&
Object.keys(possibleEffect).length === 3
) {
const cleanupKey = Object.keys(possibleEffect).find(
key => key !== HOOK_ARGS && key !== HOOK_VALUE
);
if (cleanupKey && typeof possibleEffect[cleanupKey] == 'function')
possibleEffect[cleanupKey]();
possibleEffect[cleanupKey] = undefined;
}
}
);
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].forEach(hook => {
if (hook.__H && Array.isArray(hook.__H)) {
hook.__H = undefined;
}
});
}
Component.prototype.forceUpdate.call(vnode[VNODE_COMPONENT]);
}
});
}
self[NAMESPACE] = {
getSignature: type => signaturesForType.get(type),
register: (type, id) => {
if (typeof type !== 'function') return;
if (typesById.has(id)) {
const existing = typesById.get(id);
if (existing !== type) {
pendingUpdates.push([existing, type]);
typesById.set(id, type);
}
} else {
typesById.set(id, type);
}
if (!signaturesForType.has(type)) {
signaturesForType.set(type, {
getCustomHooks: () => [],
type,
});
}
},
getPendingUpdates: () => pendingUpdates,
flush: () => {
pendingUpdates = [];
},
replaceComponent,
sign,
computeKey,
};