UNPKG

@mic-rexjs/usecases-react

Version:

React-based solution for use usecases of Clean Architecture

57 lines 2.7 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import { useArgumentTypes } from '../useArgumentTypes'; import { useConstantFn } from '../useConstantFn'; import { useConstantReducers } from '../useConstantReducers'; import { useContext } from '../useContext'; import { useEntity } from '../useEntity'; import { useFullArguments } from '../useFullArguments'; import { useIsRenderingRef } from '../useIsRenderingRef'; import { useProvider } from '../useProvider'; import { useReducers } from '../useReducers'; import { useStatuses } from '../useStatuses'; import { useStore } from '../useStore'; import { useCreation } from 'ahooks'; import { Statuses } from '../../enums/Statuses'; import { methodUseCase } from '../../usecases/methodUseCase'; export const useUseCase = function () { const isRenderingRef = useIsRenderingRef(); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } const argumentTypes = useArgumentTypes(args); const fullArguments = useFullArguments(args, argumentTypes); const _fullArguments = _slicedToArray(fullArguments, 4), unsafeEntity = _fullArguments[0], unsafeUsecase = _fullArguments[1], options = _fullArguments[2], deps = _fullArguments[3]; const _useConstantReducers = useConstantReducers(methodUseCase), cacheCalls = _useConstantReducers.cacheCalls; const usecase = useConstantFn(unsafeUsecase); const context = useContext(usecase, argumentTypes); const entityContext = context; const statuses = useStatuses(context, argumentTypes); const store = useStore(unsafeEntity, entityContext, statuses, options, deps); const reducers = useReducers(usecase, context, store, statuses, options, deps); const entity = useEntity(unsafeEntity, store, reducers, statuses, options); const Provider = useProvider(statuses, context, store, reducers); return useCreation(() => { if ((statuses & Statuses.EntityEnabled) !== Statuses.EntityEnabled) { return reducers; } const cachedReducers = cacheCalls(reducers, { onShouldCache() { return isRenderingRef.current; } }); const isNull = entity === null; const isArray = Array.isArray(entity); const isObjectType = typeof entity === 'object'; const isObjectEntity = isObjectType && !isNull && !isArray; // 这里是为了让访问器 `getter` 的值保持唯一性,否则每次 `getter` 返回的对象都是新对象,无法用于 `deps` const newEntity = isObjectEntity ? { ...entity } : entity; return [newEntity, cachedReducers, Provider]; }, [statuses, entity, reducers, Provider, isRenderingRef, cacheCalls]); };