[redux] - createStore简单的实现



class createStore {
    
    
  constructor(reducer, preloadedState, enhancer) {
    
    
    this.currentReducer = reducer;
    this.currentState = preloadedState;
    this.currentListeners = [];
    this.nextListeners = this.currentListeners;
    this.isDispatching = false;
  }

  ensureCanMutateNextListeners = () => {
    
    
    if (this.nextListeners === this.currentListeners) {
    
    
      this.nextListeners = this.currentListeners.slice();
    }
  }

  getState = () => {
    
    
    if (this.isDispatching) {
    
    
      throw new Error(
        'The reducer has already received the state as an argument.'
      )
    }
    return this.currentState;
  }

  subscribe = listener => {
    
    
    if (typeof listener !== 'function') {
    
    
      throw new Error('Expected the listener to be a function.');
    }

    if (this.isDispatching) {
    
    
      throw new Error(
        'component and invoke store.getState() in the callback to access the latest state.'
      )
    }

    let isSubscribed = true;
    this.ensureCanMutateNextListeners();
    this.nextListeners.push(listener);

    return function unsubscribe() {
    
    
      if (!isSubscribed) {
    
    
        return;
      }

      if (this.isDispatching) {
    
    
        throw new Error(
          'You may not unsubscribe from a store listener while the reducer is executing. '
        )
      }

      isSubscribed = false;  // 控制注销只执行一次

      this.ensureCanMutateNextListeners();
      const index = this.nextListeners.indexOf(listener);
      this.nextListeners.splice(index, 1);
      this.currentListeners = null;
    }

  }

  dispatch = action => {
    
    
    if (typeof action.type === 'undefined') {
    
    
      throw new Error(
        'Actions may not have an undefined "type" property. '
      )
    }

    if (this.isDispatching) {
    
    
      throw new Error('Reducers may not dispath actions')
    }

    try {
    
    
      this.isDispatching = true;
      this.currentState = this.currentReducer(this.currentState, action)
    } finally {
    
    
      this.isDispatching = false;
    }

    const listeners = (this.currentListeners = this.nextListeners)
    for (let i = 0; i < listeners.length; i++) {
    
    
      const listener = listeners[i];
      listener();
    }
    console.log(reducer);
    return action
  }
}



const reducer = (state = {
    
     count: 0 }, action) => {
    
    
  switch (action.type) {
    
    
    case 'INCREASE': return {
    
     count: state.count + 1 };
    case 'DECRESE': return {
    
     count: state.count - 1 };
    default: return state;
  }
}


const store = new createStore(reducer);
store.subscribe(() => {
    
    
  console.log(store.getState());
})


const actions = {
    
    
  increase: () => ({
    
    
    type: 'INCREASE'
  }),
  decrese: () => ({
    
    
    type: 'DECREASE'
  })
}


store.dispatch(actions.increase());
store.dispatch(actions.increase());
store.dispatch(actions.increase());

$ node 1.js

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/piano9425/article/details/109284882