redux-resource
Version:
Resource management for Redux.
55 lines (41 loc) • 2.18 kB
JavaScript
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 _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; };
// Update the meta for `resources`
export default function setResourceMeta(options) {
var resources = options.resources,
newMeta = options.newMeta,
meta = options.meta,
mergeMeta = options.mergeMeta,
_options$initialResou = options.initialResourceMeta,
initialResourceMeta = _options$initialResou === undefined ? {} : _options$initialResou;
var next = _extends({}, meta);
if (!resources) {
return meta;
}
var mergeMetaOption = typeof mergeMeta !== 'undefined' ? mergeMeta : true;
var resourcesArray = Array.isArray(resources) ? resources : Object.values(resources);
if (!resourcesArray.length) {
return next;
}
resourcesArray.forEach(function (resource) {
var id = (typeof resource === 'undefined' ? 'undefined' : _typeof(resource)) === 'object' ? resource.id : resource;
// If we have no ID for this resource, or if its not a number or string,
// then we bail. This currently isn't logging so that we don't double-blast
// the user with meta **and** attribute update problems. If the ID check
// is moved into the success reducers directly, then we may be able to
// remove these typeof checks for efficiency.
if (!id && id !== 0 || typeof id !== 'string' && typeof id !== 'number') {
return;
}
var currentMeta = next[id];
var startMeta = void 0;
if (currentMeta) {
startMeta = _extends({}, initialResourceMeta, currentMeta);
} else {
startMeta = initialResourceMeta;
}
var baseMeta = mergeMetaOption ? startMeta : initialResourceMeta;
next[id] = _extends({}, baseMeta, newMeta);
});
return next;
}