gpt4 book ai didi

reactjs - React Redux源码函数ensureCanMutateNextListeners?

转载 作者:行者123 更新时间:2023-12-03 13:46:27 30 4
gpt4 key购买 nike

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

最佳答案

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

来自 reduxsubscribe 函数的示例实现

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.

关于reactjs - React Redux源码函数ensureCanMutateNextListeners?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250266/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com