-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.3.js
94 lines (81 loc) · 1.96 KB
/
index.3.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
let isMount = true; // 区分mount update
let workInProgressHook = null; // 操作hook 链表
const fiber = {
memoizedState: null, // 保存对应组件的hook, 单向链表
stateNode: App, // 对应组件本身
}
// 调度器 触发更新
function schedule() {
workInProgressHook = fiber.memoizedState;
const app = fiber.stateNode();
isMount = false;
return app;
}
function dispatchAction(queue, action) {
const update = {
action,
next: null,
}
// 环状链表
if (queue.pending === null) {
// u0 -> u0 -> u0
update.next = update;
} else {
// u1 -> u0 -> u1
update.next = queue.pending.next;
queue.pending.next = update;
}
queue.pending = update;
schedule();
}
function useState(initialState) {
let hook;
if (isMount) {
hook = {
memoizedState: initialState,
next: null,
queue: {
pending: null,
}
}
if (fiber.memoizedState === null) {
fiber.memoizedState = hook;
} else {
workInProgressHook.next = hook;
}
workInProgressHook = hook;
} else {
hook = workInProgressHook;
workInProgressHook = workInProgressHook.next;
}
// 更新基于的上次数据
let baseState = hook.memoizedState;
// 判断需不需要更新
if (hook.queue.pending !== null) {
let firstUpdate = hook.queue.pending.next;
do {
const action = firstUpdate.action;
baseState = action(baseState);
firstUpdate = firstUpdate.next;
} while(firstUpdate !== hook.queue.pending.next)
hook.queue.pending = null;
}
hook.memoizedState = baseState;
return [ baseState, dispatchAction.bind(null, hook.queue) ]
}
function App() {
const [ num, setNum ] = useState(0);
const [ num1, setNum1 ] = useState(10);
console.log('isMount:', isMount);
console.log('num:', num);
console.log('num1:', num1);
return {
onClick() {
setNum(num => num + 1)
},
onClick1() {
setNum1(num => num + 10)
}
}
}
window.app = schedule();