mayor-model-cache
Version:
A nuclear-js module provides a cache for your models
76 lines (56 loc) • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _lodash = require('lodash.isarray');
var _lodash2 = _interopRequireDefault(_lodash);
var _lodash3 = require('lodash.isobject');
var _lodash4 = _interopRequireDefault(_lodash3);
var _invariant = require('invariant');
var _invariant2 = _interopRequireDefault(_invariant);
var _actionTypes = require('./action-types');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
exports.default = {
/**
* Action to load data to model cache.
*
* @public
* @param {object} model
* @param {object} data - raw data
* @return {object}
*/
load: function load(model, data) {
(0, _invariant2.default)((0, _lodash4.default)(model) && !(0, _lodash2.default)(model), 'ModelCache.actions.load(): expected model to be instance of Model; got %s', typeof model === 'undefined' ? 'undefined' : _typeof(model));
(0, _invariant2.default)((0, _lodash4.default)(data) || (0, _lodash2.default)(data), 'ModelCache.actions.load(): expected data to be an object; got %s', typeof data === 'undefined' ? 'undefined' : _typeof(data));
if (!(0, _lodash2.default)(data)) {
data = [data];
}
return {
type: _actionTypes.MODEL_CACHE_LOAD_ENTITY,
payload: {
model: model,
data: data
}
};
},
/**
* Action to remove data from model cache.
*
* @public
* @param {object} model
* @param {object} data
* @return {object}
*/
remove: function remove(model, data) {
(0, _invariant2.default)((0, _lodash4.default)(model) && !(0, _lodash2.default)(model), 'ModelCache.actions.load(): expected model to be instance of Model; got %s', typeof model === 'undefined' ? 'undefined' : _typeof(model));
(0, _invariant2.default)('string' === typeof data.id, 'ModelCache.actions.remove(): expected data.id to be a %s, got %s.', 'string', _typeof(data.id));
return {
type: _actionTypes.MODEL_CACHE_REMOVE_ENTITY,
payload: {
model: model,
data: data
}
};
}
};