Skip to content

Commit

Permalink
Added configurable cancelChecker to middleware factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lundin committed Mar 12, 2017
1 parent bfc2de4 commit 6ae17b5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
} from 'redux-pack';

import { isFunction } from './helpers.js';
import { _cancelChecker } from './middleware.js';

const callHandler = (handlers, fn, state, action) => {
if (!handlers[fn] || !isFunction(handlers[fn]))
Expand All @@ -15,7 +16,7 @@ const proxyHandlers = (handlers = {}) => ({
success: (...args) => callHandler(handlers, 'success', ...args),
finish: (...args) => callHandler(handlers, 'finish', ...args),
failure: (state, action) => {
if (action.payload === 'CANCEL_REJECTION')
if (_cancelChecker(action.payload))
return callHandler(handlers, 'cancel', state, action);
return callHandler(handlers, 'failure', state, action);
}
Expand Down
7 changes: 5 additions & 2 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {

import { isFunction, isPromise } from './helpers.js';

export let _cancelChecker = () => false;

function callEventHook(meta, hook, ...args) {
if (!meta || !isFunction(meta[hook]))
return;
Expand All @@ -20,16 +22,17 @@ const proxyMeta = (meta = {}) => ({
onSuccess: (...args) => callEventHook(meta, 'onSuccess', ...args),
onFinish: (...args) => callEventHook(meta, 'onFinish', ...args),
onFailure: (reason) => {
if (reason === 'CANCEL_REJECTION')
if (_cancelChecker(reason))
return callEventHook(meta, 'onCancel', reason);
return callEventHook(meta, 'onFailure', reason);
}
});

const replaceMeta = (action, newMeta) => ({ ...action, meta: newMeta });

const packaMiddleware = (store) => (next) => {
const packaMiddleware = (cancelChecker) => (store) => (next) => {
const pack = reduxPack(store)(next);
_cancelChecker = cancelChecker;

return (action) => {
if (isPromise(action.promise)) {
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createStore, applyMiddleware } from 'redux';
import { middleware as packa, handle } from '../../src';

const PROMISE_ACTION = 'PROMISE_ACTION';
export const CANCEL_TOKEN = 'CANCEL_TOKEN';

const defaultState = {
isLoading: false,
Expand Down Expand Up @@ -49,5 +50,6 @@ export function promiseAction(promise, meta = {}) {
}

export function setupStore() {
return createStore(rootReducer, defaultState, applyMiddleware(packa));
const cancelChecker = (reason) => reason === CANCEL_TOKEN;
return createStore(rootReducer, defaultState, applyMiddleware(packa(cancelChecker)));
}
4 changes: 2 additions & 2 deletions test/middleware-tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setupStore, promiseAction } from './helpers/store.js';
import { setupStore, promiseAction, CANCEL_TOKEN } from './helpers/store.js';
import * as controllablePromise from './helpers/controllable-promise.js';

const createHandleMock = () => ({
Expand Down Expand Up @@ -94,7 +94,7 @@ Feature('Middleware', () => {


When('promise is canceled', () => {
promise.reject('CANCEL_REJECTION');
promise.reject(CANCEL_TOKEN);
});

Then('store should not be in loading state', () => {
Expand Down

0 comments on commit 6ae17b5

Please sign in to comment.