ts-ioc-container 46.9.0 → 46.9.1
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/cjm/hooks/HookContext.js +3 -2
- package/cjm/hooks/hook.js +10 -2
- package/cjm/injector/IInjector.js +1 -0
- package/cjm/utils/proxy.js +23 -3
- package/esm/hooks/HookContext.js +3 -2
- package/esm/hooks/hook.js +10 -2
- package/esm/injector/IInjector.js +1 -0
- package/esm/utils/proxy.js +21 -3
- package/package.json +1 -1
- package/typings/hooks/HookContext.d.ts +1 -1
- package/typings/utils/proxy.d.ts +4 -2
package/cjm/hooks/HookContext.js
CHANGED
|
@@ -2,15 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createHookContext = exports.HookContext = void 0;
|
|
4
4
|
const inject_1 = require("../injector/inject");
|
|
5
|
+
const proxy_1 = require("../utils/proxy");
|
|
5
6
|
class HookContext {
|
|
6
|
-
instance;
|
|
7
7
|
scope;
|
|
8
8
|
methodName;
|
|
9
|
+
instance;
|
|
9
10
|
initialArgs = [];
|
|
10
11
|
constructor(instance, scope, methodName) {
|
|
11
|
-
this.instance = instance;
|
|
12
12
|
this.scope = scope;
|
|
13
13
|
this.methodName = methodName;
|
|
14
|
+
this.instance = (0, proxy_1.isProxy)(instance) ? (0, proxy_1.getProxyTarget)(instance) : instance;
|
|
14
15
|
}
|
|
15
16
|
resolveArgs(...args) {
|
|
16
17
|
return (0, inject_1.resolveArgs)(this.instance.constructor, this.methodName)(this.scope, ...[...this.initialArgs, ...args]);
|
package/cjm/hooks/hook.js
CHANGED
|
@@ -4,17 +4,25 @@ exports.hook = exports.toHookFn = void 0;
|
|
|
4
4
|
exports.getHooks = getHooks;
|
|
5
5
|
exports.hasHooks = hasHooks;
|
|
6
6
|
const basic_1 = require("../utils/basic");
|
|
7
|
+
const proxy_1 = require("../utils/proxy");
|
|
7
8
|
const isHookClassConstructor = (execute) => {
|
|
8
9
|
return basic_1.Is.constructor(execute) && execute.prototype.execute;
|
|
9
10
|
};
|
|
10
11
|
const toHookFn = (execute) => isHookClassConstructor(execute) ? (context) => context.scope.resolve(execute).execute(context) : execute;
|
|
11
12
|
exports.toHookFn = toHookFn;
|
|
13
|
+
const getReflectionTarget = (target) => {
|
|
14
|
+
return (0, proxy_1.isProxy)(target) ? (0, proxy_1.getProxyTarget)(target) : target;
|
|
15
|
+
};
|
|
12
16
|
// Get hooks metadata
|
|
13
17
|
function getHooks(target, key) {
|
|
14
|
-
|
|
18
|
+
const reflectionTarget = getReflectionTarget(target);
|
|
19
|
+
return Reflect.hasMetadata(key, reflectionTarget.constructor)
|
|
20
|
+
? Reflect.getMetadata(key, reflectionTarget.constructor)
|
|
21
|
+
: new Map();
|
|
15
22
|
}
|
|
16
23
|
function hasHooks(target, key) {
|
|
17
|
-
|
|
24
|
+
const reflectionTarget = getReflectionTarget(target);
|
|
25
|
+
return Reflect.hasMetadata(key, reflectionTarget.constructor);
|
|
18
26
|
}
|
|
19
27
|
// Hook decorator
|
|
20
28
|
const hook = (key, ...fns) => (target, propertyKey) => {
|
|
@@ -4,6 +4,7 @@ exports.Injector = void 0;
|
|
|
4
4
|
const proxy_1 = require("../utils/proxy");
|
|
5
5
|
class Injector {
|
|
6
6
|
resolve(scope, Target, { args, lazy } = {}) {
|
|
7
|
+
// @ts-ignore
|
|
7
8
|
return (0, proxy_1.toLazyIf)(() => {
|
|
8
9
|
const instance = this.createInstance(scope, Target, { args });
|
|
9
10
|
scope.addInstance(instance);
|
package/cjm/utils/proxy.js
CHANGED
|
@@ -1,16 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isProxy = isProxy;
|
|
4
|
+
exports.getProxyTarget = getProxyTarget;
|
|
3
5
|
exports.lazyProxy = lazyProxy;
|
|
4
6
|
exports.toLazyIf = toLazyIf;
|
|
7
|
+
const proxyStateMap = new WeakMap();
|
|
8
|
+
const unwrapProxyTarget = (value) => {
|
|
9
|
+
return isProxy(value) ? getProxyTarget(value) : value;
|
|
10
|
+
};
|
|
11
|
+
function isProxy(value) {
|
|
12
|
+
return proxyStateMap.has(value);
|
|
13
|
+
}
|
|
14
|
+
function getProxyTarget(value) {
|
|
15
|
+
return proxyStateMap.get(value).getTarget();
|
|
16
|
+
}
|
|
5
17
|
function lazyProxy(resolveInstance) {
|
|
6
18
|
let instance;
|
|
7
|
-
|
|
19
|
+
const state = {
|
|
20
|
+
getTarget: () => {
|
|
21
|
+
instance = instance ?? unwrapProxyTarget(resolveInstance());
|
|
22
|
+
return instance;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
const proxy = new Proxy({}, {
|
|
8
26
|
get: (_, prop) => {
|
|
9
|
-
|
|
27
|
+
const target = state.getTarget();
|
|
10
28
|
// @ts-ignore
|
|
11
|
-
return
|
|
29
|
+
return target[prop];
|
|
12
30
|
},
|
|
13
31
|
});
|
|
32
|
+
proxyStateMap.set(proxy, state);
|
|
33
|
+
return proxy;
|
|
14
34
|
}
|
|
15
35
|
function toLazyIf(resolveInstance, isLazy = false) {
|
|
16
36
|
if (isLazy) {
|
package/esm/hooks/HookContext.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { resolveArgs } from '../injector/inject';
|
|
2
|
+
import { getProxyTarget, isProxy } from '../utils/proxy';
|
|
2
3
|
export class HookContext {
|
|
3
|
-
instance;
|
|
4
4
|
scope;
|
|
5
5
|
methodName;
|
|
6
|
+
instance;
|
|
6
7
|
initialArgs = [];
|
|
7
8
|
constructor(instance, scope, methodName) {
|
|
8
|
-
this.instance = instance;
|
|
9
9
|
this.scope = scope;
|
|
10
10
|
this.methodName = methodName;
|
|
11
|
+
this.instance = isProxy(instance) ? getProxyTarget(instance) : instance;
|
|
11
12
|
}
|
|
12
13
|
resolveArgs(...args) {
|
|
13
14
|
return resolveArgs(this.instance.constructor, this.methodName)(this.scope, ...[...this.initialArgs, ...args]);
|
package/esm/hooks/hook.js
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import { Is } from '../utils/basic';
|
|
2
|
+
import { getProxyTarget, isProxy } from '../utils/proxy';
|
|
2
3
|
const isHookClassConstructor = (execute) => {
|
|
3
4
|
return Is.constructor(execute) && execute.prototype.execute;
|
|
4
5
|
};
|
|
5
6
|
export const toHookFn = (execute) => isHookClassConstructor(execute) ? (context) => context.scope.resolve(execute).execute(context) : execute;
|
|
7
|
+
const getReflectionTarget = (target) => {
|
|
8
|
+
return isProxy(target) ? getProxyTarget(target) : target;
|
|
9
|
+
};
|
|
6
10
|
// Get hooks metadata
|
|
7
11
|
export function getHooks(target, key) {
|
|
8
|
-
|
|
12
|
+
const reflectionTarget = getReflectionTarget(target);
|
|
13
|
+
return Reflect.hasMetadata(key, reflectionTarget.constructor)
|
|
14
|
+
? Reflect.getMetadata(key, reflectionTarget.constructor)
|
|
15
|
+
: new Map();
|
|
9
16
|
}
|
|
10
17
|
export function hasHooks(target, key) {
|
|
11
|
-
|
|
18
|
+
const reflectionTarget = getReflectionTarget(target);
|
|
19
|
+
return Reflect.hasMetadata(key, reflectionTarget.constructor);
|
|
12
20
|
}
|
|
13
21
|
// Hook decorator
|
|
14
22
|
export const hook = (key, ...fns) => (target, propertyKey) => {
|
package/esm/utils/proxy.js
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
|
+
const proxyStateMap = new WeakMap();
|
|
2
|
+
const unwrapProxyTarget = (value) => {
|
|
3
|
+
return isProxy(value) ? getProxyTarget(value) : value;
|
|
4
|
+
};
|
|
5
|
+
export function isProxy(value) {
|
|
6
|
+
return proxyStateMap.has(value);
|
|
7
|
+
}
|
|
8
|
+
export function getProxyTarget(value) {
|
|
9
|
+
return proxyStateMap.get(value).getTarget();
|
|
10
|
+
}
|
|
1
11
|
export function lazyProxy(resolveInstance) {
|
|
2
12
|
let instance;
|
|
3
|
-
|
|
13
|
+
const state = {
|
|
14
|
+
getTarget: () => {
|
|
15
|
+
instance = instance ?? unwrapProxyTarget(resolveInstance());
|
|
16
|
+
return instance;
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
const proxy = new Proxy({}, {
|
|
4
20
|
get: (_, prop) => {
|
|
5
|
-
|
|
21
|
+
const target = state.getTarget();
|
|
6
22
|
// @ts-ignore
|
|
7
|
-
return
|
|
23
|
+
return target[prop];
|
|
8
24
|
},
|
|
9
25
|
});
|
|
26
|
+
proxyStateMap.set(proxy, state);
|
|
27
|
+
return proxy;
|
|
10
28
|
}
|
|
11
29
|
export function toLazyIf(resolveInstance, isLazy = false) {
|
|
12
30
|
if (isLazy) {
|
package/package.json
CHANGED
|
@@ -12,9 +12,9 @@ export interface IHookContext {
|
|
|
12
12
|
setInitialArgs(...args: unknown[]): this;
|
|
13
13
|
}
|
|
14
14
|
export declare class HookContext implements IHookContext {
|
|
15
|
-
instance: object;
|
|
16
15
|
scope: IContainer;
|
|
17
16
|
methodName?: string | undefined;
|
|
17
|
+
readonly instance: object;
|
|
18
18
|
private initialArgs;
|
|
19
19
|
constructor(instance: object, scope: IContainer, methodName?: string | undefined);
|
|
20
20
|
resolveArgs(...args: unknown[]): unknown[];
|
package/typings/utils/proxy.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export declare function
|
|
2
|
-
export declare function
|
|
1
|
+
export declare function isProxy(value: object): boolean;
|
|
2
|
+
export declare function getProxyTarget<T extends object>(value: T): T;
|
|
3
|
+
export declare function lazyProxy<T extends object>(resolveInstance: () => T): T;
|
|
4
|
+
export declare function toLazyIf<T extends object>(resolveInstance: () => T, isLazy?: boolean): T;
|