@tmlmobilidade/utils 20260709.1314.12 → 20260709.1326.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/singleton-proxy.d.ts +5 -2
- package/dist/singleton-proxy.js +28 -12
- package/package.json +1 -1
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Creates a proxy for a singleton class that delays method access until the instance is initialized.
|
|
2
|
+
* Creates a recursive async proxy for a singleton class that delays method and property access until the instance is initialized.
|
|
3
|
+
* Supports nested property chains like `await proxy.core.agencies.findById(id)` by recursively wrapping each property access
|
|
4
|
+
* in a new proxy, resolving the full path only when the chain is invoked (called as a function or awaited).
|
|
5
|
+
*
|
|
3
6
|
* @param cls - A class with a static `getInstance` method that returns a promise resolving to the class instance.
|
|
4
|
-
* @returns A proxy object that intercepts method calls
|
|
7
|
+
* @returns A proxy object that intercepts property access and method calls, ensuring the instance is initialized before resolving.
|
|
5
8
|
*/
|
|
6
9
|
export declare function asyncSingletonProxy<T extends object>(cls: {
|
|
7
10
|
getInstance: () => Promise<T>;
|
package/dist/singleton-proxy.js
CHANGED
|
@@ -1,19 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Creates a proxy for a singleton class that delays method access until the instance is initialized.
|
|
2
|
+
* Creates a recursive async proxy for a singleton class that delays method and property access until the instance is initialized.
|
|
3
|
+
* Supports nested property chains like `await proxy.core.agencies.findById(id)` by recursively wrapping each property access
|
|
4
|
+
* in a new proxy, resolving the full path only when the chain is invoked (called as a function or awaited).
|
|
5
|
+
*
|
|
3
6
|
* @param cls - A class with a static `getInstance` method that returns a promise resolving to the class instance.
|
|
4
|
-
* @returns A proxy object that intercepts method calls
|
|
7
|
+
* @returns A proxy object that intercepts property access and method calls, ensuring the instance is initialized before resolving.
|
|
5
8
|
*/
|
|
6
9
|
export function asyncSingletonProxy(cls) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return async (
|
|
10
|
+
function createProxy(pathSegments) {
|
|
11
|
+
const resolver = (...args) => {
|
|
12
|
+
return (async () => {
|
|
10
13
|
const instance = await cls.getInstance();
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
let value = instance;
|
|
15
|
+
let receiver = undefined;
|
|
16
|
+
for (const seg of pathSegments) {
|
|
17
|
+
receiver = value;
|
|
18
|
+
value = Reflect.get(value, seg, value);
|
|
14
19
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
if (typeof value === 'function') {
|
|
21
|
+
return value.apply(receiver ?? instance, args);
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
})();
|
|
25
|
+
};
|
|
26
|
+
return new Proxy(resolver, {
|
|
27
|
+
get(_target, prop) {
|
|
28
|
+
if (prop === 'then')
|
|
29
|
+
return undefined;
|
|
30
|
+
return createProxy([...pathSegments, prop]);
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return createProxy([]);
|
|
19
35
|
}
|