@freshlysqueezedgames/hermes
Version:
independant state management pipeline
161 lines (120 loc) • 5.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _Action2 = require('./Action');
var _Action3 = _interopRequireDefault(_Action2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var toString = Object.prototype.toString;
var ARRAY = '[object Array]';
var OBJECT = '[object Object]';
var Reducer = function () {
function Reducer() {
_classCallCheck(this, Reducer);
var t = this;
t.path = "";
t.hermes = null;
}
/**
* @name Reduce
* @description This is called when a payload is ready to be incorporated in the store heap. Before this happens, the Reducer can be overridden
* so that the payload object is handled in the proper manner.
*
* NOTE: If you have child reducers (reducers assigned to child addresses) of the store heap, there is no need to modify these at they are handled first and
* will already have been affected.
* @param {*} action
* @param {*} state
* @param {*} payload
*/
_createClass(Reducer, [{
key: 'Reduce',
value: function Reduce(action) {
var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Object.create(null);
var payload = arguments[2];
if (!payload) {
return state;
}
var stateType = toString.call(state);
var payloadType = toString.call(payload);
// Because children can update, you must update only your layer of influence.
// In lamence terms, only update the state with the root layer of values... I think... this I guess depends on whether a custom child mutation has happened?
// The default reducer will apply to children... so I guess there will be updates there too...
if (stateType !== payloadType) {
return payloadType === ARRAY ? [].concat(_toConsumableArray(payload)) : _extends({}, payload);
}
if (stateType === ARRAY) {
var _newState = new Array(payload.length);
var _i = payload.length;
while (_i--) {
var item = state[_i];
if ((typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object') {
_newState[_i] = item || payload[_i];
} else {
_newState[_i] = payload[_i];
}
}
return _newState;
}
var newState = _extends({}, state, payload);
var keys = Object.keys(state);
var i = -1;
var l = keys.length;
while (++i < l) {
var key = keys[i];
var _item = state[key];
if ((typeof _item === 'undefined' ? 'undefined' : _typeof(_item)) === 'object') {
newState[key] = _item;
}
}
return newState;
}
}, {
key: 'SilentAction',
value: function SilentAction(name, payload, context) {
var _this = this;
var action = new _Action3.default(name, payload, context, false);
action.Reducer = function () {
return _this;
};
return action;
}
/**
* @name Action
* @description This is the way the system creates Actions.
* @param {*} name
* @param {*} payload
* @param {*} context
*/
}, {
key: 'Action',
value: function Action(name, payload, context) {
var _this2 = this;
var action = new _Action3.default(name, payload, context);
action.Reducer = function () {
return _this2;
};
return action;
}
/**
* @name Dispatch
* @description Adds events to a list of events that gets triggered once the store has fully updated.
* @param {String} eventName
*/
}, {
key: 'Dispatch',
value: function Dispatch(eventName) {
this.hermes.AddEvent(eventName);
}
}]);
return Reducer;
}();
Reducer.EVENTS = {
CHANGE: 'reducer.change'
};
exports.default = Reducer;
//# sourceMappingURL=Reducer.js.map