Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@workpop/optimistic-middleware
Advanced tools
Optimistically apply actions that will be reverted on error.
npm install --save @workpop/optimistic-middleware
import { optimisticMiddleware } from '@workpop/optimistic-middleware';
Optimistic updates require your reducer to have a certain state shape.
type OptimisticStateType = {
data: any,
optimisticState: string
}
function todos(state = {}, action = {}) {
const { type, data, ...rest } = action;
switch(type) {
case 'ADD_TODO':
const todos = state.data;
return {
data: todos.concat([data.text]),
...rest
};
default:
return state;
}
}
function optimisticAddTodo(text) {
return {
type: 'ADD_TODO',
stateKey: 'todos',
mutation(cb) {
return Meteor.call('addTodo', text, cb);
},
data: text
}
}
Let's break down our action shape:
type OptimisticActionType = {
type: string,
stateKey: string,
mutation: Function,
data: any
}
Parameters:
[type] - action type corresponding to reducer state change
[stateKey] - key of reducer related to action
[mutation] - asynchronous function intended to mutate some data on the server
[data] - data needed to change the state in the reducer
When you dispatch an OptimisticAction, the action is intercepted by Redux middleware. Immediately we save the previous state of the passed in stateKey
in case our action throws an error.
We start the dispatch process immediately executing the data change in the action to the reducer. The state is appended with OPTIMISTIC_UPDATE_START
, to signal the start of our dispatch.
From there, we call our asynchronous method.
If the method returns an error, we append several pieces of meta data with the error reason and OPTIMISTIC_UPDATE_FAILURE
(helpful for development).
type OptimisticErrorType = {
error: string,
data: any
optimisticState: string,
type: string
}
If successful, we append OPTIMISTIC_UPDATE_SUCCESS
and finish the dispatch process.
Because we are reverting our state when errors occur based on the reducer state, this middleware is confined to the reducer stateKey
passed into the action. Optimistic Middleware does not currently support updates that affect multiple stateKeys.
FAQs
Optimistic Methods Middleware for Redux
The npm package @workpop/optimistic-middleware receives a total of 0 weekly downloads. As such, @workpop/optimistic-middleware popularity was classified as not popular.
We found that @workpop/optimistic-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.