javascript - 通过全局变量反应互斥锁
问题描述
我们正在使用React
,Redux
我们如何在互斥锁的帮助下保护资源不被访问?
可以通过全局变量以及将它们存储在哪里来做到这一点吗?
PS 由于状态更新延迟,我们不想通过 Redux 执行此操作
提供更多上下文的小例子
export default function configureStore() {
const store = createStore(rootReducer, persistedState, composedEnhancers);
store.subscribe(
// saveState saves state to localStorage
// here we need mutex which will prevent state from saving to localStorage
// and some way to toggle this mutex
throttle(() => {
saveState(store.getState());
}, 2000),
);
return store;
}
解决方案
我不确定这是否是正确的解决方案
请记住,这是可行的,因为store.subscribe
会监听 redux 状态更改
export default function configureStore() {
const store = createStore(rootReducer, persistedState, composedEnhancers);
store.subscribe(
throttle(() => {
const currentState = store.getState();
// if we allowing state to sync with localStorage
if (currentState.isMutexVariable) {
saveState(currentState);
}
}, 2000),
);
return store;
}
推荐阅读
- swift3 - swift3 - 为每一行打印数组索引
- javascript - 页面中无法识别 JS 方法
- javascript - Jest - 模拟一个返回 Promise 的 React 组件的依赖项
- c - 跳过字数,位于字符串中,由一个参数指示
- airflow - 气流 1.9 到 1.10 升级,现在我在 UI 中得到空日志
- firebase - Firebase 仪表板设置电子邮件已验证
- c - 使用 Cordic 进行数模转换
- python - 为什么这个 Python 脚本在 CPU 中比在 GPU 中运行得更快?
- ios - 无法在 IntelliJ Idea 上安装 RoboVM 插件
- c++ - 网络套接字创建问题