Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
redux-promise-mutex
Advanced tools
配合redux-thunk使用,可以自动对一些异步的action加锁。
/* Store */
import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import promiseMutex from 'redux-promise-mutex';
// promiseMutex 位置必须在 redux-thunk 之前
const middlewares = [promiseMutex, thunkMiddleware];
const store = createStore(
reducers,
applyMiddleware(...middlewares)
);
/* Actions */
function doAsync(startTime) {
// actions 必须返回函数(与redux-thunk相同),且该函数不能为匿名函数
return function doAsync(dispatch) {
//函数必须返回一个Promise/类Promise对象
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Done!');
}, 2000);
}).then((res) => {
const now = Date.now();
console.log('get: ' + res + ' cost: ' + (now - startTime) + 'ms');
});
}
}
/* Pages */
dispatch(doAsync(Date.now()));
0、中间件顺序必须在redux-thunk之前 1、action 返回一个函数。(这个也是 redux-thunk 异步操作的要求) 2、action 返回的函数必须是 * 非匿名函数 * ,或者函数带有 uuid 属性 3、函数返回值为 Promise / 类Promise 对象(可以进行then操作)
可以过滤掉短时间内重复的action。
/* without redux-promise-mutex */
for(let i = 0; i < 10; i++) {
dispatch(doAsync(Date.now()));
}
// => 2秒后有10个console
/* with redux-promise-mutex */
for(let i = 0; i < 10; i++) {
dispatch(doAsync(Date.now()));
}
// => 2秒后只有1个console
FAQs
add mutex for redux-thunk
We found that redux-promise-mutex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.