Skip to content

Instantly share code, notes, and snippets.

@wegorich
Created September 14, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wegorich/656415262788d1f5aa7c00d2bb99a176 to your computer and use it in GitHub Desktop.
Save wegorich/656415262788d1f5aa7c00d2bb99a176 to your computer and use it in GitHub Desktop.
let actionsArray = [];
let types = {};
export function getTypes() {
return types;
}
export function setActionTypes(actions, mutateWith = ['Request', 'Error']) {
if (!actionsArray.length) {
actionsArray = Object.keys(actions);
types = actionsArray.reduce((res, cur) => {
res[cur] = cur;
mutateWith.forEach(e => {
const key = cur + e;
res[key] = key;
});
return res;
}, {});
if (typeof window !== 'undefined') {
window.types = types;
}
}
return actionsArray;
}
export function simpleActions(actions, mutateWith) {
/* eslint-disable */
const actionsArray = setActionTypes(actions, mutateWith);
actionsArray.forEach(key => {
let action = actions[key];
actions[key] = function () {
if (!action) {
console.warn(`The action ${key} should return Object or Promise`);
action = {};
}
if (action.type) {
types[key] = action.type;
}
const actionResult = action.apply(this, arguments);
if (actionResult && actionResult.then) {
actionResult.type = action.type || key;
return actionResult;
}
return {
payload: actionResult,
type: action.type || key,
};
};
});
/* eslint-enable */
return actions;
}
/**
* Handle promise process:
* https://github.com/pirateminds/redux-pirate-promise
* The library provide possibility to handle request, response, error
* globally via overriding the default params in the redux-pirate-promise.
* By the default it doenst modify the flow.
* You can work with promises like with pure function without any overhead
* It bit changed to original redux-pirate-promise - by the default
* library not patch the content going thought the pure function without promise responses
* but in our case we should use it. To be able path the {actions, types} arguments to
* the redux
*/
export default (params) => {
let {
// @ts-ignore
request = (data, next) => ({}),
response = (data, next) => next(data),
error = (data, next) => next(data),
} = params;
return (next) => (action) => {
let { promise, payload, type, ...rest } = action;
if (typeof action.then === "function") {
// new way working with promises
return action.then(
(result) => {
response({ payload: result, type: type }, next);
// set delay to be sure that changes pass to the state
return new Promise(resolve => setTimeout(() => resolve(result)));
},
(err) => {
console.log('reducer error', err);
error({ ...rest, error: err, type: type }, next);
return Promise.reject(err);
},
);
} else {
/**
* NODE:
* We cant handle errors in pure function only in promisified function
*/
if (!promise) {
response({ payload: payload || rest, type: type }, next)
return action;
}
// old way working with promise param
request({ payload: payload || rest, type: type }, next);
return promise().then(
(result) => {
response({ payload: result, type: type }, next);
return result;
},
(err) => {
error({ ...rest, error: err, type: type }, next);
},
);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment