Comparing version 4.0.1 to 4.1.0
import { ServiceKey } from './types.js'; | ||
export type ServiceProxy<T> = T; | ||
export declare function dependency<T>(thisArg: any, serviceKey: ServiceKey<T>): T; | ||
export declare function dependency<T>(thisArg: any, serviceKey: ServiceKey<T>, cache?: boolean): T; |
import { MESH_REF } from './mesh.js'; | ||
export function dependency(thisArg, serviceKey) { | ||
const handler = new Proxy({}, { | ||
get(_target, trap) { | ||
return function (_target, ...args) { | ||
const instance = thisArg[MESH_REF].resolve(serviceKey); | ||
return Reflect[trap](instance, ...args); | ||
}; | ||
} | ||
}); | ||
const PROXY_TRAPS = Object.getOwnPropertyNames(Reflect); | ||
export function dependency(thisArg, serviceKey, cache = true) { | ||
let cachedInstance; | ||
const handler = {}; | ||
for (const trap of PROXY_TRAPS) { | ||
handler[trap] = function (_target, ...args) { | ||
let instance = cachedInstance; | ||
if (!instance) { | ||
instance = thisArg[MESH_REF].resolve(serviceKey); | ||
} | ||
if (cache) { | ||
cachedInstance = instance; | ||
} | ||
return Reflect[trap](instance, ...args); | ||
}; | ||
} | ||
return new Proxy(thisArg, handler); | ||
} | ||
//# sourceMappingURL=proxy.js.map |
{ | ||
"name": "mesh-ioc", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Powerful and Lightweight IoC Library", | ||
@@ -42,3 +42,3 @@ "type": "module", | ||
"devDependencies": { | ||
"@nodescript/eslint-config": "^2.0.2", | ||
"@nodescript/eslint-config": "^2.0.3", | ||
"@types/mocha": "^10.0.10", | ||
@@ -45,0 +45,0 @@ "@types/node": "^22.10.2", |
Sorry, the diff of this file is not supported yet
41337
397