@ngxs-labs/entity-state
Version:
<p align="center"> <img src="https://raw.githubusercontent.com/ngxs-labs/emitter/master/docs/assets/logo.png"> </p>
16 lines (14 loc) • 14 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@ngxs/store"),require("@ngxs/store/operators")):"function"==typeof define&&define.amd?define("@ngxs-labs/entity-state",["exports","@ngxs/store","@ngxs/store/operators"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self)["ngxs-labs"]=t["ngxs-labs"]||{},t["ngxs-labs"]["entity-state"]={}),t["ngxs-store"],t.operators)}(this,(function(t,e,n){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var r=function(t,e){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};function i(t,e){function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}Object.create;function o(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,o=n.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)a.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return a}function a(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(o(arguments[e]));return t}Object.create;var c,u=function(t){function e(e){return t.call(this,e)||this}return i(e,t),e}(Error),s=function(t){function e(e){return void 0===e&&(e=""),t.call(this,("No active entity to affect. "+e).trim())||this}return i(e,t),e}(u),f=function(t){function e(e){return t.call(this,"No entity for ID "+e)||this}return i(e,t),e}(u),p=function(t){function e(e){return t.call(this,"Invalid ID: "+e)||this}return i(e,t),e}(u),d=function(t){function e(){return t.call(this,"idOf returned undefined")||this}return i(e,t),e}(u),l=function(t){function e(e){return t.call(this,"Updating entity failed.\n\tCause: "+e)||this}return i(e,t),e}(u),y=function(t){function e(e){return t.call(this,"Unable to generate an ID.\n\tCause: "+e)||this}return i(e,t),e}(u),v=function(t){function e(e){return t.call(this,"Cannot use "+e+" as EntitySelector")||this}return i(e,t),e}(u);function g(t,e,n){var r=e.NGXS_META.path,i=new function(t){this.payload=t}(n);return Reflect.getPrototypeOf(i).constructor.type="["+r+"] "+t,i}function h(t){return t.entities[t.active]}function b(t){return Array.isArray(t)?t:[t]}function S(t,e,n,r){return t?e<n?r:e>r?n:e:function(t,e,n){return Math.min(n,Math.max(e,t))}(e,n,r)}(c=t.EntityActionType||(t.EntityActionType={})).Add="add",c.CreateOrReplace="createOrReplace",c.Update="update",c.UpdateAll="updateAll",c.UpdateActive="updateActive",c.Remove="remove",c.RemoveAll="removeAll",c.RemoveActive="removeActive",c.SetLoading="setLoading",c.SetError="setError",c.SetActive="setActive",c.ClearActive="clearActive",c.Reset="reset",c.GoToPage="goToPage",c.SetPageSize="setPageSize";var A=function(e,n){return g(t.EntityActionType.Add,e,n)},O=function(e,n){return g(t.EntityActionType.CreateOrReplace,e,n)},E=function(e,n){return g(t.EntityActionType.Remove,e,n)},m=function(e){return g(t.EntityActionType.RemoveAll,e)},x=function(e,n,r){return g(t.EntityActionType.Update,e,{selector:n,data:r})},j=function(e,n){return g(t.EntityActionType.UpdateAll,e,{data:n})},I=function(e,n){return g(t.EntityActionType.SetLoading,e,n)},T=function(e,n){return g(t.EntityActionType.SetActive,e,n)},w=function(e){return g(t.EntityActionType.ClearActive,e)},P=function(e){return g(t.EntityActionType.RemoveActive,e)},U=function(e,n){return g(t.EntityActionType.UpdateActive,e,n)},G=function(e,n){return g(t.EntityActionType.SetError,e,n)},R=function(e){return g(t.EntityActionType.Reset,e)},D=function(e,n){return g(t.EntityActionType.GoToPage,e,Object.assign({wrap:!1},n))},_=function(e,n){return g(t.EntityActionType.SetPageSize,e,n)};function M(t,e,n){return function(r){var i=Object.assign({},r.entities),o=a(r.ids),c=r;return b(t).forEach((function(t){var r,a=n(t,c),u=t;t[e]!==a&&(u=Object.assign(Object.assign({},t),((r={})[e]=a,r))),i[a]=u,o.includes(a)||o.push(a),c=Object.assign(Object.assign({},c),{entities:i,ids:o})})),Object.assign(Object.assign({},c),{entities:i,ids:o,lastUpdated:Date.now()})}}function N(){return n.patch({lastUpdated:Date.now()})}function C(){return function(t){return Object.assign(Object.assign({},t),{entities:{},ids:[],active:void 0,lastUpdated:Date.now()})}}function z(t){var e=n.patch({entities:L(t),ids:X(t)});return n.compose(e,K(t),N())}function K(t){return function(e){return Object.assign(Object.assign({},e),{active:t.includes(e.active)?void 0:e.active})}}function X(t){return function(e){return e.filter((function(e){return!t.includes(e)}))}}function L(t){return function(e){var n=Object.assign({},e);return t.forEach((function(t){return delete n[t]})),n}}function k(t,e,n){return function(r){var i=Object.assign({},r.entities),o=function(t,e,n){if(null===e)return t;if("function"==typeof e)return t.filter((function(t){return e(t)}));var r=b(e);return t.filter((function(t){return r.includes(t[n])}))}(Object.values(i),t.selector,e);return"function"==typeof t.data?o.forEach((function(r){i=q(i,t.data(r),r[e],n)})):o.forEach((function(r){i=q(i,t.data,r[e],n)})),Object.assign(Object.assign({},r),{entities:i,lastUpdated:Date.now()})}}function F(t,e,n){return function(e){var r=function(t){var e=h(t);if(void 0===e)throw new s;return{id:t.active,active:e}}(e),i=r.id,o=r.active,a=e.entities;return"function"==typeof t?Object.assign(Object.assign({},e),{entities:q(a,t(o),i,n),lastUpdated:Date.now()}):Object.assign(Object.assign({},e),{entities:q(a,t,i,n),lastUpdated:Date.now()})}}function q(t,e,n,r){var i;if(void 0===n)throw new l(new p(n));var o=t[n];if(void 0===o)throw new l(new f(n));var a=r(o,e);return Object.assign(Object.assign({},t),((i={})[n]=a,i))}function B(t){return void 0===t&&(t={}),Object.assign({entities:{},ids:[],loading:!1,error:void 0,active:void 0,pageSize:10,pageIndex:0,lastUpdated:Date.now()},t)}var H=function(){function n(e,n,r){this.idKey=n,this.storePath=e.NGXS_META.path,this.idGenerator=new r(n),this.setup(e,Object.values(t.EntityActionType))}return n.prototype.onUpdate=function(t,e){return Object.assign(Object.assign({},t),e)},Object.defineProperty(n,"activeId",{get:function(){return e.createSelector([this],(function(t){return t.active}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"active",{get:function(){return e.createSelector([this],(function(t){return h(t)}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"keys",{get:function(){return e.createSelector([this],(function(t){return Object.keys(t.entities)}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"entities",{get:function(){return e.createSelector([this],(function(t){return t.ids.map((function(e){return t.entities[e]}))}))},enumerable:!1,configurable:!0}),n.nthEntity=function(t){return e.createSelector([this],(function(e){var n=e.ids[t];return e.entities[n]}))},Object.defineProperty(n,"paginatedEntities",{get:function(){return e.createSelector([this],(function(t){var e=t.ids,n=t.pageIndex,r=t.pageSize;return e.slice(n*r,(n+1)*r).map((function(e){return t.entities[e]}))}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"entitiesMap",{get:function(){return e.createSelector([this],(function(t){return t.entities}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"size",{get:function(){return e.createSelector([this],(function(t){return Object.keys(t.entities).length}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"error",{get:function(){return e.createSelector([this],(function(t){return t.error}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"loading",{get:function(){return e.createSelector([this],(function(t){return t.loading}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"latest",{get:function(){return e.createSelector([this],(function(t){var e=t.ids[t.ids.length-1];return t.entities[e]}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"latestId",{get:function(){return e.createSelector([this],(function(t){return t.ids[t.ids.length-1]}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"lastUpdated",{get:function(){return e.createSelector([this],(function(t){return new Date(t.lastUpdated)}))},enumerable:!1,configurable:!0}),Object.defineProperty(n,"age",{get:function(){return e.createSelector([this],(function(t){return Date.now()-t.lastUpdated}))},enumerable:!1,configurable:!0}),n.prototype.add=function(t,e){var n=this;(0,t.setState)(M(e.payload,this.idKey,(function(t,e){return n.idGenerator.generateId(t,e)})))},n.prototype.createOrReplace=function(t,e){var n=this;(0,t.setState)(M(e.payload,this.idKey,(function(t,e){return n.idGenerator.getPresentIdOrGenerate(t,e)})))},n.prototype.update=function(t,e){var n=this,r=t.setState,i=e.payload;if(null==i.selector)throw new v(i);r(k(i,this.idKey,(function(t,e){return n.onUpdate(t,e)})))},n.prototype.updateAll=function(t,e){var n=this,r=t.setState,i=e.payload;r(k(Object.assign(Object.assign({},i),{selector:null}),this.idKey,(function(t,e){return n.onUpdate(t,e)})))},n.prototype.updateActive=function(t,e){var n=this;(0,t.setState)(F(e.payload,this.idKey,(function(t,e){return n.onUpdate(t,e)})))},n.prototype.removeActive=function(t){var e=t.getState;(0,t.setState)(z([e().active]))},n.prototype.remove=function(t,e){var n=this,r=t.getState,i=t.setState,o=(t.patchState,e.payload);if(null===o)throw new v(o);i(z("function"==typeof o?Object.values(r().entities).filter((function(t){return o(t)})).map((function(t){return n.idOf(t)})):b(o)))},n.prototype.removeAll=function(t){(0,t.setState)(C())},n.prototype.reset=function(t){(0,t.setState)(B())},n.prototype.setLoading=function(t,e){(0,t.patchState)({loading:e.payload})},n.prototype.setActive=function(t,e){(0,t.patchState)({active:e.payload})},n.prototype.clearActive=function(t){(0,t.patchState)({active:void 0})},n.prototype.setError=function(t,e){(0,t.patchState)({error:e.payload})},n.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 o=n(),a=o.pageSize,c=o.pageIndex,u=o.ids.length,s=Math.floor(u/a);if("last"in i)r({pageIndex:s});else{var f=c+(i.prev?-1:1);r({pageIndex:f=S(i.wrap,f,0,s)})}}},n.prototype.setPageSize=function(t,e){(0,t.patchState)({pageSize:e.payload})},n.prototype.setup=function(t,e){var n=this;e.forEach((function(e){var r="["+n.storePath+"] "+e;t.NGXS_META.actions[r]=[{fn:e,options:{},type:r}]}))},n.prototype.idOf=function(t){return t[this.idKey]},n}();!function(t){var e=function(){function t(t){this.idKey=t}return t.prototype.isIdInState=function(t,e){return e.ids.includes(t)},t.prototype.getPresentIdOrGenerate=function(t,e){var n=this.getIdOf(t);return void 0===n?this.generateId(t,e):n},t.prototype.mustGetIdOf=function(t){var e=this.getIdOf(t);if(void 0===e)throw new d;return e},t.prototype.getIdOf=function(t){return t[this.idKey]},t}();t.IdGenerator=e;var n=function(t){function e(e){return t.call(this,e)||this}return i(e,t),e.prototype.generateId=function(t,e){return(Math.max.apply(Math,a([-1],e.ids.map((function(t){return parseInt(t,10)}))))+1).toString(10)},e}(e);t.IncrementingIdGenerator=n;var r=function(t){function e(e){return t.call(this,e)||this}return i(e,t),e.prototype.generateId=function(t,e){var n;do{n=this.uuidv4()}while(this.isIdInState(n,e));return n},e.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)}))},e}(e);t.UUIDGenerator=r;var o=function(t){function e(e){return t.call(this,e)||this}return i(e,t),e.prototype.generateId=function(t,e){var n=this.mustGetIdOf(t);if(this.isIdInState(n,e))throw new y("The provided ID already exists: "+n);return n},e}(e);t.EntityIdGenerator=o}(t.IdStrategy||(t.IdStrategy={}));t.Add=A,t.ClearActive=w,t.CreateOrReplace=O,t.EntityState=H,t.EntityStateError=u,t.GoToPage=D,t.InvalidEntitySelectorError=v,t.InvalidIdError=p,t.InvalidIdOfError=d,t.NoActiveEntityError=s,t.NoSuchEntityError=f,t.Remove=E,t.RemoveActive=P,t.RemoveAll=m,t.Reset=R,t.SetActive=T,t.SetError=G,t.SetLoading=I,t.SetPageSize=_,t.UnableToGenerateIdError=y,t.Update=x,t.UpdateActive=U,t.UpdateAll=j,t.UpdateFailedError=l,t.addOrReplace=M,t.alsoUpdateTimestamp=function(t){return n.compose(N(),t)},t.clearActiveIfRemoved=K,t.defaultEntityState=B,t.ofEntityAction=function(t,n){var r="["+t.NGXS_META.path+"] "+n;return e.ofAction({type:r})},t.ofEntityActionCompleted=function(t,n){var r="["+t.NGXS_META.path+"] "+n;return e.ofActionCompleted({type:r})},t.ofEntityActionDispatched=function(t,n){var r="["+t.NGXS_META.path+"] "+n;return e.ofActionDispatched({type:r})},t.ofEntityActionErrored=function(t,n){var r="["+t.NGXS_META.path+"] "+n;return e.ofActionErrored({type:r})},t.ofEntityActionSuccessful=function(t,n){var r="["+t.NGXS_META.path+"] "+n;return e.ofActionSuccessful({type:r})},t.removeAllEntities=C,t.removeEntities=z,t.removeEntitiesFromArray=X,t.removeEntitiesFromDictionary=L,t.update=k,t.updateActive=F,t.updateDictionary=q,t.updateTimestamp=N,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=ngxs-labs-entity-state.umd.min.js.map