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

simpleCreateStore #9

Open
ZWkang opened this issue May 27, 2018 · 0 comments
Open

simpleCreateStore #9

ZWkang opened this issue May 27, 2018 · 0 comments

Comments

@ZWkang
Copy link
Owner

ZWkang commented May 27, 2018

const createStore = (reducer) > {
  let state;
  let listeners = [];
  const getState = () => state;
  const dispatch = (action) => {
    state = reduce(state, action);
    listeners.forEach(listener => listener());
  }
  const subscribe = (listener) => {
    listeners.push(listener);
    return () => {
      listeners = listeners.filter(l => l !== listener)
    };
  }
  dispatch( {} )
  return { getState, dispatch, subscribe};
}
// 接受多个reducer
// 对state做遍历执行 找到对应的reducer然后进行处理。
const combineReducers = (reducers) => {
    return (state = {} ,action )=>{
        return Object.keys(reducers).reduce(
            (nextState,key) => {
                 nextState[key] = reducers[key](state[key],action);
                 return nextState
            },
            {}
        )
    }
}
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