React Redux 源函数确保CanMutateNextListeners? [英] React Redux source function ensureCanMutateNextListeners?

查看:29
本文介绍了React Redux 源函数确保CanMutateNextListeners?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何理解函数ensureCanMutateNextListeners?https://github.com/reactjs/redux/blob/master/src/createStore.js

how to understand function ensureCanMutateNextListeners? https://github.com/reactjs/redux/blob/master/src/createStore.js

推荐答案

发布从 Aaron Powell 的博客文章

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;
    };
};

对于每个执行的dispatch,我们必须通知每个subscriber.

For every dispatch performed, we must notify every subscriber.

来自博客

现在您可能想要添加到 subscribe 函数的一件事是对订阅者集合的突变保持.这样做的原因是您希望在调度运行时不应该更改侦听器的集合.

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.

继续...

所以这里我们有一个函数 ensureCanMutateNextListeners ,当调用时,检查两个数组是否是相同的数组引用,如果是,使用 slice 克隆 currentSubscriptions 以便当我们修改 nextSubscriptions 数组时(添加或删除侦听器)它不会影响任何当前正在运行的调度管道.此处的目标是确保调度使用的侦听器时间点,用于调度开始的时间.

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.

这篇关于React Redux 源函数确保CanMutateNextListeners?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆