@mic-rexjs/usecases-react 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +1 -1
- package/es/hooks/useEntity/index.js +9 -8
- package/es/hooks/useRuntimeEntity/index.d.ts +2 -1
- package/es/hooks/useRuntimeEntity/index.js +2 -2
- package/es/hooks/useUseCase/index.test.js +8 -5
- package/lib/hooks/useEntity/index.js +7 -11
- package/lib/hooks/useRuntimeEntity/index.js +2 -2
- package/lib/hooks/useUseCase/index.test.js +10 -5
- package/package.json +3 -3
package/dist/index.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see index.min.js.LICENSE.txt */
|
|
2
|
-
(()=>{var t={20:(t,e,n)=>{"use strict";var r=n(594),o=Symbol.for("react.element"),u=(Symbol.for("react.fragment"),Object.prototype.hasOwnProperty),a=r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,i={key:!0,ref:!0,__self:!0,__source:!0};e.jsx=function(t,e,n){var r,c={},l=null,f=null;for(r in void 0!==n&&(l=""+n),void 0!==e.key&&(l=""+e.key),void 0!==e.ref&&(f=e.ref),e)u.call(e,r)&&!i.hasOwnProperty(r)&&(c[r]=e[r]);if(t&&t.defaultProps)for(r in e=t.defaultProps)void 0===c[r]&&(c[r]=e[r]);return{$$typeof:o,type:t,key:l,ref:f,props:c,_owner:a.current}}},848:(t,e,n)=>{"use strict";t.exports=n(20)},594:t=>{"use strict";t.exports=React}},e={};function n(r){var o=e[r];if(void 0!==o)return o.exports;var u=e[r]={exports:{}};return t[r](u,u.exports,n),u.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),n.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var r={};(()=>{"use strict";n.r(r),n.d(r,{UseCaseFragment:()=>x,useUseCase:()=>_});var t=n(848),e=n(594),o=n.n(e);function u(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function a(t,e){if(t){if("string"==typeof t)return u(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(t,e):void 0}}function i(t){return function(t){if(Array.isArray(t))return u(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||a(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=n){var r,o,u,a,i=[],c=!0,l=!1;try{if(u=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;c=!1}else for(;!(c=(r=u.call(n)).done)&&(i.push(r.value),i.length!==e);c=!0);}catch(t){l=!0,o=t}finally{try{if(!c&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(l)throw o}}return i}}(t,e)||a(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}const l=ahooks;var f,s=function(t){return c((0,e.useState)((function(){return t})),1)[0]};function y(t){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},y(t)}function v(t){var e=function(t,e){if("object"!=y(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,"string");if("object"!=y(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==y(e)?e:String(e)}!function(t){t[t.None=0]="None",t[t.RootEnabled=2]="RootEnabled",t[t.ContextEnabled=4]="ContextEnabled",t[t.EntityEnabled=8]="EntityEnabled",t[t.ContextRootEnabled=22]="ContextRootEnabled",t[t.EntityRootEnabled=62]="EntityRootEnabled"}(f||(f={}));const d=usecases;var b=["watch","onChange","options"];function p(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function E(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?p(Object(n),!0).forEach((function(e){var r,o,u;r=t,o=e,u=n[e],(o=v(o))in r?Object.defineProperty(r,o,{value:u,enumerable:!0,configurable:!0,writable:!0}):r[o]=u})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):p(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}var h,m=function(t){return c((0,e.useState)(t),1)[0]},g=new Map;!function(t){t[t.None=0]="None",t[t.Entity=2]="Entity"}(h||(h={}));var O=(0,e.createContext)(null),w=function(t,e){var n=t.length;if(n!==e.length)return!1;for(var r=0;r<n;r++)if(t[r]!==e[r])return!1;return!0},j=function(t,e){var n,r=e||{},o=r.onShouldCache,u=void 0===o?function(){return!0}:o,a=r.onCompare,i=void 0===a?function(t,e){return w(t,e)}:a;return function(){for(var e=u(),r=arguments.length,o=new Array(r),a=0;a<r;a++)o[a]=arguments[a];if(n&&e){var c=n,l=c.parameters,f=c.returnValue;if(i(l,o))return f}var s=t.apply(void 0,o);return e&&(n={parameters:o,returnValue:s}),s}},S=function(t,e,n){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,o=(0,l.useLatest)(n),u=(0,l.useCreation)((function(){return j(o.current,{onCompare:function(){return!0}})}),[r,o]);return(0,l.useCreation)((function(){return(e&f.RootEnabled)!==f.RootEnabled&&t?t:u()}),[e,t,u])},C=function(t,e){return"object"===y(t)&&null!==t&&Object.hasOwn(t,e)},R=function t(e,n,r,o){var u=arguments.length>4&&void 0!==arguments[4]?arguments[4]:[],a=void 0,l=c(r.match(/^([^.]+)\.(.+)$/)||[],3),f=l[1],s=void 0===f?r:f,y=l[2],v=void 0===y?"":y,d=C(e,s),b=C(n,s),p=[].concat(i(u),[s]);if(d||b){var E=d?e[s]:a,h=b?n[s]:a;if(v)return void t(E,h,v,o,p);if(E===h)return;o(E,h,p)}else if(!/^\d+^/.test(s))for(var m=Array.isArray(e)?e:[],g=Array.isArray(n)?n:[],O=m.length,w=g.length,j=Math.max(O,w),S=0;S<j;S++)t(m[S],g[S],r,o,[].concat(i(u),["".concat(S)]))},P=function(t,e,n){for(var r=function(){var t=c(u[o],2),r=t[0],a=t[1];R(n,e,r,(function(t,r,o){a({fieldPaths:o,newEntity:e,oldEntity:n,newValue:r,oldValue:t})}))},o=0,u=Object.entries(t);o<u.length;o++)r()},_=function(){for(var t,n=((t=(0,e.useRef)(!0)).current=!0,(0,e.useEffect)((function(){t.current=!1})),t),r=arguments.length,u=new Array(r),a=0;a<r;a++)u[a]=arguments[a];var v=function(t){return m((function(){var e=h.None;return"function"===y(c(t,2)[1])&&(e|=h.Entity),e}))}(u),p=function(t,e){var n=c(t,4),r=n[0],o=n[1],u=n[2],a=n[3];return(e&h.Entity)===h.Entity?[r,o,u||{},a||[]]:[null,r,o||{},u||[]]}(u,v),C=c(p,4),R=C[0],_=C[1],x=C[2],A=C[3],N=s(_),I=N,M=N,k=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:h.None,r=g,o=m((function(){if(r.has(t)){var o=r.get(t),u=o.value,a=o.times;return r.set(t,{value:u,times:a+1}),u}if((n&h.Entity)!==h.Entity)return O;var i=(0,e.createContext)(null);return r.set(t,{value:i,times:1}),i}));return(0,l.useUnmount)((function(){if(r.has(t)){var e=r.get(t),n=e.value,u=e.times;o===n&&(1!==u?r.set(t,{value:n,times:u-1}):r.delete(t))}})),o}(N,v),T=(0,e.useContext)(k),L=T,D=function(t,e){return(0,l.useCreation)((function(){var n=f.None,r=(e||{}).store;return(t&h.Entity)===h.Entity&&(n|=f.EntityRootEnabled),n|=e?f.ContextEnabled:f.RootEnabled,r&&(n|=f.EntityEnabled),n}),[t,e])}(v,T),U=(0,l.useLatest)(x),V=function(t){var n=(0,e.useRef)(0),r=(0,e.useRef)([]),o=r.current,u=w(t,o);return r.current=t,n.current+=u?0:1,n.current}(A),$=(T||{}).reducers,F=void 0===$?null:$,B=(L||{}).store,G=c(function(t,n,r,o,u){var a=c((0,e.useState)(n),2),i=a[0],s=a[1],y=function(t,n,r,o){var u=(0,e.useRef)(r);if((t&f.EntityRootEnabled)!==f.EntityRootEnabled)return o;var a="function"==typeof r,i=u.current;switch(!0){case a:case i===r:return n}return u.current=r,r}(t,i,n,r?r.value:null),v=(t&f.EntityEnabled)===f.EntityEnabled,b=(t&f.EntityRootEnabled)===f.EntityRootEnabled,p=(0,l.useLatest)(o),E=S(r,t,(function(){return b?new d.EntityStore(y,{onChange:function(t){s(t)}}):new d.EntityStore(null)}));return b&&(E.value=y),(0,e.useEffect)((function(){if(v){var t=function(t,e){var n=p.current,r=n.onChange,o=n.watch;o&&P(o,t,e),null==r||r(t,e)};return E.watch(t),function(){E.unwatch(t)}}}),[v,E,u,p]),(0,l.useCreation)((function(){return[y,E]}),[y,E])}(D,R,void 0===B?null:B,x,V),2),W=G[0],Y=G[1],q=S(F,D,(function(){var t,e,n,r=(t=x,e=function(t,e){var n,r;return null===(n=(r=U.current)[t])||void 0===n?void 0:n.call.apply(n,[r].concat(i(e)))},n={},Object.keys(t).forEach((function(r){var o=t[r];n[r]="function"!=typeof o?o:function(){for(var t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];return e(r,n)}})),n);return(D&f.EntityEnabled)!==f.EntityEnabled?I(r):function(t,e,n){n.watch,n.onChange;var r=n.options,o=function(t,e){if(null==t)return{};var n,r,o=function(t,e){if(null==t)return{};var n,r,o={},u=Object.keys(t);for(r=0;r<u.length;r++)n=u[r],e.indexOf(n)>=0||(o[n]=t[n]);return o}(t,e);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(t);for(r=0;r<u.length;r++)n=u[r],e.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(t,n)&&(o[n]=t[n])}return o}(n,b);return(0,d.createEntityReducers)(e,t,E(E(E({},o),r),{},{onGenerate:function(t,e){return e}}))}(M,Y,r)}),V),z=function(t,n,r,u){var a=r.value,i=(0,e.useMemo)((function(){return(t&f.EntityEnabled)===f.EntityEnabled?{store:r,reducers:u,statuses:t}:{reducers:u,statuses:t}}),[t,a,r,u]),c=(0,l.useLatest)(i);return s((function(t){var e=t.children,r=t.with,u=void 0===r?[]:r,a=n.Provider;return o().createElement(a,{value:c.current},u.reduceRight((function(t,e){return o().createElement(e,{},t)}),e))}))}(D,k,Y,q);return(0,l.useCreation)((function(){if((D&f.EntityEnabled)!==f.EntityEnabled)return q;var t=(D&f.RootEnabled)===f.RootEnabled,e=function(t,e){var n={};return Object.entries(t).forEach((function(t){var r=c(t,2),o=r[0],u=r[1];n[o]=j(u,e)})),n}(q,{onShouldCache:function(){return n.current}});return t?[W,e,z]:[W,e]}),[D,W,q,z,n])},x=function(n){var r=n.usecase,o=n.watch,u=n.onChange;return _(r,{watch:o,onChange:u}),(0,t.jsx)(e.Fragment,{})}})();var o=usecases="undefined"==typeof usecases?{}:usecases;for(var u in r)o[u]=r[u];r.__esModule&&Object.defineProperty(o,"__esModule",{value:!0})})();
|
|
2
|
+
(()=>{var t={20:(t,e,n)=>{"use strict";var r=n(594),o=Symbol.for("react.element"),u=(Symbol.for("react.fragment"),Object.prototype.hasOwnProperty),a=r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,i={key:!0,ref:!0,__self:!0,__source:!0};e.jsx=function(t,e,n){var r,c={},l=null,f=null;for(r in void 0!==n&&(l=""+n),void 0!==e.key&&(l=""+e.key),void 0!==e.ref&&(f=e.ref),e)u.call(e,r)&&!i.hasOwnProperty(r)&&(c[r]=e[r]);if(t&&t.defaultProps)for(r in e=t.defaultProps)void 0===c[r]&&(c[r]=e[r]);return{$$typeof:o,type:t,key:l,ref:f,props:c,_owner:a.current}}},848:(t,e,n)=>{"use strict";t.exports=n(20)},594:t=>{"use strict";t.exports=React}},e={};function n(r){var o=e[r];if(void 0!==o)return o.exports;var u=e[r]={exports:{}};return t[r](u,u.exports,n),u.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),n.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var r={};(()=>{"use strict";n.r(r),n.d(r,{UseCaseFragment:()=>x,useUseCase:()=>_});var t=n(848),e=n(594),o=n.n(e);function u(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function a(t,e){if(t){if("string"==typeof t)return u(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(t,e):void 0}}function i(t){return function(t){if(Array.isArray(t))return u(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||a(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=n){var r,o,u,a,i=[],c=!0,l=!1;try{if(u=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;c=!1}else for(;!(c=(r=u.call(n)).done)&&(i.push(r.value),i.length!==e);c=!0);}catch(t){l=!0,o=t}finally{try{if(!c&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(l)throw o}}return i}}(t,e)||a(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}const l=ahooks;var f,s=function(t){return c((0,e.useState)((function(){return t})),1)[0]};function y(t){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},y(t)}function v(t){var e=function(t,e){if("object"!=y(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,"string");if("object"!=y(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==y(e)?e:String(e)}!function(t){t[t.None=0]="None",t[t.RootEnabled=2]="RootEnabled",t[t.ContextEnabled=4]="ContextEnabled",t[t.EntityEnabled=8]="EntityEnabled",t[t.ContextRootEnabled=22]="ContextRootEnabled",t[t.EntityRootEnabled=62]="EntityRootEnabled"}(f||(f={}));const d=usecases;var b=["watch","onChange","options"];function p(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function E(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?p(Object(n),!0).forEach((function(e){var r,o,u;r=t,o=e,u=n[e],(o=v(o))in r?Object.defineProperty(r,o,{value:u,enumerable:!0,configurable:!0,writable:!0}):r[o]=u})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):p(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}var h,m=function(t){return c((0,e.useState)(t),1)[0]},g=new Map;!function(t){t[t.None=0]="None",t[t.Entity=2]="Entity"}(h||(h={}));var O=(0,e.createContext)(null),w=function(t,e){var n=t.length;if(n!==e.length)return!1;for(var r=0;r<n;r++)if(t[r]!==e[r])return!1;return!0},j=function(t,e){var n,r=e||{},o=r.onShouldCache,u=void 0===o?function(){return!0}:o,a=r.onCompare,i=void 0===a?function(t,e){return w(t,e)}:a;return function(){for(var e=u(),r=arguments.length,o=new Array(r),a=0;a<r;a++)o[a]=arguments[a];if(n&&e){var c=n,l=c.parameters,f=c.returnValue;if(i(l,o))return f}var s=t.apply(void 0,o);return e&&(n={parameters:o,returnValue:s}),s}},S=function(t,e,n){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,o=(0,l.useLatest)(n),u=(0,l.useCreation)((function(){return j(o.current,{onCompare:function(){return!0}})}),[r,o]);return(0,l.useCreation)((function(){return(e&f.RootEnabled)!==f.RootEnabled&&t?t:u()}),[e,t,u])},C=function(t,e){return"object"===y(t)&&null!==t&&Object.hasOwn(t,e)},R=function t(e,n,r,o){var u=arguments.length>4&&void 0!==arguments[4]?arguments[4]:[],a=void 0,l=c(r.match(/^([^.]+)\.(.+)$/)||[],3),f=l[1],s=void 0===f?r:f,y=l[2],v=void 0===y?"":y,d=C(e,s),b=C(n,s),p=[].concat(i(u),[s]);if(d||b){var E=d?e[s]:a,h=b?n[s]:a;if(v)return void t(E,h,v,o,p);if(E===h)return;o(E,h,p)}else if(!/^\d+^/.test(s))for(var m=Array.isArray(e)?e:[],g=Array.isArray(n)?n:[],O=m.length,w=g.length,j=Math.max(O,w),S=0;S<j;S++)t(m[S],g[S],r,o,[].concat(i(u),["".concat(S)]))},P=function(t,e,n){for(var r=function(){var t=c(u[o],2),r=t[0],a=t[1];R(n,e,r,(function(t,r,o){a({fieldPaths:o,newEntity:e,oldEntity:n,newValue:r,oldValue:t})}))},o=0,u=Object.entries(t);o<u.length;o++)r()},_=function(){for(var t,n=((t=(0,e.useRef)(!0)).current=!0,(0,e.useEffect)((function(){t.current=!1})),t),r=arguments.length,u=new Array(r),a=0;a<r;a++)u[a]=arguments[a];var v=function(t){return m((function(){var e=h.None;return"function"===y(c(t,2)[1])&&(e|=h.Entity),e}))}(u),p=function(t,e){var n=c(t,4),r=n[0],o=n[1],u=n[2],a=n[3];return(e&h.Entity)===h.Entity?[r,o,u||{},a||[]]:[null,r,o||{},u||[]]}(u,v),C=c(p,4),R=C[0],_=C[1],x=C[2],A=C[3],N=s(_),I=N,M=N,k=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:h.None,r=g,o=m((function(){if(r.has(t)){var o=r.get(t),u=o.value,a=o.times;return r.set(t,{value:u,times:a+1}),u}if((n&h.Entity)!==h.Entity)return O;var i=(0,e.createContext)(null);return r.set(t,{value:i,times:1}),i}));return(0,l.useUnmount)((function(){if(r.has(t)){var e=r.get(t),n=e.value,u=e.times;o===n&&(1!==u?r.set(t,{value:n,times:u-1}):r.delete(t))}})),o}(N,v),T=(0,e.useContext)(k),L=T,U=function(t,e){return(0,l.useCreation)((function(){var n=f.None,r=(e||{}).store;return(t&h.Entity)===h.Entity&&(n|=f.EntityRootEnabled),n|=e?f.ContextEnabled:f.RootEnabled,r&&(n|=f.EntityEnabled),n}),[t,e])}(v,T),D=(0,l.useLatest)(x),V=function(t){var n=(0,e.useRef)(0),r=(0,e.useRef)([]),o=r.current,u=w(t,o);return r.current=t,n.current+=u?0:1,n.current}(A),$=(T||{}).reducers,F=void 0===$?null:$,B=(L||{}).store,G=c(function(t,n,r,o,u){var a=(0,l.useUpdate)(),i=(0,l.useLatest)(o),c=r?r.value:null,s=(t&f.EntityEnabled)===f.EntityEnabled,y=(t&f.EntityRootEnabled)===f.EntityRootEnabled,v=S(r,t,(function(){if(!y)return new d.EntityStore(null);var t="function"==typeof n;return new d.EntityStore(t?n():n,{onChange:function(){a()}})})),b=function(t,n,r,o){var u=(0,e.useRef)(n);if((o&f.EntityRootEnabled)!==f.EntityRootEnabled)return r;var a="function"==typeof n,i=u.current;switch(!0){case a:case i===n:return t.value}return u.current=n,n}(v,n,c,t);return y&&(v.value=b),(0,e.useEffect)((function(){if(s){var t=function(t,e){var n=i.current,r=n.onChange,o=n.watch;o&&P(o,t,e),null==r||r(t,e)};return v.watch(t),function(){v.unwatch(t)}}}),[s,v,u,i]),(0,l.useCreation)((function(){return[b,v]}),[b,v])}(U,R,void 0===B?null:B,x,V),2),W=G[0],Y=G[1],q=S(F,U,(function(){var t,e,n,r=(t=x,e=function(t,e){var n,r;return null===(n=(r=D.current)[t])||void 0===n?void 0:n.call.apply(n,[r].concat(i(e)))},n={},Object.keys(t).forEach((function(r){var o=t[r];n[r]="function"!=typeof o?o:function(){for(var t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];return e(r,n)}})),n);return(U&f.EntityEnabled)!==f.EntityEnabled?I(r):function(t,e,n){n.watch,n.onChange;var r=n.options,o=function(t,e){if(null==t)return{};var n,r,o=function(t,e){if(null==t)return{};var n,r,o={},u=Object.keys(t);for(r=0;r<u.length;r++)n=u[r],e.indexOf(n)>=0||(o[n]=t[n]);return o}(t,e);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(t);for(r=0;r<u.length;r++)n=u[r],e.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(t,n)&&(o[n]=t[n])}return o}(n,b);return(0,d.createEntityReducers)(e,t,E(E(E({},o),r),{},{onGenerate:function(t,e){return e}}))}(M,Y,r)}),V),z=function(t,n,r,u){var a=r.value,i=(0,e.useMemo)((function(){return(t&f.EntityEnabled)===f.EntityEnabled?{store:r,reducers:u,statuses:t}:{reducers:u,statuses:t}}),[t,a,r,u]),c=(0,l.useLatest)(i);return s((function(t){var e=t.children,r=t.with,u=void 0===r?[]:r,a=n.Provider;return o().createElement(a,{value:c.current},u.reduceRight((function(t,e){return o().createElement(e,{},t)}),e))}))}(U,k,Y,q);return(0,l.useCreation)((function(){if((U&f.EntityEnabled)!==f.EntityEnabled)return q;var t=(U&f.RootEnabled)===f.RootEnabled,e=function(t,e){var n={};return Object.entries(t).forEach((function(t){var r=c(t,2),o=r[0],u=r[1];n[o]=j(u,e)})),n}(q,{onShouldCache:function(){return n.current}});return t?[W,e,z]:[W,e]}),[U,W,q,z,n])},x=function(n){var r=n.usecase,o=n.watch,u=n.onChange;return _(r,{watch:o,onChange:u}),(0,t.jsx)(e.Fragment,{})}})();var o=usecases="undefined"==typeof usecases?{}:usecases;for(var u in r)o[u]=r[u];r.__esModule&&Object.defineProperty(o,"__esModule",{value:!0})})();
|
|
@@ -1,27 +1,28 @@
|
|
|
1
|
-
import { useEffect
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
2
|
import { EntityStore } from '@mic-rexjs/usecases';
|
|
3
3
|
import { UseCaseStatuses } from '../../enums/UseCaseStatuses';
|
|
4
4
|
import { useContextualItem } from '../useContextualItem';
|
|
5
|
-
import { useCreation, useLatest } from 'ahooks';
|
|
5
|
+
import { useCreation, useLatest, useUpdate } from 'ahooks';
|
|
6
6
|
import { triggerWatchers } from '../../methods/triggerWatchers';
|
|
7
7
|
import { useRuntimeEntity } from '../useRuntimeEntity';
|
|
8
8
|
export const useEntity = (statuses, rootEntity, contextStore, options, depsKey) => {
|
|
9
|
-
const
|
|
9
|
+
const update = useUpdate();
|
|
10
|
+
const optionsRef = useLatest(options);
|
|
10
11
|
const contextEntity = (contextStore ? contextStore.value : null);
|
|
11
|
-
const runtimeEntity = useRuntimeEntity(statuses, entityState, rootEntity, contextEntity);
|
|
12
12
|
const entityEnabled = (statuses & UseCaseStatuses.EntityEnabled) === UseCaseStatuses.EntityEnabled;
|
|
13
13
|
const entityRootEnabled = (statuses & UseCaseStatuses.EntityRootEnabled) === UseCaseStatuses.EntityRootEnabled;
|
|
14
|
-
const optionsRef = useLatest(options);
|
|
15
14
|
const store = useContextualItem(contextStore, statuses, () => {
|
|
16
15
|
if (!entityRootEnabled) {
|
|
17
16
|
return new EntityStore(null);
|
|
18
17
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
const isFunction = typeof rootEntity === 'function';
|
|
19
|
+
return new EntityStore(isFunction ? rootEntity() : rootEntity, {
|
|
20
|
+
onChange() {
|
|
21
|
+
update();
|
|
22
22
|
},
|
|
23
23
|
});
|
|
24
24
|
});
|
|
25
|
+
const runtimeEntity = useRuntimeEntity(store, rootEntity, contextEntity, statuses);
|
|
25
26
|
if (entityRootEnabled) {
|
|
26
27
|
// 执行同步操作
|
|
27
28
|
store.value = runtimeEntity;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { UseCaseStatuses } from '../../enums/UseCaseStatuses';
|
|
2
2
|
import { EntityGetter } from '../../hooks/useUseCase/types';
|
|
3
|
-
|
|
3
|
+
import { EntityStore } from '@mic-rexjs/usecases';
|
|
4
|
+
export declare const useRuntimeEntity: <T>(store: EntityStore<T>, rootEntity: T | EntityGetter<T>, contextEntity: T, statuses: UseCaseStatuses) => T;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { UseCaseStatuses } from '../../enums/UseCaseStatuses';
|
|
2
2
|
import { useRef } from 'react';
|
|
3
|
-
export const useRuntimeEntity = (
|
|
3
|
+
export const useRuntimeEntity = (store, rootEntity, contextEntity, statuses) => {
|
|
4
4
|
const prevRootEntityRef = useRef(rootEntity);
|
|
5
5
|
const entityRootDisabled = (statuses & UseCaseStatuses.EntityRootEnabled) !== UseCaseStatuses.EntityRootEnabled;
|
|
6
6
|
if (entityRootDisabled) {
|
|
@@ -11,7 +11,7 @@ export const useRuntimeEntity = (statuses, entityState, rootEntity, contextEntit
|
|
|
11
11
|
switch (true) {
|
|
12
12
|
case isFunction:
|
|
13
13
|
case prevRootEntity === rootEntity:
|
|
14
|
-
return
|
|
14
|
+
return store.value;
|
|
15
15
|
}
|
|
16
16
|
prevRootEntityRef.current = rootEntity;
|
|
17
17
|
return rootEntity;
|
|
@@ -143,7 +143,7 @@ describe('useUseCase', () => {
|
|
|
143
143
|
const [entity] = cores;
|
|
144
144
|
expect(entity).toBe(defaultFile);
|
|
145
145
|
});
|
|
146
|
-
test('Should return the
|
|
146
|
+
test('Should return the `EntityStore.value` if root entity has not changed', () => {
|
|
147
147
|
const { result } = renderHook(() => {
|
|
148
148
|
return useUseCase(defaultFile, fileUseCase);
|
|
149
149
|
});
|
|
@@ -154,16 +154,19 @@ describe('useUseCase', () => {
|
|
|
154
154
|
});
|
|
155
155
|
expect(result.current[0]).toEqual({ ...defaultFile, path: PATH_1, ext: EXT_1 });
|
|
156
156
|
});
|
|
157
|
-
test('Should return the
|
|
157
|
+
test('Should return the root entity if root entity has changed', () => {
|
|
158
158
|
let file = defaultFile;
|
|
159
159
|
const { result, rerender } = renderHook(() => {
|
|
160
160
|
return useUseCase(file, fileUseCase);
|
|
161
161
|
});
|
|
162
|
-
file = { ...defaultFile, path:
|
|
162
|
+
file = { ...defaultFile, path: PATH_2 };
|
|
163
163
|
rerender();
|
|
164
|
-
expect(result.current[0]).toEqual({ ...defaultFile, path:
|
|
164
|
+
expect(result.current[0]).toEqual({ ...defaultFile, path: PATH_2 });
|
|
165
|
+
// 渲染多一次,避免第二次渲染的 `entity` 没变化而取错缓存的情况
|
|
166
|
+
rerender();
|
|
167
|
+
expect(result.current[0]).toEqual({ ...defaultFile, path: PATH_2 });
|
|
165
168
|
});
|
|
166
|
-
test('Should return the
|
|
169
|
+
test('Should return the root entity if root entity has changed even with entity state changed', () => {
|
|
167
170
|
let file = defaultFile;
|
|
168
171
|
const { result } = renderHook(() => {
|
|
169
172
|
return useUseCase(file, fileUseCase);
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
7
6
|
exports.useEntity = void 0;
|
|
8
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
9
7
|
var _react = require("react");
|
|
10
8
|
var _usecases = require("@mic-rexjs/usecases");
|
|
11
9
|
var _UseCaseStatuses = require("../../enums/UseCaseStatuses");
|
|
@@ -14,25 +12,23 @@ var _ahooks = require("ahooks");
|
|
|
14
12
|
var _triggerWatchers = require("../../methods/triggerWatchers");
|
|
15
13
|
var _useRuntimeEntity = require("../useRuntimeEntity");
|
|
16
14
|
var useEntity = exports.useEntity = function useEntity(statuses, rootEntity, contextStore, options, depsKey) {
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
entityState = _useState2[0],
|
|
20
|
-
setEntityState = _useState2[1];
|
|
15
|
+
var update = (0, _ahooks.useUpdate)();
|
|
16
|
+
var optionsRef = (0, _ahooks.useLatest)(options);
|
|
21
17
|
var contextEntity = contextStore ? contextStore.value : null;
|
|
22
|
-
var runtimeEntity = (0, _useRuntimeEntity.useRuntimeEntity)(statuses, entityState, rootEntity, contextEntity);
|
|
23
18
|
var entityEnabled = (statuses & _UseCaseStatuses.UseCaseStatuses.EntityEnabled) === _UseCaseStatuses.UseCaseStatuses.EntityEnabled;
|
|
24
19
|
var entityRootEnabled = (statuses & _UseCaseStatuses.UseCaseStatuses.EntityRootEnabled) === _UseCaseStatuses.UseCaseStatuses.EntityRootEnabled;
|
|
25
|
-
var optionsRef = (0, _ahooks.useLatest)(options);
|
|
26
20
|
var store = (0, _useContextualItem.useContextualItem)(contextStore, statuses, function () {
|
|
27
21
|
if (!entityRootEnabled) {
|
|
28
22
|
return new _usecases.EntityStore(null);
|
|
29
23
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
var isFunction = typeof rootEntity === 'function';
|
|
25
|
+
return new _usecases.EntityStore(isFunction ? rootEntity() : rootEntity, {
|
|
26
|
+
onChange: function onChange() {
|
|
27
|
+
update();
|
|
33
28
|
}
|
|
34
29
|
});
|
|
35
30
|
});
|
|
31
|
+
var runtimeEntity = (0, _useRuntimeEntity.useRuntimeEntity)(store, rootEntity, contextEntity, statuses);
|
|
36
32
|
if (entityRootEnabled) {
|
|
37
33
|
// 执行同步操作
|
|
38
34
|
store.value = runtimeEntity;
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.useRuntimeEntity = void 0;
|
|
7
7
|
var _UseCaseStatuses = require("../../enums/UseCaseStatuses");
|
|
8
8
|
var _react = require("react");
|
|
9
|
-
var useRuntimeEntity = exports.useRuntimeEntity = function useRuntimeEntity(
|
|
9
|
+
var useRuntimeEntity = exports.useRuntimeEntity = function useRuntimeEntity(store, rootEntity, contextEntity, statuses) {
|
|
10
10
|
var prevRootEntityRef = (0, _react.useRef)(rootEntity);
|
|
11
11
|
var entityRootDisabled = (statuses & _UseCaseStatuses.UseCaseStatuses.EntityRootEnabled) !== _UseCaseStatuses.UseCaseStatuses.EntityRootEnabled;
|
|
12
12
|
if (entityRootDisabled) {
|
|
@@ -17,7 +17,7 @@ var useRuntimeEntity = exports.useRuntimeEntity = function useRuntimeEntity(stat
|
|
|
17
17
|
switch (true) {
|
|
18
18
|
case isFunction:
|
|
19
19
|
case prevRootEntity === rootEntity:
|
|
20
|
-
return
|
|
20
|
+
return store.value;
|
|
21
21
|
}
|
|
22
22
|
prevRootEntityRef.current = rootEntity;
|
|
23
23
|
return rootEntity;
|
|
@@ -267,7 +267,7 @@ var useDeepCompareUpdate = function useDeepCompareUpdate(callback, deps) {
|
|
|
267
267
|
entity = _cores[0];
|
|
268
268
|
(0, _globals.expect)(entity).toBe(defaultFile);
|
|
269
269
|
});
|
|
270
|
-
(0, _globals.test)('Should return the
|
|
270
|
+
(0, _globals.test)('Should return the `EntityStore.value` if root entity has not changed', function () {
|
|
271
271
|
var _renderHook3 = (0, _react.renderHook)(function () {
|
|
272
272
|
return (0, _.useUseCase)(defaultFile, fileUseCase);
|
|
273
273
|
}),
|
|
@@ -283,7 +283,7 @@ var useDeepCompareUpdate = function useDeepCompareUpdate(callback, deps) {
|
|
|
283
283
|
ext: EXT_1
|
|
284
284
|
}));
|
|
285
285
|
});
|
|
286
|
-
(0, _globals.test)('Should return the
|
|
286
|
+
(0, _globals.test)('Should return the root entity if root entity has changed', function () {
|
|
287
287
|
var file = defaultFile;
|
|
288
288
|
var _renderHook4 = (0, _react.renderHook)(function () {
|
|
289
289
|
return (0, _.useUseCase)(file, fileUseCase);
|
|
@@ -291,14 +291,19 @@ var useDeepCompareUpdate = function useDeepCompareUpdate(callback, deps) {
|
|
|
291
291
|
result = _renderHook4.result,
|
|
292
292
|
rerender = _renderHook4.rerender;
|
|
293
293
|
file = _objectSpread(_objectSpread({}, defaultFile), {}, {
|
|
294
|
-
path:
|
|
294
|
+
path: PATH_2
|
|
295
295
|
});
|
|
296
296
|
rerender();
|
|
297
297
|
(0, _globals.expect)(result.current[0]).toEqual(_objectSpread(_objectSpread({}, defaultFile), {}, {
|
|
298
|
-
path:
|
|
298
|
+
path: PATH_2
|
|
299
|
+
}));
|
|
300
|
+
// 渲染多一次,避免第二次渲染的 `entity` 没变化而取错缓存的情况
|
|
301
|
+
rerender();
|
|
302
|
+
(0, _globals.expect)(result.current[0]).toEqual(_objectSpread(_objectSpread({}, defaultFile), {}, {
|
|
303
|
+
path: PATH_2
|
|
299
304
|
}));
|
|
300
305
|
});
|
|
301
|
-
(0, _globals.test)('Should return the
|
|
306
|
+
(0, _globals.test)('Should return the root entity if root entity has changed even with entity state changed', function () {
|
|
302
307
|
var file = defaultFile;
|
|
303
308
|
var _renderHook5 = (0, _react.renderHook)(function () {
|
|
304
309
|
return (0, _.useUseCase)(file, fileUseCase);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mic-rexjs/usecases-react",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.2",
|
|
4
4
|
"description": "React-based solution for use usecases of Clean Architecture",
|
|
5
5
|
"private": false,
|
|
6
6
|
"keywords": [
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"license": "MIT",
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@mic-rexjs/usecases": "^2.7.
|
|
36
|
+
"@mic-rexjs/usecases": "^2.7.7",
|
|
37
37
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"@commitlint/cli": "^19.0.3",
|
|
50
50
|
"@commitlint/config-angular": "^19.0.3",
|
|
51
51
|
"@jest/globals": "^29.5.0",
|
|
52
|
-
"@mic-rexjs/usecases": "^2.7.
|
|
52
|
+
"@mic-rexjs/usecases": "^2.7.7",
|
|
53
53
|
"@testing-library/react": "^14.0.0",
|
|
54
54
|
"@types/node": "^20.4.2",
|
|
55
55
|
"@types/react": "^18.2.14",
|