From 337ed6e05bd6eefebecb21c3ce04ed6f596ff7ce Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Wed, 9 Apr 2025 03:33:17 +0700 Subject: [PATCH] feat(lib/utils/proxy): add `unproxify` function, improve typings, add workarounds --- lib/utils/src/proxy.ts | 70 ++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/lib/utils/src/proxy.ts b/lib/utils/src/proxy.ts index 3eed20e..5d16d8f 100644 --- a/lib/utils/src/proxy.ts +++ b/lib/utils/src/proxy.ts @@ -17,20 +17,46 @@ export function getProxyTarget(obj: object) { return _targets.get(obj) } -const proxifyMetadatas = new WeakMap unknown, cacheable: boolean, cache?: unknown]>() +// Heavily inspired by Wintry's lazy utils, but more optimized and stripped down. +// https://github.com/pylixonly/wintry/blob/main/src/utils/lazy.ts -const proxifyHandler = Object.fromEntries( - Object.entries(Reflect).map(([k, fn]) => [ - k, - (hint: object, ...args: any[]) => { - const m = proxifyMetadatas.get(hint)! - // @ts-expect-error - if (m[1]) return fn(m[2] ?? (m[2] = m[0]()), ...args) - // @ts-expect-error - return fn(m[0](), ...args) - }, - ]), -) as ProxyHandler +const _proxifyMetadatas = new WeakMap unknown, cacheable: boolean, cache?: unknown]>() + +const _proxifyHandler = { + ...Object.fromEntries( + Object.getOwnPropertyNames(Reflect).map(k => [ + k, + (hint: object, ...args: any[]) => { + // @ts-expect-error + const fn = Reflect[k] + const meta = _proxifyMetadatas.get(hint)! + if (meta[1]) return fn(meta[2] ?? (meta[2] = meta[0]()), ...args) + return fn(meta[0](), ...args) + }, + ]), + ), + // Workaround to fix functions that the correct `this` + get: (hint, p, recv) => { + const meta = _proxifyMetadatas.get(hint)! + const metaTarget = meta[1] ? (meta[2] ?? (meta[2] = meta[0]())) : meta[0]() + const val = Reflect.get(metaTarget!, p, recv) + + if (val && val.__proto__ === Function.prototype) + return new Proxy(val, { + // If thisArg happens to be a proxified value, we will use the target object instead + apply: (fn, thisArg, args) => Reflect.apply(fn, thisArg === recv ? metaTarget : thisArg, args), + }) + + return val + }, + // Workaround to fix: + // TypeError: getOwnPropertyDescriptor trap result is not configurable but target property '...' is configurable or non-existent + getOwnPropertyDescriptor: (hint, p) => { + const d = Reflect.getOwnPropertyDescriptor(hint, p) + if (d && !Reflect.getOwnPropertyDescriptor(hint, p)) Object.defineProperty(hint, p, d) + return d + }, +} as ProxyHandler export type ProxifyOptions = { /** @@ -51,9 +77,21 @@ export type ProxifyOptions = { * @param options The options to use for the proxified value. * @returns A proxified value that will be updated when the signal is updated. */ -export function proxify(signal: () => unknown, options?: ProxifyOptions) { +export function proxify(signal: () => T, options?: ProxifyOptions): T | undefined { // biome-ignore lint/complexity/useArrowFunction: We need a function with a constructor const hint = (options?.hint === 'object' ? {} : function () {}) as any - proxifyMetadatas.set(hint, [signal, options?.cache ?? false]) - return new Proxy(hint, proxifyHandler) + _proxifyMetadatas.set(hint, [signal, options?.cache ?? false]) + return new Proxy(hint, _proxifyHandler) +} + +/** + * Get the value of a proxified value at the current moment. + * @param proxified The proxified value. + * @returns The unproxified value. + */ +export function unproxify(proxified: T): T | undefined { + const meta = _proxifyMetadatas.get(getProxyTarget(proxified)!) + if (!meta) throw new TypeError(`${typeof proxified} is not a proxified value`) + if (meta[1]) return meta[2] ?? ((meta[2] = meta[0]()) as any) + return meta[0]() as any }