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-awaiter
Advanced tools
A Redux middleware for giving opportunities to await and receive actions in anywhere
Redux Awaiter is a Redux middleware for giving opportunities to await and receive actions in anywhere.
Redux Awaiter is designed to await and receive Redux actions in React components and help us manage local state conveniently.
It's inspired by Redux Saga, but the problems they solved are totally different.
class UserListView extends React.PureComponent {
state = { loading: false };
componentDidMount() {
const { actions: { fetchUserList } } = this.props;
fetchUserList();
this.setState(state => ({ ...state, loading: true }));
await take('RECEIVE_USER_LIST');
this.setState(state => ({ ...state, loading: false }));
}
render() {
return (
<Spin loading={loading}>
<ul>
{users.map(({ id, name }) => <li key={id}>{name}</li>)}
</ul>
</Spin>
)
}
}
npm i redux-awaiter -D
Redux Awaiter is written in TypeScript.
The internal type definition is based on flux standard action.
type ActionType = string;
interface BaseAction {
type: ActionType;
}
interface Action<P, M = {}> extends BaseAction {
payload: P;
meta?: M;
error?: true;
}
A pattern is to determine whether an action is matching with another.
type Pattern<P = {}, M = {}> = string | RegExp | ((action: Action<P, M>) => boolean);
import { createStore, applyMiddleware } from 'redux';
import { createAwaiterMiddleware } from 'redux-awaiter';
const awaiterMiddleware = createAwaiterMiddleware();
const store = createStore(rootReducer, applyMiddleware(
// other middlewares (e.g. routerMiddleware)
awaiterMiddleware,
));
const take: <P, M = {}>(pattern: Pattern<P, M>) => Promise<Action<P, M>>;
take
receives a pattern as its single argument, and returns a promise.
That promise will resolve the first matching action which is dispatched.
const action = await take('RECEIVE_DATA'); // action.type should be RECEIVE_DATA
takeAllOf
receives an array of patterns as it single argument, and returns an array of promises.
Internally, takeAllOf(patterns)
is the same with Promise.all(patterns.map(take))
.
If you need to await and receive multiple actions in specific order, use multiple await take()
instead.
const [{ payload: articles }, { payload: users }] = await takeAllOf(['RECEIVE_ARTICLES', 'RECEIVE_USERS']);
takeOneOf
receives an array of patterns as it single argument, and returns a promise.
Internally, takeOneOf(patterns)
is the same with Promise.race(patterns.map(take))
.
const { type } = await takeOneOf(['FETCH_USER_SUCCESS', 'FETCH_USER_FAILURE']);
if (type === 'FETCH_USER_SUCCESS') {
// success
} else {
// failure
}
You might not need takeOneOf
:
const { type } = await take(/^FETCH_USER/);
Avoid relying on props MUTATION!
This may cause unexpected behaviors, or make your components difficult to maintain.
async componentDidMount() {
const { data } = this.props // this.props.data is mapped from redux store.
// dispatch an action and do some async call (e.g. xhr, fetch)
await take('RECEIVE_DATA'); // receive new data and update redux store by reducer
// DANGER: this.props.data is MUTATED!
console.assert(this.props.data === data); // Assertion failed!
}
FAQs
A Redux middleware for giving opportunities to await and receive actions in anywhere
We found that redux-awaiter 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.