@mic-rexjs/usecases-react 1.3.36 → 1.4.1
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/README.md +2 -2
- package/dist/index.min.js +1 -1
- package/es/components/UpdateFragment/index.d.ts +2 -0
- package/es/components/UpdateFragment/index.js +8 -0
- package/es/components/UpdateFragment/types.d.ts +3 -0
- package/es/components/UseCaseFragment/index.d.ts +1 -1
- package/es/components/UseCaseFragment/types.d.ts +3 -3
- package/es/configs/defaultUseCaseContext/index.d.ts +3 -1
- package/es/configs/defaultUseCaseContext/index.js +1 -1
- package/es/configs/defaultUseCaseContext/types.d.ts +11 -0
- package/es/configs/defaultUseCaseContext/types.js +1 -0
- package/es/configs/usecaseContextMap/index.d.ts +1 -3
- package/es/configs/usecaseContextMap/types.d.ts +4 -17
- package/es/hooks/useCompareDeps/index.d.ts +1 -0
- package/es/hooks/useCompareDeps/index.js +24 -0
- package/es/hooks/useConstant/index.d.ts +2 -0
- package/es/hooks/useConstant/index.js +5 -0
- package/es/hooks/useConstant/types.d.ts +3 -0
- package/es/hooks/useConstant/types.js +1 -0
- package/es/hooks/useConstantFn/index.d.ts +1 -0
- package/es/hooks/useConstantFn/index.js +7 -0
- package/es/hooks/useOptionsRefCollection/index.d.ts +3 -0
- package/es/hooks/useOptionsRefCollection/index.js +22 -0
- package/es/hooks/useOptionsRefCollection/types.d.ts +2 -0
- package/es/hooks/useOptionsRefCollection/types.js +1 -0
- package/es/hooks/useProvider/index.d.ts +7 -0
- package/es/hooks/useProvider/index.js +17 -0
- package/es/hooks/useProvider/types.d.ts +3 -0
- package/es/hooks/useProvider/types.js +1 -0
- package/es/hooks/useScope/index.d.ts +2 -0
- package/es/hooks/useScope/index.js +3 -0
- package/es/hooks/useScope/types.d.ts +3 -0
- package/es/hooks/useScope/types.js +1 -0
- package/es/hooks/useUseCase/index.js +68 -89
- package/es/hooks/useUseCase/index.test.js +638 -156
- package/es/hooks/useUseCase/types.d.ts +27 -13
- package/es/hooks/useUseCaseContext/index.d.ts +3 -0
- package/es/hooks/useUseCaseContext/index.js +17 -0
- package/es/index.d.ts +3 -2
- package/es/methods/compareProperty/index.d.ts +2 -0
- package/es/methods/compareProperty/index.js +34 -0
- package/es/methods/compareProperty/types.d.ts +3 -0
- package/es/methods/compareProperty/types.js +1 -0
- package/es/methods/getRootEntity/index.d.ts +2 -0
- package/es/methods/getRootEntity/index.js +9 -0
- package/es/methods/hasField/index.d.ts +1 -0
- package/es/methods/hasField/index.js +6 -0
- package/es/methods/initEntityReducers/index.d.ts +5 -0
- package/es/methods/initEntityReducers/index.js +13 -0
- package/es/methods/initEntityReducers/types.d.ts +3 -0
- package/es/methods/initEntityReducers/types.js +1 -0
- package/es/methods/removeItem/index.d.ts +1 -0
- package/es/methods/removeItem/index.js +7 -0
- package/es/methods/triggerCallbacks/index.d.ts +3 -0
- package/es/methods/triggerCallbacks/index.js +14 -0
- package/es/methods/triggerWatchers/index.d.ts +2 -0
- package/es/methods/triggerWatchers/index.js +14 -0
- package/lib/components/UpdateFragment/index.js +16 -0
- package/lib/configs/defaultUseCaseContext/index.js +1 -1
- package/lib/configs/defaultUseCaseContext/types.js +5 -0
- package/lib/hooks/useCompareDeps/index.js +31 -0
- package/lib/hooks/useConstant/index.js +16 -0
- package/lib/hooks/useConstant/types.js +5 -0
- package/lib/hooks/useConstantFn/index.js +18 -0
- package/lib/hooks/useOptionsRefCollection/index.js +29 -0
- package/lib/hooks/useOptionsRefCollection/types.js +5 -0
- package/lib/hooks/useProvider/index.js +30 -0
- package/lib/hooks/useProvider/types.js +5 -0
- package/lib/hooks/useScope/index.js +10 -0
- package/lib/hooks/useScope/types.js +5 -0
- package/lib/hooks/useUseCase/index.js +73 -115
- package/lib/hooks/useUseCase/index.test.js +841 -240
- package/lib/hooks/useUseCaseContext/index.js +24 -0
- package/lib/methods/compareProperty/index.js +50 -0
- package/lib/methods/compareProperty/types.js +5 -0
- package/lib/methods/getRootEntity/index.js +16 -0
- package/lib/methods/hasField/index.js +15 -0
- package/lib/methods/initEntityReducers/index.js +29 -0
- package/lib/methods/initEntityReducers/types.js +5 -0
- package/lib/methods/removeItem/index.js +14 -0
- package/lib/methods/triggerCallbacks/index.js +22 -0
- package/lib/{hooks/useUseCase/methods → methods}/triggerWatchers/index.js +16 -9
- package/package.json +2 -2
- package/es/core/usecases/optionsGetterCellectionUseCase/index.d.ts +0 -2
- package/es/core/usecases/optionsGetterCellectionUseCase/index.js +0 -13
- package/es/core/usecases/optionsGetterCellectionUseCase/types.d.ts +0 -8
- package/es/hooks/useOptionsTransfer/index.d.ts +0 -3
- package/es/hooks/useOptionsTransfer/index.js +0 -15
- package/es/hooks/useUseCase/methods/triggerCallbacks/index.d.ts +0 -3
- package/es/hooks/useUseCase/methods/triggerCallbacks/index.js +0 -11
- package/es/hooks/useUseCase/methods/triggerWatchers/index.d.ts +0 -2
- package/es/hooks/useUseCase/methods/triggerWatchers/index.js +0 -10
- package/lib/core/usecases/optionsGetterCellectionUseCase/index.js +0 -23
- package/lib/hooks/useOptionsTransfer/index.js +0 -23
- package/lib/hooks/useUseCase/methods/triggerCallbacks/index.js +0 -32
- /package/es/{core/usecases/optionsGetterCellectionUseCase → components/UpdateFragment}/types.js +0 -0
- /package/lib/{core/usecases/optionsGetterCellectionUseCase → components/UpdateFragment}/types.js +0 -0
package/README.md
CHANGED
|
@@ -87,8 +87,8 @@ const fileUseCase = <T extends File>({ maxContentLength = 2000 }: FileUseCaseOpt
|
|
|
87
87
|
const objectReducers = objectUseCase<T>();
|
|
88
88
|
|
|
89
89
|
const writeFile = function* (entity: T, content: string): EntityGenerator<T, string> {
|
|
90
|
-
const { content:
|
|
91
|
-
const newContent =
|
|
90
|
+
const { content: oldContent } = entity;
|
|
91
|
+
const newContent = oldContent + content;
|
|
92
92
|
|
|
93
93
|
if (newContent.length > maxContentLength) {
|
|
94
94
|
throw 'max length error';
|
package/dist/index.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see index.min.js.LICENSE.txt */
|
|
2
|
-
!function(e,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r(require("react"),require("ahooks"),require("@mic-rexjs/usecases"));else if("function"==typeof define&&define.amd)define(["react","ahooks","@mic-rexjs/usecases"],r);else{var t="object"==typeof exports?r(require("react"),require("ahooks"),require("@mic-rexjs/usecases")):r(e.react,e.ahooks,e["@mic-rexjs/usecases"]);for(var n in t)("object"==typeof exports?exports:e)[n]=t[n]}}(this,((e,r,t)=>(()=>{"use strict";var n={251:(e,r,t)=>{var n=t(156),o=Symbol.for("react.element"),u=(Symbol.for("react.fragment"),Object.prototype.hasOwnProperty),i=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,a={key:!0,ref:!0,__self:!0,__source:!0};r.jsx=function(e,r,t){var n,c={},f=null,
|
|
2
|
+
!function(e,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r(require("react"),require("ahooks"),require("@mic-rexjs/usecases"));else if("function"==typeof define&&define.amd)define(["react","ahooks","@mic-rexjs/usecases"],r);else{var t="object"==typeof exports?r(require("react"),require("ahooks"),require("@mic-rexjs/usecases")):r(e.react,e.ahooks,e["@mic-rexjs/usecases"]);for(var n in t)("object"==typeof exports?exports:e)[n]=t[n]}}(this,((e,r,t)=>(()=>{"use strict";var n={251:(e,r,t)=>{var n=t(156),o=Symbol.for("react.element"),u=(Symbol.for("react.fragment"),Object.prototype.hasOwnProperty),i=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,a={key:!0,ref:!0,__self:!0,__source:!0};r.jsx=function(e,r,t){var n,c={},f=null,s=null;for(n in void 0!==t&&(f=""+t),void 0!==r.key&&(f=""+r.key),void 0!==r.ref&&(s=r.ref),r)u.call(r,n)&&!a.hasOwnProperty(n)&&(c[n]=r[n]);if(e&&e.defaultProps)for(n in r=e.defaultProps)void 0===c[n]&&(c[n]=r[n]);return{$$typeof:o,type:e,key:f,ref:s,props:c,_owner:i.current}}},893:(e,r,t)=>{e.exports=t(251)},112:e=>{e.exports=t},749:e=>{e.exports=r},156:r=>{r.exports=e}},o={};function u(e){var r=o[e];if(void 0!==r)return r.exports;var t=o[e]={exports:{}};return n[e](t,t.exports,u),t.exports}u.n=e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return u.d(r,{a:r}),r},u.d=(e,r)=>{for(var t in r)u.o(r,t)&&!u.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},u.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),u.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var i={};return(()=>{u.r(i),u.d(i,{UseCaseFragment:()=>x,useUseCase:()=>w});var e=u(893),r=u(156),t=u.n(r);function n(e){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},n(e)}function o(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function a(e,r){if(e){if("string"==typeof e)return o(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(e,r):void 0}}function c(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,o,u,i,a=[],c=!0,f=!1;try{if(u=(t=t.call(e)).next,0===r){if(Object(t)!==t)return;c=!1}else for(;!(c=(n=u.call(t)).done)&&(a.push(n.value),a.length!==r);c=!0);}catch(e){f=!0,o=e}finally{try{if(!c&&null!=t.return&&(i=t.return(),Object(i)!==i))return}finally{if(f)throw o}}return a}}(e,r)||a(e,r)||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.")}()}var f=u(749),s=function(e){return c((0,r.useState)(e),1)[0]};function l(e,r,t){return(r=function(e){var r=function(e,r){if("object"!==n(e)||null===e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var o=t.call(e,"string");if("object"!==n(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===n(r)?r:String(r)}(r))in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}var y=u(112),p=["stateless","watch","onChange","options"];function v(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function b(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?v(Object(t),!0).forEach((function(r){l(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):v(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var d=function(e,r){var t=e.indexOf(r);-1!==t&&e.splice(t,1)};function m(e){return function(e){if(Array.isArray(e))return o(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||a(e)||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.")}()}var h=function(e,r){return"object"===n(e)&&null!==e&&Object.hasOwn(e,r)},O=function e(r,t,n,o){var u=arguments.length>4&&void 0!==arguments[4]?arguments[4]:[],i=void 0,a=c(n.match(/^([^.]+)\.(.+)$/)||[],3),f=a[1],s=void 0===f?n:f,l=a[2],y=void 0===l?"":l,p=h(r,s),v=h(t,s),b=[].concat(m(u),[s]);if(p||v){var d=p?r[s]:i,O=v?t[s]:i;if(y)return void e(d,O,y,o,b);if(d===O)return;o(d,O,b)}else if(!/^\d+^/.test(s))for(var g=Array.isArray(r)?r:[],j=Array.isArray(t)?t:[],S=g.length,w=j.length,x=Math.max(S,w),C=0;C<x;C++)e(g[C],j[C],n,o,[].concat(m(u),["".concat(C)]))},g=function(e,r,t){for(var n=function(){var e=c(u[o],2),n=e[0],i=e[1];O(t,r,n,(function(e,n,o){i({fieldPaths:o,newEntity:r,oldEntity:t,newValue:n,oldValue:e})}))},o=0,u=Object.entries(e);o<u.length;o++)n()},j=new Map,S=(0,r.createContext)({}),w=function(e,o,u,i){var a,l,v,m,h,O,w,x,C=s("function"==typeof o),P=(C?u:o)||{},_=P.stateless,E=c((0,r.useState)(C&&!_?e:null),2),A=E[0],R=E[1],k=(l=(C?i:u)||[],v=(0,r.useRef)(0),m=(0,r.useRef)([]),h=m.current,O=h.length,w=l.length,x=function(){if(w!==O)return!0;for(var e=0;e<w;e++)if(l[e]!==h[e])return!0;return!1}(),m.current=l,v.current+=x?1:0,v.current),I=(a=C?o:e,c((0,r.useState)((function(){return a})),1)[0]),M=I,T=function(e,t){return s((function(){var n=j;if(n.has(e))return n.get(e);if(t)return t;var o=(0,r.createContext)({});return n.set(e,o),o}))}(M,C?null:S),U=(0,r.useContext)(T),q=U.entity,D=U.reducers,F=U.optionsRefCollection,L="object"===n(F),$=C||L,N=function(e,r,t){var n=(0,f.useLatest)(t),o=(0,f.useCreation)((function(){return e?[]:r}),[r,e]),u=(0,f.usePrevious)(o);return(0,f.useCreation)((function(){u&&d(u,n),o.push(n)}),[n,o,u]),(0,f.useUnmount)((function(){d(o,n)})),o}(C||!L,F||null,P),z=(0,f.useCreation)((function(){return C?function(e,r,t){return t?"function"==typeof r?r():r:e}(A,e,_):q}),[C,q,_,A,e]),V=(0,f.useMemoizedFn)((function(e,r){_||R(e),function(e,r,t){for(var n=e.length;n>0;n--){var o=e[n-1].current;if(o){var u=o.onChange,i=o.watch;i&&g(i,r,t),null==u||u(r,t)}}}(N,e,r)})),B=(0,f.useMemoizedFn)((function(){var e={},r={};return C?r=function(e,r,t,n){var o=(0,y.entityReducerUseCase)().createEntityReducers,u=(t.stateless,t.watch,t.onChange,t.options);return o(e,r,b(b(b({},function(e,r){if(null==e)return{};var t,n,o=function(e,r){if(null==e)return{};var t,n,o={},u=Object.keys(e);for(n=0;n<u.length;n++)t=u[n],r.indexOf(t)>=0||(o[t]=e[t]);return o}(e,r);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(e);for(n=0;n<u.length;n++)t=u[n],r.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(o[t]=e[t])}return o}(t,p)),u),{},{onChange:n,onGenerate:function(e,r){return r}}))}(z,M,P,V):D?r=D:e=I(P),[e,r]})),G=C?z:D,W=c((0,f.useCreation)((function(){return B()}),[B,k,G]),2),Y=W[0],H=W[1],J=function(e,r,n,o){var u=(0,f.useCreation)((function(){return{entity:r,reducers:n,optionsRefCollection:o}}),[r,n,o]),i=(0,f.useLatest)(u);return s((function(){return function(r){var n=r.children,o=e.Provider;return t().createElement(o,{value:i.current},n)}}))}(T,z,H,N),K=(0,f.useCreation)((function(){return[z,H,C?J:null]}),[C,z,H,J]);return(0,f.useCreation)((function(){return $?K:Y}),[$,Y,K])},x=function(t){var n=t.usecase,o=t.watch,u=t.onChange;return w(n,{watch:o,onChange:u}),(0,e.jsx)(r.Fragment,{})}})(),i})()));
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { UseCaseFragmentProps } from './types';
|
|
2
2
|
import { EntityReducers } from '@mic-rexjs/usecases';
|
|
3
|
-
export declare const UseCaseFragment: <T,
|
|
3
|
+
export declare const UseCaseFragment: <T, TEntityReducers extends EntityReducers<T>>({ usecase, watch, onChange, }: UseCaseFragmentProps<T, TEntityReducers>) => React.ReactElement;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EntityReducers, EntityUseCase } from '@mic-rexjs/usecases';
|
|
2
2
|
import { EntityWatchMap } from '../../hooks/useUseCase/types';
|
|
3
|
-
export interface UseCaseFragmentProps<T,
|
|
4
|
-
usecase: EntityUseCase<T,
|
|
3
|
+
export interface UseCaseFragmentProps<T, TEntityReducers extends EntityReducers<T>> {
|
|
4
|
+
usecase: EntityUseCase<T, TEntityReducers & EntityReducers<T>>;
|
|
5
5
|
watch?: EntityWatchMap<T>;
|
|
6
|
-
onChange?(
|
|
6
|
+
onChange?(newEntity: T, oldEntity: T): void;
|
|
7
7
|
}
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
|
|
2
|
+
import { EntityReducers } from '@mic-rexjs/usecases';
|
|
3
|
+
import { UseCaseContextValue } from './types';
|
|
4
|
+
export declare const defaultUseCaseContext: import("react").Context<UseCaseContextValue<unknown, EntityReducers<unknown>, object>>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { createContext } from 'react';
|
|
2
|
-
export const defaultUseCaseContext = createContext(
|
|
2
|
+
export const defaultUseCaseContext = createContext({});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { OptionsRefCollection } from '../../hooks/useOptionsRefCollection/types';
|
|
2
|
+
import { AsyncEntityGenerator, EntityGenerator, EntityReducer, EntityReducers } from '@mic-rexjs/usecases';
|
|
3
|
+
export type ContextualEntityReducer<T, TReducer extends EntityReducer<T>> = TReducer extends (entity: T, ...args: infer TArgs) => infer TReturn ? (...args: TArgs) => TReturn extends AsyncEntityGenerator<T, infer TResult> ? Promise<TResult> : TReturn extends EntityGenerator<T, infer TResult> ? TResult : TReturn : never;
|
|
4
|
+
export type ContextualEntityReducers<T, TEntityReducers extends EntityReducers<T>> = {
|
|
5
|
+
[K in keyof TEntityReducers]: ContextualEntityReducer<T, TEntityReducers[K]>;
|
|
6
|
+
};
|
|
7
|
+
export interface UseCaseContextValue<T, TEntityReducers extends EntityReducers<T>, TUseCaseOptions extends object> {
|
|
8
|
+
entity?: T;
|
|
9
|
+
reducers?: ContextualEntityReducers<T, TEntityReducers>;
|
|
10
|
+
optionsRefCollection?: OptionsRefCollection<TUseCaseOptions>;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
export
|
|
5
|
-
export type ContextualEntityReducers<T, TReducers> = {
|
|
6
|
-
[K in keyof TReducers]: ContextualEntityReducer<T, TReducers[K]>;
|
|
7
|
-
};
|
|
8
|
-
export type UseCaseContext<T, TReducers> = [entity: T, reducers: ContextualEntityReducers<T, TReducers>];
|
|
9
|
-
export type UseCaseContextWithCollectionReducers<T, TReducers, TOptions> = [
|
|
10
|
-
...context: UseCaseContext<T, TReducers>,
|
|
11
|
-
collectionReducers: OptionsGetterCollectionReducers<TOptions>
|
|
12
|
-
];
|
|
13
|
-
export interface UseCaseProvider extends React.FC<React.PropsWithChildren> {
|
|
2
|
+
import { EntityReducers, EntityUseCase } from '@mic-rexjs/usecases';
|
|
3
|
+
import { UseCaseContextValue } from '../defaultUseCaseContext/types';
|
|
4
|
+
export interface UseCaseMappingContext<T, TEntityReducers extends EntityReducers<T>, TUseCaseOptions extends object = object> extends React.Context<UseCaseContextValue<T, TEntityReducers, TUseCaseOptions>> {
|
|
14
5
|
}
|
|
15
|
-
export
|
|
16
|
-
...args: UseCaseContext<T, TReducers>,
|
|
17
|
-
Provider: UseCaseProvider
|
|
18
|
-
];
|
|
19
|
-
export interface UseCaseContextMap<T, TReducers extends EntityReducers<T>, TOptions = object> extends Map<EntityUseCase<T, TReducers>, React.Context<UseCaseContextWithCollectionReducers<T, TReducers, TOptions> | null>> {
|
|
6
|
+
export interface UseCaseContextMap<T, TEntityReducers extends EntityReducers<T>, TUseCaseOptions extends object = object> extends Map<EntityUseCase<T, TEntityReducers, TUseCaseOptions>, UseCaseMappingContext<T, TEntityReducers, TUseCaseOptions>> {
|
|
20
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useCompareDeps: <T>(deps: T[]) => React.Key;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
import { useScope } from '../useScope';
|
|
3
|
+
export const useCompareDeps = (deps) => {
|
|
4
|
+
const updateTimesRef = useRef(0);
|
|
5
|
+
const prevDepsRef = useRef([]);
|
|
6
|
+
const { current: prevDeps } = prevDepsRef;
|
|
7
|
+
const { length: prevLength } = prevDeps;
|
|
8
|
+
const { length } = deps;
|
|
9
|
+
const updated = useScope(() => {
|
|
10
|
+
if (length !== prevLength) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
for (let i = 0; i < length; i++) {
|
|
14
|
+
if (deps[i] === prevDeps[i]) {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
});
|
|
21
|
+
prevDepsRef.current = deps;
|
|
22
|
+
updateTimesRef.current += updated ? 1 : 0;
|
|
23
|
+
return updateTimesRef.current;
|
|
24
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useConstantFn: <T>(initialValue: T) => T;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { UseCaseHookOptions } from '../useUseCase/types';
|
|
2
|
+
import { OptionsRefCollection } from './types';
|
|
3
|
+
export declare const useOptionsRefCollection: <T, TUseCaseOptions extends object, TOptions extends UseCaseHookOptions<T, TUseCaseOptions> = UseCaseHookOptions<T, TUseCaseOptions>>(isRoot: boolean, contextOptionsRefCollection: OptionsRefCollection<TOptions> | null, options: TOptions) => OptionsRefCollection<TOptions>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useCreation, useLatest, usePrevious, useUnmount } from 'ahooks';
|
|
2
|
+
import { removeItem } from '../../methods/removeItem';
|
|
3
|
+
export const useOptionsRefCollection = (isRoot, contextOptionsRefCollection, options) => {
|
|
4
|
+
const optionsRef = useLatest(options);
|
|
5
|
+
const optionsRefCollection = useCreation(() => {
|
|
6
|
+
if (isRoot) {
|
|
7
|
+
return [];
|
|
8
|
+
}
|
|
9
|
+
return contextOptionsRefCollection;
|
|
10
|
+
}, [contextOptionsRefCollection, isRoot]);
|
|
11
|
+
const prevOptionsRefCollection = usePrevious(optionsRefCollection);
|
|
12
|
+
useCreation(() => {
|
|
13
|
+
if (prevOptionsRefCollection) {
|
|
14
|
+
removeItem(prevOptionsRefCollection, optionsRef);
|
|
15
|
+
}
|
|
16
|
+
optionsRefCollection.push(optionsRef);
|
|
17
|
+
}, [optionsRef, optionsRefCollection, prevOptionsRefCollection]);
|
|
18
|
+
useUnmount(() => {
|
|
19
|
+
removeItem(optionsRefCollection, optionsRef);
|
|
20
|
+
});
|
|
21
|
+
return optionsRefCollection;
|
|
22
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { UseCaseMappingContext } from '../../configs/usecaseContextMap/types';
|
|
2
|
+
import { EntityReducers } from '@mic-rexjs/usecases';
|
|
3
|
+
import { UseCaseHookOptions } from '../useUseCase/types';
|
|
4
|
+
import { ContextualEntityReducers } from '../../configs/defaultUseCaseContext/types';
|
|
5
|
+
import { UseCaseProvider } from './types';
|
|
6
|
+
import { OptionsRefCollection } from '../useOptionsRefCollection/types';
|
|
7
|
+
export declare const useProvider: <T, TEntityReducers extends EntityReducers<T>, TUseCaseOptions extends object>(context: UseCaseMappingContext<T, TEntityReducers, TUseCaseOptions>, entity: T, reducers: ContextualEntityReducers<T, TEntityReducers>, optionsRefCollection: OptionsRefCollection<UseCaseHookOptions<T, TUseCaseOptions>>) => UseCaseProvider;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useConstant } from '../useConstant';
|
|
3
|
+
import { useCreation, useLatest } from 'ahooks';
|
|
4
|
+
export const useProvider = (context, entity, reducers, optionsRefCollection) => {
|
|
5
|
+
const contextValue = useCreation(() => {
|
|
6
|
+
return { entity, reducers, optionsRefCollection };
|
|
7
|
+
}, [entity, reducers, optionsRefCollection]);
|
|
8
|
+
const contextValueRef = useLatest(contextValue);
|
|
9
|
+
return useConstant(() => {
|
|
10
|
+
return ({ children }) => {
|
|
11
|
+
const { Provider: ContextProvider } = context;
|
|
12
|
+
return React.createElement(ContextProvider, {
|
|
13
|
+
value: contextValueRef.current,
|
|
14
|
+
}, children);
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,99 +1,78 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { useCreation, useMemoizedFn } from 'ahooks';
|
|
2
|
+
import { useContext, useState } from 'react';
|
|
3
|
+
import { useConstant } from '../useConstant';
|
|
4
|
+
import { useConstantFn } from '../useConstantFn';
|
|
5
|
+
import { useProvider } from '../useProvider';
|
|
6
|
+
import { useCompareDeps } from '../useCompareDeps';
|
|
7
|
+
import { initEntityReducers } from '../../methods/initEntityReducers';
|
|
8
|
+
import { useOptionsRefCollection } from '../useOptionsRefCollection';
|
|
9
|
+
import { triggerCallbacks } from '../../methods/triggerCallbacks';
|
|
10
|
+
import { getRootEntity } from '../../methods/getRootEntity';
|
|
11
|
+
import { useUseCaseContext } from '../useUseCaseContext';
|
|
3
12
|
import { defaultUseCaseContext } from '../../configs/defaultUseCaseContext';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (!
|
|
28
|
-
return defaultUseCaseContext;
|
|
29
|
-
}
|
|
30
|
-
const ctx = createContext(null);
|
|
31
|
-
map.set(entityUseCase, ctx);
|
|
32
|
-
return ctx;
|
|
33
|
-
});
|
|
34
|
-
const [contextEntity, contextEntityReducers, contextCollectionReducers] = useContext(context) || [];
|
|
35
|
-
const entity = useMemo(() => {
|
|
36
|
-
if (!hasDefaultEntity) {
|
|
13
|
+
export const useUseCase = (arg1, arg2, arg3, arg4) => {
|
|
14
|
+
const hasInitialEntity = useConstant(typeof arg2 === 'function');
|
|
15
|
+
const options = ((hasInitialEntity ? arg3 : arg2) || {});
|
|
16
|
+
const { stateless } = options;
|
|
17
|
+
const [entityState, setEntityState] = useState(hasInitialEntity && !stateless ? arg1 : null);
|
|
18
|
+
const deps = ((hasInitialEntity ? arg4 : arg3) || []);
|
|
19
|
+
const depsKey = useCompareDeps(deps);
|
|
20
|
+
const usecase = useConstantFn(hasInitialEntity
|
|
21
|
+
? arg2
|
|
22
|
+
: arg1);
|
|
23
|
+
const entityUseCase = usecase;
|
|
24
|
+
const defaultContext = defaultUseCaseContext;
|
|
25
|
+
const context = useUseCaseContext(entityUseCase, hasInitialEntity ? null : defaultContext);
|
|
26
|
+
const { entity: contextEntity, reducers: contextEntityReducers, optionsRefCollection: contextOptionsRefCollection, } = useContext(context);
|
|
27
|
+
// 只有 `contextOptionsRefCollection` 是可控的,是当前 `hook` 生成的,所以用其来判断最保险
|
|
28
|
+
const hasContextOptionsRefCollection = typeof contextOptionsRefCollection === 'object';
|
|
29
|
+
// 是否需要初始化根 `redurcers`
|
|
30
|
+
const isRoot = hasInitialEntity || !hasContextOptionsRefCollection;
|
|
31
|
+
// 是否为上下文模式
|
|
32
|
+
const isContextMode = hasInitialEntity || hasContextOptionsRefCollection;
|
|
33
|
+
// 获取 `optionsRefCollection`
|
|
34
|
+
const optionsRefCollection = useOptionsRefCollection(isRoot, contextOptionsRefCollection || null, options);
|
|
35
|
+
const entity = useCreation(() => {
|
|
36
|
+
if (!hasInitialEntity) {
|
|
37
37
|
return contextEntity;
|
|
38
38
|
}
|
|
39
|
+
return getRootEntity(entityState, arg1, stateless);
|
|
40
|
+
}, [hasInitialEntity, contextEntity, stateless, entityState, arg1]);
|
|
41
|
+
const onEntityChange = useMemoizedFn((newEntity, oldEntity) => {
|
|
39
42
|
if (!stateless) {
|
|
40
|
-
|
|
43
|
+
setEntityState(newEntity);
|
|
41
44
|
}
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
triggerCallbacks(optionsRefCollection, newEntity, oldEntity);
|
|
46
|
+
});
|
|
47
|
+
const createReducers = useMemoizedFn(() => {
|
|
48
|
+
let reducers = {};
|
|
49
|
+
let entityReducers = {};
|
|
50
|
+
if (hasInitialEntity) {
|
|
51
|
+
const initOptions = options;
|
|
52
|
+
entityReducers = initEntityReducers(entity, entityUseCase, initOptions, onEntityChange);
|
|
44
53
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const initReducers = useMemoizedFn(() => {
|
|
48
|
-
const mergedOptions = { ...restUseCaseOptions, ...usecaseOptions };
|
|
49
|
-
if (!hasDefaultEntity) {
|
|
50
|
-
return contextEntityReducers || usecase(mergedOptions);
|
|
54
|
+
else if (contextEntityReducers) {
|
|
55
|
+
entityReducers = contextEntityReducers;
|
|
51
56
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...mergedOptions,
|
|
55
|
-
onChange(newEntity, prevEntity) {
|
|
56
|
-
if (hasDefaultEntity && !stateless) {
|
|
57
|
-
setEntityState(newEntity);
|
|
58
|
-
}
|
|
59
|
-
triggerCallbacks(optionsGetterListRef.current, newEntity, prevEntity);
|
|
60
|
-
},
|
|
61
|
-
onGenerate(newEntity, result) {
|
|
62
|
-
return result;
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
const reducers = useMemo.call(null, initReducers, [entity, initReducers, ...deps]);
|
|
67
|
-
const collectionReducers = useMemo(() => {
|
|
68
|
-
if (!hasDefaultEntity && contextCollectionReducers) {
|
|
69
|
-
return contextCollectionReducers;
|
|
57
|
+
else {
|
|
58
|
+
reducers = usecase(options);
|
|
70
59
|
}
|
|
71
|
-
return
|
|
72
|
-
}, [hasDefaultEntity, contextCollectionReducers]);
|
|
73
|
-
const contextValueWithCollectionReducers = useMemo(() => {
|
|
74
|
-
return [entity, reducers, collectionReducers];
|
|
75
|
-
}, [entity, reducers, collectionReducers]);
|
|
76
|
-
const contextValueWithCollectionReducersRef = useLatest(contextValueWithCollectionReducers);
|
|
77
|
-
const [Provider] = useState(() => {
|
|
78
|
-
return ({ children }) => {
|
|
79
|
-
const { Provider: ContextProvider } = context;
|
|
80
|
-
return React.createElement(ContextProvider, {
|
|
81
|
-
value: contextValueWithCollectionReducersRef.current,
|
|
82
|
-
}, children);
|
|
83
|
-
};
|
|
60
|
+
return [reducers, entityReducers];
|
|
84
61
|
});
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return
|
|
90
|
-
}, [
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
62
|
+
const createReducersDep = hasInitialEntity ? entity : contextEntityReducers;
|
|
63
|
+
const [reducers, entityReducers] = useCreation(() => {
|
|
64
|
+
void depsKey;
|
|
65
|
+
void createReducersDep;
|
|
66
|
+
return createReducers();
|
|
67
|
+
}, [createReducers, depsKey, createReducersDep]);
|
|
68
|
+
const Provider = useProvider(context, entity, entityReducers, optionsRefCollection);
|
|
69
|
+
const coreCollection = useCreation(() => {
|
|
70
|
+
return [entity, entityReducers, hasInitialEntity ? Provider : null];
|
|
71
|
+
}, [hasInitialEntity, entity, entityReducers, Provider]);
|
|
72
|
+
return useCreation(() => {
|
|
73
|
+
if (isContextMode) {
|
|
74
|
+
return coreCollection;
|
|
94
75
|
}
|
|
95
|
-
return
|
|
96
|
-
}, [
|
|
97
|
-
|
|
98
|
-
return result;
|
|
99
|
-
});
|
|
76
|
+
return reducers;
|
|
77
|
+
}, [isContextMode, reducers, coreCollection]);
|
|
78
|
+
};
|