Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以将多个dispatch 合并成一个操作 #11

Open
whxcode opened this issue Sep 24, 2020 · 0 comments
Open

可以将多个dispatch 合并成一个操作 #11

whxcode opened this issue Sep 24, 2020 · 0 comments

Comments

@whxcode
Copy link

whxcode commented Sep 24, 2020

我刚刚学react,看见您写的小书对我帮助很大。感谢
function createStore(s,stateChanger) {
const listeners = []
const queue = []
let state = s
const getState = () => state
let timerId :any = 0
const dispatch = (action) => {
if(typeof action == 'function') {
const d = stateChanger(state,action())
for(const value of listeners) {
value(d)
state = d
}
return;
}
queue.push(() => {
return stateChanger(state,action)
})
clearImmediate(timerId)
timerId = setImmediate (() => {
let d = null
while (queue.length) {
d = queue.shift()()
state = d
}
for(const value of listeners) {
value(d)
}
},1)
}
const subscribe = (listen) => listeners.push(listen)
return { getState,dispatch,subscribe }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant