feat(lib/utils/proxy): add unproxify function, improve typings, add workarounds
This commit is contained in:
parent
b727aaa208
commit
337ed6e05b
1 changed files with 54 additions and 16 deletions
|
|
@ -17,20 +17,46 @@ export function getProxyTarget(obj: object) {
|
||||||
return _targets.get(obj)
|
return _targets.get(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
const proxifyMetadatas = new WeakMap<object, [() => 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(
|
const _proxifyMetadatas = new WeakMap<object, [() => unknown, cacheable: boolean, cache?: unknown]>()
|
||||||
Object.entries(Reflect).map(([k, fn]) => [
|
|
||||||
k,
|
const _proxifyHandler = {
|
||||||
(hint: object, ...args: any[]) => {
|
...Object.fromEntries(
|
||||||
const m = proxifyMetadatas.get(hint)!
|
Object.getOwnPropertyNames(Reflect).map(k => [
|
||||||
// @ts-expect-error
|
k,
|
||||||
if (m[1]) return fn(m[2] ?? (m[2] = m[0]()), ...args)
|
(hint: object, ...args: any[]) => {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
return fn(m[0](), ...args)
|
const fn = Reflect[k]
|
||||||
},
|
const meta = _proxifyMetadatas.get(hint)!
|
||||||
]),
|
if (meta[1]) return fn(meta[2] ?? (meta[2] = meta[0]()), ...args)
|
||||||
) as ProxyHandler<object>
|
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<object>
|
||||||
|
|
||||||
export type ProxifyOptions = {
|
export type ProxifyOptions = {
|
||||||
/**
|
/**
|
||||||
|
|
@ -51,9 +77,21 @@ export type ProxifyOptions = {
|
||||||
* @param options The options to use for the proxified value.
|
* @param options The options to use for the proxified value.
|
||||||
* @returns A proxified value that will be updated when the signal is updated.
|
* @returns A proxified value that will be updated when the signal is updated.
|
||||||
*/
|
*/
|
||||||
export function proxify(signal: () => unknown, options?: ProxifyOptions) {
|
export function proxify<T>(signal: () => T, options?: ProxifyOptions): T | undefined {
|
||||||
// biome-ignore lint/complexity/useArrowFunction: We need a function with a constructor
|
// biome-ignore lint/complexity/useArrowFunction: We need a function with a constructor
|
||||||
const hint = (options?.hint === 'object' ? {} : function () {}) as any
|
const hint = (options?.hint === 'object' ? {} : function () {}) as any
|
||||||
proxifyMetadatas.set(hint, [signal, options?.cache ?? false])
|
_proxifyMetadatas.set(hint, [signal, options?.cache ?? false])
|
||||||
return new Proxy(hint, proxifyHandler)
|
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<T extends object>(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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue