import {getCurrentDesignInstance, onBeforeUnmount} from '@peryl/react-compose'; import {delay} from "@peryl/utils/delay"; type Shift = T extends [infer L, ...infer R] ? R : T export function useFunctionWrapper any>( key: string, func: T, ): (...args: Shift>) => ReturnType { return (...args) => { const ctx = getCurrentDesignInstance() as any; if (!ctx._use) ctx._use = {}; onBeforeUnmount(() => delay().then(() => {delete ctx._use;})); if (!!ctx._use[key]) { throw new Error(`use ${key} can only be executed once!`); } return (ctx._use[key] = func(ctx, ...args)); }; } /*const func = useFunctionWrapper('', (ctx, name: string, age: number) => { return { sayHello: () => {}, } }) const data = func('111', 111) data.sayHello()*/