redux-saga-thunk
Version:
Dispatching an action handled by redux-saga returns promise
76 lines (61 loc) • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateThunk = exports.hasKey = exports.getThunkId = exports.hasId = exports.getThunkName = exports.createThunkAction = exports.getThunkMeta = exports.isCleanAction = exports.isThunkRequestAction = exports.isThunkAction = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _actions = require('./actions');
var isThunkAction = exports.isThunkAction = function isThunkAction(action) {
return !!(action && action.meta && action.meta.thunk);
};
var isThunkRequestAction = exports.isThunkRequestAction = function isThunkRequestAction(action) {
return !!(isThunkAction(action) && _typeof(action.meta.thunk) === 'object' && action.meta.thunk.type === 'REQUEST');
};
var isCleanAction = exports.isCleanAction = function isCleanAction(action) {
return action.type === _actions.CLEAN;
};
var getThunkMeta = exports.getThunkMeta = function getThunkMeta(action) {
if (isThunkAction(action)) {
return action.meta.thunk;
}
return null;
};
var createThunkAction = exports.createThunkAction = function createThunkAction(action, thunk) {
return _extends({}, action, {
meta: _extends({}, action.meta, {
thunk: thunk
})
});
};
var getThunkName = exports.getThunkName = function getThunkName(action) {
var meta = getThunkMeta(action);
if (meta && typeof meta === 'string') {
return meta;
}
if (meta && (typeof meta === 'undefined' ? 'undefined' : _typeof(meta)) === 'object' && 'name' in meta) {
return meta.name;
}
return action.type;
};
var hasId = exports.hasId = function hasId(action) {
var meta = getThunkMeta(action);
return !!meta && (typeof meta === 'undefined' ? 'undefined' : _typeof(meta)) === 'object' && 'id' in meta;
};
var getThunkId = exports.getThunkId = function getThunkId(action) {
return hasId(action) ? getThunkMeta(action).id : undefined;
};
var hasKey = exports.hasKey = function hasKey(action) {
var meta = getThunkMeta(action);
return !!meta && (typeof meta === 'undefined' ? 'undefined' : _typeof(meta)) === 'object' && 'key' in meta;
};
var generateThunk = exports.generateThunk = function generateThunk(action) {
var thunk = getThunkMeta(action);
return hasKey(action) ? _extends({}, thunk, {
type: 'RESPONSE'
}) : _extends({}, (typeof thunk === 'undefined' ? 'undefined' : _typeof(thunk)) === 'object' ? thunk : {}, {
name: getThunkName(action),
key: Math.random().toFixed(16).substring(2),
type: 'REQUEST'
});
};