@workpop/optimistic-middleware
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -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
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
31155
57
96