// @ts-nocheck import { useState, useEffect, useContext, useRef } from 'react'; // @ts-ignore import isEqual from 'C:/Users/Administrator/Desktop/setting/mz-web/node_modules/@umijs/plugin-model/node_modules/fast-deep-equal/index.js'; // @ts-ignore import { UmiContext } from 'C:/Users/Administrator/Desktop/setting/mz-web/node_modules/@umijs/plugin-model/lib/helpers/constant'; import { Model, models } from './Provider'; export type Models = Model[T] export function useModel>(model: T): Model[T] export function useModel, U>(model: T, selector: (model: Model[T]) => U): U export function useModel, U>( namespace: T, updater?: (model: Model[T]) => U ) : typeof updater extends undefined ? Model[T] : ReturnType>{ type RetState = typeof updater extends undefined ? Model[T] : ReturnType> const dispatcher = useContext(UmiContext); const updaterRef = useRef(updater); updaterRef.current = updater; const [state, setState] = useState( () => updaterRef.current ? updaterRef.current(dispatcher.data![namespace]) : dispatcher.data![namespace] ); const stateRef = useRef(state); stateRef.current = state; useEffect(() => { const handler = (e: any) => { if(updater && updaterRef.current){ const currentState = updaterRef.current(e); const previousState = stateRef.current if(!isEqual(currentState, previousState)){ setState(currentState); } } else { setState(e); } } try { dispatcher.callbacks![namespace]!.add(handler); } catch (e) { dispatcher.callbacks![namespace] = new Set(); dispatcher.callbacks![namespace]!.add(handler); } return () => { dispatcher.callbacks![namespace]!.delete(handler); } }, [namespace]); return state; };