Skip to content

Instantly share code, notes, and snippets.

@asfktz
Created June 8, 2017 16:22
Show Gist options
  • Save asfktz/1551b293805fcf291a923bd18176ee91 to your computer and use it in GitHub Desktop.
Save asfktz/1551b293805fcf291a923bd18176ee91 to your computer and use it in GitHub Desktop.
let pouchMiddleware
let invalidate = true;
const MyWrapperMiddleware = store => next => action => {
const state = store.getState()
if (!state.isLoggedIn)
return next(action)
if (someConditionForChange) {
invalidate = true
/* clean up, kill listeners if needed */
}
if (!invalidate) {
invalidate = false
pouchMiddleware = PouchMiddleware({
path: state.currentPath,
db,
actions: {
remove: doc => { return { type: types.DELETE_TODO, id: doc._id } },
insert: doc => { return { type: types.INSERT_TODO, todo: doc } },
batchInsert: docs => { return { type: types.BATCH_INSERT_TODOS, todos: docs } }
update: doc => { return { type: types.UPDATE_TODO, todo: doc } },
}
})
}
return pouchMiddleware(store)(next)(action)
}
export default MyWrapperMiddleware;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment