Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@workpop/optimistic-middleware

Package Overview
Dependencies
Maintainers
6
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@workpop/optimistic-middleware - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

66

dist/middleware.js

@@ -19,40 +19,42 @@ 'use strict';

function optimisticMiddleware(store) {
return function (next) {
return function (action) {
var storeState = store.getState();
var mutation = action.mutation;
var stateKey = action.stateKey;
function optimisticMiddleware() {
return function (store) {
return function (next) {
return function (action) {
var storeState = store.getState();
var mutation = action.mutation;
var stateKey = action.stateKey;
var rest = _objectWithoutProperties(action, ['mutation', 'stateKey']);
// get the previous state before we start
var rest = _objectWithoutProperties(action, ['mutation', 'stateKey']);
// get the previous state before we start
var previousState = storeState && storeState[stateKey] && storeState[stateKey].data;
// if we don't have a mutation, proceed like normal
if (!mutation) {
return next(action);
}
// apply the optimistic update first. This is described in our reducer for this actionType
next(_extends({}, rest, {
optimisticState: OPTIMISTIC.START
}));
// next we're going to call our mutation, because we're in a Meteor context, we are expecting a callback with e,r
return mutation(function (error) {
// if there is an error we need to revert our state back to the initial state before middleware ran
if (error) {
return next({
error: error.reason,
data: previousState,
optimisticState: OPTIMISTIC.ERROR,
type: action.type
});
var previousState = storeState && storeState[stateKey] && storeState[stateKey].data;
// if we don't have a mutation, proceed like normal
if (!mutation) {
return next(action);
}
// apply our update again but this time, change the OPTIMISTIC state
return next(_extends({}, rest, {
optimisticState: OPTIMISTIC.SUCCESS
}));
});
// apply the optimistic update first. This is described in our reducer for this actionType
next(_extends({
optimisticState: OPTIMISTIC.START
}, rest));
// next we're going to call our mutation, because we're in a Meteor context, we are expecting a callback with e,r
return mutation(function (error) {
// if there is an error we need to revert our state back to the initial state before middleware ran
if (error) {
return next({
error: error.reason,
data: previousState,
optimisticState: OPTIMISTIC.ERROR,
type: action.type
});
}
// apply our update again but this time, change the OPTIMISTIC state
return next(_extends({
optimisticState: OPTIMISTIC.SUCCESS
}, rest));
});
};
};
};
}
{
"name": "@workpop/optimistic-middleware",
"version": "0.0.1",
"version": "0.1.0",
"description": "Optimistic Methods Middleware for Redux",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -9,2 +9,4 @@ # Optimistic Middleware

`import { optimisticMiddleware } from '@workpop/optimistic-middleware';`
## Usage

@@ -11,0 +13,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc