@tmlmobilidade/utils
Version:
A collection of utility functions and helpers for the TML Mobilidade Go monorepo, providing common functionality for batching operations, caching, HTTP requests, object manipulation, permissions, and more.
36 lines (35 loc) • 1.55 kB
JavaScript
/**
* Creates a recursive async proxy for a singleton class that delays method and property access until the instance is initialized.
* Supports nested property chains like `await proxy.core.agencies.findById(id)` by recursively wrapping each property access
* in a new proxy, resolving the full path only when the chain is invoked (called as a function or awaited).
*
* @param cls - A class with a static `getInstance` method that returns a promise resolving to the class instance.
* @returns A proxy object that intercepts property access and method calls, ensuring the instance is initialized before resolving.
*/
export function asyncSingletonProxy(cls) {
function createProxy(pathSegments) {
const resolver = (...args) => {
return (async () => {
const instance = await cls.getInstance();
let value = instance;
let receiver = undefined;
for (const seg of pathSegments) {
receiver = value;
value = Reflect.get(value, seg, value);
}
if (typeof value === 'function') {
return value.apply(receiver ?? instance, args);
}
return value;
})();
};
return new Proxy(resolver, {
get(_target, prop) {
if (prop === 'then')
return undefined;
return createProxy([...pathSegments, prop]);
},
});
}
return createProxy([]);
}