@workpop/optimistic-middleware
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33612
2
17
71
+ Addedlodash.isfunction@^3.0.8
+ Addedlodash.isfunction@3.0.9(transitive)