-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhooks.js
More file actions
234 lines (184 loc) · 5.6 KB
/
hooks.js
File metadata and controls
234 lines (184 loc) · 5.6 KB
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
/*
This is a conceptual representation of what React does with hooks.
It is over-simplified, but illustrates a few concepts:
1) React will rerun your component function for you
2) Hooks being called with trigger the rerun
3) Order of hooks matters because of this, as they need to retrieve
the values that React has saved off.
4) Updates are batched together to save on work.
You can run this file with `node hooks` in the project directory.
Each function is set to print out what it does so you can see the
different behavior over time. The simulated experience is an input
field that gets updated except for if the update is 'special_value'
and there is a counter for how many times the input has been updated.
*/
// Example Component
function MyComponent() {
start('MyComponent');
const [value, setValue] = useState('');
const [total, add] = useReducer((total, n) => total + n, 0);
useEffect(() => {
block('custom effect running', { value, total });
}, [value, total]);
const output = create('div', {}, [
create('input', { value, onChange: (newValue) => {
if (newValue === 'special_value') {
setValue(value);
add(0);
} else {
setValue(newValue);
add(1);
}
}}),
create('span', {}, [total]),
]);
end('MyComponent');
return output;
}
// utilities
let indent = '';
function start(block, ...args) {
console.log(`${indent}${block}`, ...args);
indent += ' ';
}
function end(block) {
indent = indent.substring(0, indent.length - 2);
console.log(`${indent}END ${block}`);
}
function block(name, ...args) {
console.log(`${indent}${name}`, ...args);
}
// "React" internals
function create(component, props = {}, children = []) {
block('create', component);
return { component, props, children };
}
let currentContext;
function mount(instance) {
start('mount', instance.component);
const children = instance.children.map(mount);
if (typeof instance.component === 'string') {
end('mount');
return { ...instance, children };
}
currentContext = instance.context = instance.context ?? {
hooks: [],
component: instance.component,
capture: true,
hookIndex: 0,
instance,
};
currentContext.hookIndex = 0;
instance.value = instance.component({ ...instance.props, children });
currentContext.capture = false;
end('mount');
return instance;
}
const scheduled = new WeakMap();
function schedule(fn, arg) {
start('schedule', fn);
if (!scheduled.has(fn)) {
block('new function');
scheduled.set(fn, new Set());
}
const fnCalls = scheduled.get(fn);
if (fnCalls.has(arg)) {
block('already scheduled');
end('schedule');
return;
}
block('scheduling unique call');
fnCalls.add(arg);
queueMicrotask(() => {
fn(arg);
fnCalls.delete(arg);
});
end('schedule');
}
function useState(initialValue) {
start('useState', { initialValue });
if (!currentContext.capture) {
const hook = currentContext.hooks[currentContext.hookIndex];
block('getting saved value', { value: hook.pair[0] });
currentContext.hookIndex++;
end('useState');
return hook.pair;
}
const pair = [initialValue, (newValue) => {
const oldValue = pair[0];
pair[0] = newValue;
block('setState', { oldValue, newValue });
if (oldValue !== newValue) {
schedule(mount, currentContext.instance);
}
}];
currentContext.hooks.push({ pair });
end('useState');
return pair;
}
function useReducer(fn, initialValue) {
start('useReducer', { initialValue });
if (!currentContext.capture) {
const hook = currentContext.hooks[currentContext.hookIndex];
block('getting saved value', { value: hook.pair[0] });
currentContext.hookIndex++;
end('useReducer');
return hook.pair;
}
const pair = [initialValue, (...args) => {
const newValue = fn(pair[0], ...args);
const oldValue = pair[0];
pair[0] = newValue;
block('runReducer', { oldValue, newValue });
if (oldValue !== newValue) {
schedule(mount, currentContext.instance);
}
}];
currentContext.hooks.push({ pair });
end('useReducer');
return pair;
}
function useEffect(effect, dependencies) {
start('useEffect', { effect });
if (!currentContext.capture) {
const hook = currentContext.hooks[currentContext.hookIndex];
block('retreiving saved effect', hook.effect);
currentContext.hookIndex++;
if (!hook.dependencies.every((dependency, i) => dependency === dependencies[i])) {
block('run cleanup', hook.cleanup);
hook.cleanup && hook.cleanup();
schedule(() => hook.cleanup = effect());
}
end('useEffect');
return;
}
const hook = { effect, dependencies };
schedule(() => hook.cleanup = effect());
currentContext.hooks.push(hook);
end('useEffect');
}
// Example "mounting" of component and interaction
function pause(message) {
return new Promise(res => {
setTimeout(() => {
console.log('--------------------------------------');
console.log(message);
console.log('--------------------------------------');
res();
}, 1000)
});
}
async function example() {
await pause('mounting MyComponent');
const dom = mount(create(MyComponent));
await pause('updating input to change');
dom.value.children[0].props.onChange('change');
await pause('updating input to new_change');
dom.value.children[0].props.onChange('new_change');
await pause('updating counter only');
dom.value.children[0].props.onChange('new_change');
await pause('updating neither input or counter');
dom.value.children[0].props.onChange('special_value');
await pause('done');
}
example();