UNPKG

@ngxs-labs/entity-state

Version:

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.0.5.

2 lines 11.5 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@ngxs/store")):"function"==typeof define&&define.amd?define("@ngxs-labs/entity-state",["exports","@ngxs/store"],e):e((t["ngxs-labs"]=t["ngxs-labs"]||{},t["ngxs-labs"]["entity-state"]={}),t["ngxs-store"])}(this,function(t,r){"use strict";var i=function(t,e){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function p(t,e){function n(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var l=function(){return(l=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function n(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,a=n.call(t),o=[];try{for(;(void 0===e||0<e--)&&!(r=a.next()).done;)o.push(r.value)}catch(u){i={error:u}}finally{try{r&&!r.done&&(n=a["return"])&&n.call(a)}finally{if(i)throw i.error}}return o}function v(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(n(arguments[e]));return t}var e,a=(p(o,e=Error),o);function o(t){return e.call(this,t)||this}var u,s=(p(c,u=a),c);function c(t){return void 0===t&&(t=""),u.call(this,("No active entity to affect. "+t).trim())||this}var f,d=(p(h,f=a),h);function h(t){return f.call(this,"No entity for ID "+t)||this}var y,g=(p(S,y=a),S);function S(t){return y.call(this,"Invalid ID: "+t)||this}var b,x=(p(O,b=a),O);function O(){return b.call(this,"idOf returned undefined")||this}var P,I=(p(m,P=a),m);function m(t){return P.call(this,"Updating entity failed.\n\tCause: "+t)||this}var A,E=(p(w,A=a),w);function w(t){return A.call(this,"Unable to generate an ID.\n\tCause: "+t)||this}var j="NGXS_META";function U(t,e,n){var r=e[j].path,i=new function(t){this.payload=t}(n);return Reflect.getPrototypeOf(i).constructor.type="["+r+"] "+t,i}function _(t){return t.entities[t.active]}function D(t,e){return e?e.split(".").reduce(function(t,e){return t&&t[e]},t):t}function R(t){return Array.isArray(t)?t:[t]}function G(t,e,n,r){return t?e<n?r:r<e?n:e:function i(t,e,n){return Math.min(n,Math.max(e,t))}(e,n,r)}function z(t,e){return U(J.Add,t,e)}function C(t,e){return U(J.CreateOrReplace,t,e)}function M(t,e){return U(J.Remove,t,e)}function T(t,e,n){return U(J.Update,t,{id:e,data:n})}function K(t,e){return U(J.SetLoading,t,e)}function L(t,e){return U(J.SetActive,t,e)}function N(t){return U(J.ClearActive,t)}function k(t){return U(J.RemoveActive,t)}function q(t,e){return U(J.UpdateActive,t,e)}function F(t,e){return U(J.SetError,t,e)}function X(t){return U(J.Reset,t)}function B(t,e){return U(J.GoToPage,t,l({wrap:!1},e))}function H(t,e){return U(J.SetPageSize,t,e)}var J={Add:"add",CreateOrReplace:"createOrReplace",Update:"update",UpdateActive:"updateActive",Remove:"remove",RemoveActive:"removeActive",SetLoading:"setLoading",SetError:"setError",SetActive:"setActive",ClearActive:"clearActive",Reset:"reset",GoToPage:"goToPage",SetPageSize:"setPageSize"};function Q(t){return void 0===t&&(t={}),l({entities:{},ids:[],loading:!1,error:undefined,active:undefined,pageSize:10,pageIndex:0,lastUpdated:Date.now()},t)}var V=(Object.defineProperty(W,"staticStorePath",{get:function(){return this[j].path},enumerable:!0,configurable:!0}),W.prototype.onUpdate=function(t,e){return l({},t,e)},Object.defineProperty(W,"activeId",{get:function(){var e=this;return function(t){return D(t,e.staticStorePath).active}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"active",{get:function(){var e=this;return function(t){return _(D(t,e.staticStorePath))}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"keys",{get:function(){var n=this;return function(t){var e=D(t,n.staticStorePath);return Object.keys(e.entities)}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"entities",{get:function(){var n=this;return function(t){var e=D(t,n.staticStorePath);return e.ids.map(function(t){return e.entities[t]})}},enumerable:!0,configurable:!0}),W.nthEntity=function(r){var i=this;return function(t){var e=D(t,i.staticStorePath),n=e.ids[r];return e.entities[n]}},Object.defineProperty(W,"paginatedEntities",{get:function(){var a=this;return function(t){var e=D(t,a.staticStorePath),n=e.ids,r=e.pageIndex,i=e.pageSize;return n.slice(r*i,(r+1)*i).map(function(t){return e.entities[t]})}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"entitiesMap",{get:function(){var e=this;return function(t){return D(t,e.staticStorePath).entities}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"size",{get:function(){var n=this;return function(t){var e=D(t,n.staticStorePath);return Object.keys(e.entities).length}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"error",{get:function(){var e=this;return function(t){return D(t,e.staticStorePath).error}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"loading",{get:function(){var e=this;return function(t){return D(t,e.staticStorePath).loading}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"latest",{get:function(){var r=this;return function(t){var e=D(t,r.staticStorePath),n=e.ids[e.ids.length-1];return e.entities[n]}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"latestId",{get:function(){var n=this;return function(t){var e=D(t,n.staticStorePath);return e.ids[e.ids.length-1]}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"lastUpdated",{get:function(){var n=this;return function(t){var e=D(t,n.staticStorePath);return new Date(e.lastUpdated)}},enumerable:!0,configurable:!0}),Object.defineProperty(W,"age",{get:function(){var n=this;return function(t){var e=D(t,n.staticStorePath);return Date.now()-e.lastUpdated}},enumerable:!0,configurable:!0}),W.prototype.add=function(t,e){var n=this,r=t.getState,i=t.patchState,a=e.payload,o=this._addOrReplace(r(),a,function(t,e){return n.idGenerator.generateId(t,e)});i(l({},o,{lastUpdated:Date.now()}))},W.prototype.createOrReplace=function(t,e){var n=this,r=t.getState,i=t.patchState,a=e.payload,o=this._addOrReplace(r(),a,function(t,e){return n.idGenerator.getPresentIdOrGenerate(t,e)});i(l({},o,{lastUpdated:Date.now()}))},W.prototype.update=function(t,e){var n,r=this,i=t.getState,a=t.patchState,o=e.payload,u=l({},i().entities);if(null===o.id)n=Object.values(u);else if("function"==typeof o.id)n=Object.values(u).filter(function(t){return o.id(t)});else{var c=R(o.id);n=Object.values(u).filter(function(t){return c.includes(r.idOf(t))})}"function"==typeof o.data?n.forEach(function(t){u=r._update(u,o.data(t),r.idOf(t))}):n.forEach(function(t){u=r._update(u,o.data,r.idOf(t))}),a({entities:u,lastUpdated:Date.now()})},W.prototype.updateActive=function(t,e){var n=t.getState,r=t.patchState,i=e.payload,a=n(),o=function d(t){var e=_(t);if(e===undefined)throw new s;return{id:t.active,active:e}}(a),u=o.id,c=o.active,f=a.entities;r("function"==typeof i?{entities:l({},this._update(f,i(c),u)),lastUpdated:Date.now()}:{entities:l({},this._update(f,i,u)),lastUpdated:Date.now()})},W.prototype.removeActive=function(t){var e=t.getState,n=t.patchState,r=e(),i=r.entities,a=r.ids,o=r.active;delete i[o],n({entities:l({},i),ids:a.filter(function(t){return t!==o}),active:undefined,lastUpdated:Date.now()})},W.prototype.remove=function(t,e){var n=this,r=t.getState,i=t.patchState,a=e.payload,o=r(),u=o.entities,c=o.ids,f=o.active;if(null===a)i({entities:{},ids:[],active:undefined,lastUpdated:Date.now()});else{var d="function"==typeof a?Object.values(u).filter(function(t){return a(t)}).map(function(t){return n.idOf(t)}):R(a),s=d.includes(f);d.forEach(function(t){return delete u[t]}),i({entities:l({},u),ids:c.filter(function(t){return!d.includes(t)}),active:s?undefined:f,lastUpdated:Date.now()})}},W.prototype.reset=function(t){(0,t.setState)(Q())},W.prototype.setLoading=function(t,e){(0,t.patchState)({loading:e.payload})},W.prototype.setActive=function(t,e){(0,t.patchState)({active:e.payload})},W.prototype.clearActive=function(t){(0,t.patchState)({active:undefined})},W.prototype.setError=function(t,e){(0,t.patchState)({error:e.payload})},W.prototype.goToPage=function(t,e){var n=t.getState,r=t.patchState,i=e.payload;if("page"in i)r({pageIndex:i.page});else if(i.first)r({pageIndex:0});else{var a=n(),o=a.pageSize,u=a.pageIndex,c=a.ids.length,f=Math.floor(c/o);if("last"in i)r({pageIndex:f});else{var d=u+(i.prev?-1:1);r({pageIndex:d=G(i.wrap,d,0,f)})}}},W.prototype.setPageSize=function(t,e){(0,t.patchState)({pageSize:e.payload})},W.prototype._addOrReplace=function(n,t,r){var i=this,a=n.entities,o=n.ids;return R(t).forEach(function(t){var e=r(t,n);t[i.idKey]=e,a[e]=t,o.includes(e)||o.push(e)}),{entities:l({},a),ids:v(o)}},W.prototype._update=function(t,e,n){if(void 0===n&&(n=this.idOf(e)),n===undefined)throw new I(new g(n));var r=t[n];if(r===undefined)throw new I(new d(n));return t[n]=this.onUpdate(r,e),t},W.prototype.setup=function(n,t){var r=this;t.forEach(function(t){var e="["+r.storePath+"] "+t;n[j].actions[e]=[{fn:t,options:{},type:e}]})},W.prototype.idOf=function(t){return t[this.idKey]},W);function W(t,e,n){this.idKey=e,this.storePath=t[j].path,this.idGenerator=new n(e),this.setup(t,Object.values(J))}!function(t){var e=(n.prototype.isIdInState=function(t,e){return e.ids.includes(t)},n.prototype.getPresentIdOrGenerate=function(t,e){var n=this.getIdOf(t);return n===undefined?this.generateId(t,e):n},n.prototype.mustGetIdOf=function(t){var e=this.getIdOf(t);if(e===undefined)throw new x;return e},n.prototype.getIdOf=function(t){return t[this.idKey]},n);function n(t){this.idKey=t}t.IdGenerator=e;var r,i=(p(a,r=e),a.prototype.generateId=function(t,e){return(Math.max.apply(Math,v([-1],e.ids.map(function(t){return parseInt(t,10)})))+1).toString(10)},a);function a(t){return r.call(this,t)||this}t.IncrementingIdGenerator=i;var o,u=(p(c,o=e),c.prototype.generateId=function(t,e){for(var n;n=this.uuidv4(),this.isIdInState(n,e););return n},c.prototype.uuidv4=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var e=16*Math.random()|0;return("x"===t?e:3&e|8).toString(16)})},c);function c(t){return o.call(this,t)||this}t.UUIDGenerator=u;var f,d=(p(s,f=e),s.prototype.generateId=function(t,e){var n=this.mustGetIdOf(t);if(this.isIdInState(n,e))throw new E("The provided ID already exists: "+n);return n},s);function s(t){return f.call(this,t)||this}t.EntityIdGenerator=d}(t.IdStrategy||(t.IdStrategy={}));t.Add=z,t.CreateOrReplace=C,t.Remove=M,t.Update=T,t.SetLoading=K,t.SetActive=L,t.ClearActive=N,t.RemoveActive=k,t.UpdateActive=q,t.SetError=F,t.Reset=X,t.EntityActionType=J,t.GoToPage=B,t.SetPageSize=H,t.defaultEntityState=Q,t.EntityState=V,t.EntityStateError=a,t.NoActiveEntityError=s,t.NoSuchEntityError=d,t.InvalidIdError=g,t.InvalidIdOfError=x,t.UpdateFailedError=I,t.UnableToGenerateIdError=E,t.ofEntityAction=function(t,e){var n="["+t[j].path+"] "+e;return r.ofAction({type:n})},t.ofEntityActionDispatched=function(t,e){var n="["+t[j].path+"] "+e;return r.ofActionDispatched({type:n})},t.ofEntityActionSuccessful=function(t,e){var n="["+t[j].path+"] "+e;return r.ofActionSuccessful({type:n})},t.ofEntityActionErrored=function(t,e){var n="["+t[j].path+"] "+e;return r.ofActionErrored({type:n})},t.ofEntityActionCompleted=function(t,e){var n="["+t[j].path+"] "+e;return r.ofActionCompleted({type:n})},Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=ngxs-labs-entity-state.umd.min.js.map