加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React Redux源函数ensureCanMutateNextListeners?

发布时间:2020-12-15 16:16:14 所属栏目:百科 来源:网络整理
导读:如何理解函数ensureCanMutateNextListeners? https://github.com/reactjs/redux/blob/master/src/createStore.js 解决方法 发布从 Aaron Powell’s blog post here获得的相关部分 来自redux的订阅函数的示例实现 let subscriptions = [];const subscribe =
如何理解函数ensureCanMutateNextListeners?
https://github.com/reactjs/redux/blob/master/src/createStore.js

解决方法

发布从 Aaron Powell’s blog post here获得的相关部分

来自redux的订阅函数的示例实现

let subscriptions = [];
const subscribe = function (fn) {
    if (typeof fn !== 'function') {
        throw Error('The provided listener must be a function');
    }

    var subscribed = true;

    // This line is what we are interested in
    subscriptions.push(fn);

    return function () {
        if (!subscribed) {
            return;
        }

        var index = subscriptions.indexOf(fn);
        subscriptions.splice(index,1);
        subscribed = false;
    };
};

对于每次执行的调度,我们必须通知每个订户.

来自博客

Now one thing you might want to add to the subscribe function is mutation hold on the subscribers collection. The reason for this that you want the collection of listeners shouldn’t change while a dispatch is running.

继续 …

So here we have a function ensureCanMutateNextListeners that,when invoked,checks if the two arrays are the same array reference,if they are,use slice to clone currentSubscriptions so that when we modify the nextSubscriptions array (adding or removing a listener) it won’t impact any currently running dispatch pipeline. The goal here is to ensure that the listeners that are used by dispatch are a point in time,for when the dispatch started.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读