// @ts-nocheck import { useState, useEffect, useContext, useRef } from 'react'; // @ts-ignore import isEqual from '/Users/zhaolinxiao/workspace/lwj-common-frontend/lwj-react/lwj-editor/node_modules/@umijs/plugin-model/node_modules/fast-deep-equal/index.js'; // @ts-ignore import { UmiContext } from './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; const isMount = useRef(false); useEffect(() => { isMount.current = true; return () => { isMount.current = false; } }, []) useEffect(() => { const handler = (e: any) => { if(!isMount.current) { // 如果 handler 执行过程中,组件被卸载了,则强制更新全局 data setTimeout(() => { dispatcher.data![namespace] = e; dispatcher.update(namespace); }); } else { 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); dispatcher.update(namespace); } catch (e) { dispatcher.callbacks![namespace] = new Set(); dispatcher.callbacks![namespace]!.add(handler); dispatcher.update(namespace); } return () => { dispatcher.callbacks![namespace]!.delete(handler); } }, [namespace]); return state; };