diff --git a/src/utils/proxy.ts b/src/utils/proxy.ts index efae4bc..ac1a8fe 100644 --- a/src/utils/proxy.ts +++ b/src/utils/proxy.ts @@ -7,6 +7,7 @@ import { } from "../preference"; import type { AnyWidget, + ApplyAttributes, CheckBox, Composite, JSXAttributes, @@ -49,53 +50,15 @@ export type Callback = | CallbackInstance | Callable; -export function createProxies( - funReceivedProxy: Callback, - funAppend?: CallbackAppend -) { - const ProxiesCallback: typeof funReceivedProxy = new Proxy( - funReceivedProxy, +export function createProxies(component: Callback) { + const ProxiesCallback = new Proxy( + component, { - construct( - target: CallbackInstance, - argArray - ) { - return new target(argArray[0]); - /*return new Proxy>>( - target(argArray[0]), - { - get( - instance, - prop: Extract< - keyof Omit, symbol>, - Callback - > - ) { - if (prop === "append") { - return (...widgets: any[]) => ( - instance.append(...widgets), - typeof funAppend === "function" - ? funAppend(widgets) - : void 0, - instance - ); - } - return typeof instance[prop] === "function" - ? (instance[prop] as Function).bind(instance) - : instance[prop]; - }, - set(instance, prop: string, value: any) { - //@ts-ignore - instance[prop] = value; - return true; - }, - } - ) as T;*/ - }, - apply(_, __, argArray): InstanceType> { - return new (ProxiesCallback as CallbackInstance)( - argArray[0] - ); + /*construct(target, argArray) { + return new target(argArray[0] as Attributes); + },*/ + apply(target, __, argArray) { + return Reflect.construct(target, argArray); }, } ); @@ -103,8 +66,26 @@ export function createProxies( return ProxiesCallback; } -export function factory(fac: T) { - if (true) { - +type Args = C extends Composite ? Attributes[] : any[]; +interface Constructor extends Function { + new(...args: Args): T; + (...iargs: Args): T; +} + +export function factory< + T extends Function, +>(fac: T) { + type Caller = Constructor; + const handler: ProxyHandler> = { + get(target, p, receiver) { + return Reflect.get(target, p, receiver); + }, + apply(target, thisArg, argArray) { + if (typeof target.name === 'string' && typeof target.constructor === 'function') { + return Reflect.construct(target, thisArg); + } + return Reflect.apply(null, target, thisArg); + } } + return new Proxy(fac as unknown as Caller, handler); } \ No newline at end of file