UNPKG

awv3

Version:
92 lines (79 loc) 2.4 kB
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _extends from "@babel/runtime/helpers/extends"; import omitBy from 'lodash/omitBy'; import isObject from 'lodash/isObject'; import { mixin } from '../lifecycle'; var scope = 'elements'; export var base = mixin(scope, element); export var types = _extends({}, base.types, { addChild: scope + "/addChild", removeChild: scope + "/removeChild", removeAllChilds: scope + "/removeAllChilds", event: scope + "/event" }); var actions = _extends({}, base.actions, { addChild: function addChild(id, child, from) { if (from === void 0) { from = 'children'; } return { type: types.addChild, id: id, child: child, from: from }; }, removeChild: function removeChild(id, child, from) { if (from === void 0) { from = 'children'; } return { type: types.removeChild, id: id, child: child, from: from }; }, removeAllChilds: function removeAllChilds(id, from) { if (from === void 0) { from = 'children'; } return { type: types.removeAllChilds, id: id, from: from }; }, event: function event(id, _event) { return { type: types.event, id: id, event: omitBy(_event, isObject) }; } }); export { actions }; function element(state, _ref) { var _extends2, _extends3, _extends4; var type = _ref.type, payload = _objectWithoutProperties(_ref, ["type"]); switch (type) { case types.addChild: return _extends({}, state, (_extends2 = {}, _extends2[payload.from] = state[payload.from].concat([payload.child]), _extends2)); case types.removeChild: return _extends({}, state, (_extends3 = {}, _extends3[payload.from] = state[payload.from].filter(function (item) { return item !== payload.child; }), _extends3)); case types.removeAllChilds: // It's worth optimizing the already-empty case to preserve referential equality // because removeAllChilds is often called idempotently return state[payload.from].length === 0 ? state : _extends({}, state, (_extends4 = {}, _extends4[payload.from] = [], _extends4)); case types.event: return _extends({}, state, { lastEvent: payload.event }); default: return state; } } export var reducer = base.reducer;