-
Notifications
You must be signed in to change notification settings - Fork 1
/
setState.js
50 lines (45 loc) · 1.26 KB
/
setState.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
class Update {
constructor (update) {
this.nextUpdate = null
this.update = update
}
}
class UpdateState {
constructor () {
this.baseState = null
this.firstUpdate = null
this.lastUpdate = null
}
enqueue (updates) {
const update = new Update(updates)
if (this.firstUpdate === null) {
this.firstUpdate = this.lastUpdate = update
} else {
this.lastUpdate.nextUpdate = update
this.lastUpdate = update
}
}
forceUpdate () {
console.log('this.baseState', this.baseState)
let currentState = this.baseState || {}
let current = this.firstUpdate
while (current) {
let nextState = typeof current.update === 'function' ? current.update(currentState) : current.update
currentState = { ...currentState, ...nextState };//使用当前更新得到新的状态
current = current.nextUpdate
}
this.firstUpdate = this.lastUpdate = null;//更新完成后要把链表清空
this.baseState = currentState;
return currentState;
}
}
const p = new UpdateState()
// setState({})
p.enqueue({name: 'perfectyang'})
p.enqueue({number: 0})
p.enqueue((state) => ({number: state.number + 1}))
p.forceUpdate()
// console.log(p.baseState)
p.enqueue({what: 'sss'})
p.forceUpdate()
// console.log(p.baseState)