awv3
Version:
⚡ AWV3 embedded CAD
258 lines (213 loc) • 8.22 kB
JavaScript
import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/taggedTemplateLiteralLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteralLoose(["Lifecycle object is missing a session"], ["Lifecycle object is missing a session"]),
_templateObject2 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["Lifecycle object is missing link to actions"], ["Lifecycle object is missing link to actions"]),
_templateObject3 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["Lifecycle object is missing a selector"], ["Lifecycle object is missing a selector"]);
import v4 from 'uuid-v4';
import omit from 'lodash/omit';
import cloneDeep from 'lodash/cloneDeep';
import { errUndefined } from '../core/helpers';
import { createObserver } from './helpers'; // Store mixin which creates four actions: register(id, props), unregister(id), update(id, props) & patch(props)
var mixin = function mixin(name, subReducer) {
var references = {};
var types = {
register: name + "/REGISTER",
registerQueue: name + "/REGISTERQUEUE",
unregister: name + "/UNREGISTER",
update: name + "/UPDATE",
merge: name + "/MERGE"
};
return {
references: references,
types: types,
actions: {
register: function register(reference, props) {
return function (dispatch) {
// Store references to the actual lifecycle class making the register call
references[reference.id] = reference;
dispatch({
type: types.register,
id: reference.id,
props: props
});
};
},
unregister: function unregister(ids) {
return function (dispatch) {
var objects = Array.isArray(ids) ? ids : [ids];
dispatch({
type: types.unregister,
ids: ids
});
objects.forEach(function (id) {
// Destroy & remove reference
if (references[id]) {
references[id].destroy(false);
delete references[id];
}
});
};
},
update: function update(id, props) {
return {
type: types.update,
id: id,
props: props
};
},
merge: function merge(props) {
return {
type: types.merge,
props: props
};
}
},
reducer: function reducer(state, _ref) {
var _extends2, _extends3;
if (state === void 0) {
state = {};
}
var type = _ref.type,
id = _ref.id,
payload = _objectWithoutProperties(_ref, ["type", "id"]);
switch (type) {
case types.register:
return _extends({}, state, (_extends2 = {}, _extends2[id] = _extends({
id: id
}, payload.props), _extends2));
case types.unregister:
return omit(state, payload.ids);
case types.update:
return _extends({}, state, (_extends3 = {}, _extends3[id] = _extends({}, state[id], payload.props), _extends3));
case types.merge:
return Object.keys(payload.props).reduce(function (prev, key) {
var _extends4;
return _extends({}, prev, (_extends4 = {}, _extends4[key] = _extends({
id: key
}, prev[key], payload.props[key]), _extends4));
}, state);
default:
if (payload && id && state[id]) {
var _extends5;
return _extends({}, state, (_extends5 = {}, _extends5[id] = subReducer(state[id], _extends({
type: type
}, payload)), _extends5));
}
}
return state;
}
};
}; // Base for all objects tied to the store. Lifecycle takes care of the initial
// register, watches for removal, manages child-relations and observers
// Lifecycle eligiable objects must have store actions that expose:
// 1. register(id, props) method
// 2. unregister(id) method
// 3. update(id, props) method
export { mixin };
export default (function (Base, arg) {
if (Base === void 0) {
Base = function Base() {};
}
return (
/*#__PURE__*/
function (_Base) {
_inheritsLoose(Lifecycle, _Base);
function Lifecycle(session, actions, selector, props) {
var _this;
if (session === void 0) {
session = errUndefined(_templateObject);
}
if (actions === void 0) {
actions = errUndefined(_templateObject2);
}
if (selector === void 0) {
selector = errUndefined(_templateObject3);
}
_this = _Base.call(this, arg) || this;
_this.id = v4();
_this.session = session;
_this.store = session.store;
_this.actions = actions;
_this.getState = function () {
return selector(_this.store.getState());
};
_this.subscriptions = new Set();
_this.dependencies = []; // Register object and store original properties
_this.store.dispatch(actions.register(_assertThisInitialized(_this), props));
_this.props = cloneDeep(props); // Create managed observer
var observer = createObserver(_this.store, _this.getState);
_this.observe = function (selector, callback, options) {
return observer(selector, callback, _extends({
manager: _this.managed && _this.addSubscription.bind(_assertThisInitialized(_this))
}, options));
}; // Make props available on the object with automatic setters/getters
var _loop = function _loop(prop) {
!_this[prop] && Object.defineProperty(_assertThisInitialized(_this), prop, {
configurable: true,
get: function get() {
var state = _this.getState();
return state && state[prop];
},
set: function set(value) {
var _actions$update;
return _this.store.dispatch(actions.update(_this.id, (_actions$update = {}, _actions$update[prop] = value, _actions$update)));
}
});
};
for (var prop in props) {
_loop(prop);
}
return _this;
}
var _proto = Lifecycle.prototype;
_proto.update = function update(props) {
this.store.dispatch(this.actions.update(this.id, props));
};
_proto.merge = function merge(props) {
this.store.dispatch(this.actions.merge(this.id, props));
};
_proto.reset = function reset() {
this.store.dispatch(this.actions.update(this.id, this.props));
};
_proto.destroy = function destroy(dispatchUnregister) {
if (dispatchUnregister === void 0) {
dispatchUnregister = true;
}
if (dispatchUnregister) {
this.store.dispatch(this.actions.unregister(this.id));
} else {
this.removeSubscriptions();
this.willBeDestroyed && this.willBeDestroyed();
this.__onDestroyed && this.__onDestroyed();
this.onDestroyed();
this.dependencies.slice().forEach(function (child) {
return child.destroy();
});
this.dependencies = [];
}
};
_proto.addSubscription = function addSubscription(unsubscribe) {
this.subscriptions.add(unsubscribe);
return unsubscribe;
};
_proto.removeSubscription = function removeSubscription(unsubscribe) {
unsubscribe();
this.subscriptions.delete(unsubscribe);
};
_proto.removeSubscriptions = function removeSubscriptions() {
this.subscriptions.forEach(function (unsubscribe) {
return unsubscribe();
});
this.subscriptions.clear();
this.dependencies.forEach(function (child) {
return child.removeSubscriptions();
});
};
_proto.onDestroyed = function onDestroyed() {};
return Lifecycle;
}(Base)
);
});