[redux] - createStore简单的实现

49 篇文章 0 订阅
8 篇文章 0 订阅


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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值