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.2.0 to 0.3.0

92

dist/middleware.js

@@ -13,2 +13,4 @@ 'use strict';

var isFunction = require('lodash.isfunction');
var OPTIMISTIC = {

@@ -20,57 +22,55 @@ START: 'OPTIMISTIC_UPDATE_START',

function optimisticMiddleware() {
return function (store) {
return function (next) {
return function (action) {
var storeState = store.getState();
var onSuccess = action.onSuccess;
var onError = action.onError;
var errorType = action.errorType;
var mutation = action.mutation;
var stateKey = action.stateKey;
var data = action.data;
var type = action.type;
function optimisticMiddleware(store) {
return function (next) {
return function (action) {
var storeState = store.getState();
var onSuccess = action.onSuccess;
var onError = action.onError;
var errorType = action.errorType;
var mutation = action.mutation;
var stateKey = action.stateKey;
var data = action.data;
var type = action.type;
var rest = _objectWithoutProperties(action, ['onSuccess', 'onError', 'errorType', 'mutation', 'stateKey', 'data', 'type']);
// get the previous state before we start
var rest = _objectWithoutProperties(action, ['onSuccess', 'onError', 'errorType', 'mutation', 'stateKey', 'data', 'type']);
// 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({
type: type,
data: data,
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
mutation(function (error, result) {
// if there is an error we need to revert our state back to the initial state before middleware ran
if (error) {
if (_.isFunction(onError)) {
onError(error);
}
return next({
error: error.reason,
data: previousState,
optimisticState: OPTIMISTIC.ERROR,
type: errorType || 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 the optimistic update first. This is described in our reducer for this actionType
next(_extends({
type: type,
data: data,
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, result) {
// if there is an error we need to revert our state back to the initial state before middleware ran
if (error) {
if (isFunction(onError)) {
onError(error);
}
// apply our update again but this time, change the OPTIMISTIC state
if (_.isFunction(onSuccess)) {
onSuccess(result);
}
return next({
type: type,
optimisticState: OPTIMISTIC.SUCCESS,
data: data
error: error.reason,
data: previousState,
optimisticState: OPTIMISTIC.ERROR,
type: errorType || type
});
}
// apply our update again but this time, change the OPTIMISTIC state
if (isFunction(onSuccess)) {
onSuccess(result);
}
return next({
type: type,
optimisticState: OPTIMISTIC.SUCCESS,
data: data
});
};
});
};
};
}
{
"name": "@workpop/optimistic-middleware",
"version": "0.2.0",
"version": "0.3.0",
"description": "Optimistic Methods Middleware for Redux",

@@ -27,6 +27,8 @@ "main": "dist/index.js",

"react-tools": "^0.13.3",
"redux": "^3.5.2"
"redux": "^3.5.2",
"sinon": "^1.17.4"
},
"dependencies": {
"babel-runtime": "6.x.x"
"babel-runtime": "6.x.x",
"lodash.isfunction": "^3.0.8"
},

@@ -33,0 +35,0 @@ "eslintConfig": {

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