redux源码解读(一)自实现简易redux

redux的源码解读(一)

首先是自己实现的简易版的redux。主要是对着API按照自己的思路实现了一遍的初版,没有任何的异常处理

function createStore(reducer, initialState) {
    let currentState = initialState,
        listeners = [];
    function getState() {
        //返回当前的state
        return currentState;
    }
    function dispatch(action) {
        let prestate = currentState;
        currentState = reducer(prestate, action);
        listeners.forEach((item) => item());
    }
    function subscribe(callback) {
        //返回值是取消订阅
        let hasUnsubscribe = false;
        listeners.push(callback);
        return function unsubscribe() {
            if (!hasUnsubscribe) {
                let index = listeners.indexOf(callback);
                listeners = listeners.splice(index, 1);
            }
        }
    }
    function replaceReducer(newReducer) {
        reducer = newReducer;
    }
    return {getState, dispatch, subscribe, replaceReducer}
}

function testReducer(state = {sum:0}, action) {
    switch (action.type) {
        case 'ADD':
            return {
                ...state,
                sum: state.sum + 1
            }
        case 'DEC':
            return {
                ...state,
                sum: state.sum - 1
            }
        default:
            return state;
    }
}
const store = createStore(testReducer);
//测试
console.log('begin',store.getState());//begin undefined
store.subscribe(() => console.log('触发了action',store.getState()));
store.dispatch({type:'ADD'});//触发了action {sum: 1}
console.log('end',store.getState());//end {sum: 1}

这样的话是有问题的,就是我们不去dispatch的时候,初始的state是undefined的,显然不符合redux的API的要求。在redux API中,我们的初始state可以是从reducer或者从createStore中传入的。然而我们的代码中没从createStore中传入state的话在没有dispatch任何东西之前state是undefined的(从上面的log信息)。。。所以,创建store实例的时候自己先dispatch一次。从redux的源码中可以看到初始化的时候dispatch({ type: ActionTypes.INIT })。这个ActionTypes.INIT是一个随机数。我们做出一点修改即可。

function createStore(reducer, initialState) {
    let currentState = initialState,
        listeners = [];
        dispatch('initial');//初始化的时候dispatch下
    function getState() {
        //返回当前的state
        return currentState;
    }
    function dispatch(action) {
        let prestate = currentState;
        currentState = reducer(prestate, action);
        listeners.forEach((item) => item());
    }
    function subscribe(callback) {
        //返回值是取消订阅
        let hasUnsubscribe = false;
        listeners.push(callback);
        return function unsubscribe() {
            if (!hasUnsubscribe) {
                let index = listeners.indexOf(callback);
                listeners = listeners.splice(index, 1);
            }
        }
    }
    function replaceReducer(newReducer) {
        reducer = newReducer;
    }
    return {getState, dispatch, subscribe, replaceReducer}
}

function testReducer(state = {sum:0}, action) {
    switch (action.type) {
        case 'ADD':
            return {
                ...state,
                sum: state.sum + 1
            }
        case 'DEC':
            return {
                ...state,
                sum: state.sum - 1
            }
        default:
            return state;
    }
}
const store = createStore(testReducer);
console.log('begin',store.getState());
store.subscribe(() => console.log('触发了action',store.getState()));
store.dispatch({type:'ADD'});
console.log('end',store.getState());

至此我们的简易版redux的createStore就实现了。当然我们没有做任何的异常处理hhh。后面就进行其它部分的探讨,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值