ts-ioc-container 37.0.2 → 37.0.3
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/by.js +6 -46
- package/cjm/container/AutoMockedContainer.js +0 -1
- package/cjm/container/Container.js +21 -24
- package/cjm/container/EmptyContainer.js +2 -3
- package/cjm/index.js +9 -7
- package/cjm/injector/inject.js +2 -2
- package/cjm/isDepKey.js +46 -0
- package/cjm/provider/{singleton/MultiCache.js → Cache.js} +16 -1
- package/cjm/provider/IProvider.js +6 -0
- package/cjm/provider/{singleton/SingletonProvider.js → SingletonProvider.js} +3 -3
- package/cjm/registration/IRegistration.js +2 -2
- package/cjm/registration/Registration.js +1 -2
- package/esm/by.js +5 -43
- package/esm/container/AutoMockedContainer.js +0 -1
- package/esm/container/Container.js +21 -24
- package/esm/container/EmptyContainer.js +2 -3
- package/esm/index.js +5 -3
- package/esm/injector/inject.js +1 -1
- package/esm/isDepKey.js +41 -0
- package/esm/provider/{singleton/MultiCache.js → Cache.js} +14 -0
- package/esm/provider/IProvider.js +6 -0
- package/esm/provider/{singleton/SingletonProvider.js → SingletonProvider.js} +2 -2
- package/esm/registration/IRegistration.js +1 -1
- package/esm/registration/Registration.js +1 -2
- package/package.json +1 -1
- package/typings/by.d.ts +2 -16
- package/typings/container/AutoMockedContainer.d.ts +0 -1
- package/typings/container/Container.d.ts +8 -5
- package/typings/container/EmptyContainer.d.ts +0 -1
- package/typings/container/IContainer.d.ts +1 -10
- package/typings/index.d.ts +7 -4
- package/typings/injector/inject.d.ts +1 -1
- package/typings/isDepKey.d.ts +16 -0
- package/typings/provider/Cache.d.ts +22 -0
- package/typings/provider/IProvider.d.ts +4 -1
- package/typings/provider/Provider.d.ts +1 -0
- package/typings/provider/{singleton/SingletonProvider.d.ts → SingletonProvider.d.ts} +3 -3
- package/typings/registration/IRegistration.d.ts +1 -1
- package/cjm/provider/singleton/Cache.js +0 -2
- package/cjm/provider/singleton/SingleCache.js +0 -18
- package/esm/provider/singleton/Cache.js +0 -1
- package/esm/provider/singleton/SingleCache.js +0 -14
- package/typings/provider/singleton/Cache.d.ts +0 -6
- package/typings/provider/singleton/MultiCache.d.ts +0 -10
- package/typings/provider/singleton/SingleCache.d.ts +0 -8
package/cjm/by.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const Registration_1 = require("./registration/Registration");
|
|
5
|
-
const Provider_1 = require("./provider/Provider");
|
|
3
|
+
exports.by = exports.byAlias = exports.byAliases = exports.IMemoKey = exports.all = void 0;
|
|
6
4
|
const all = () => true;
|
|
7
5
|
exports.all = all;
|
|
8
6
|
exports.IMemoKey = Symbol('IMemo');
|
|
@@ -26,12 +24,14 @@ const byAlias = (predicate, { memoize, lazy } = {}) => (c) => {
|
|
|
26
24
|
const predicateFn = (aliases) => predicate(aliases, c);
|
|
27
25
|
const memoKey = memoize?.(c);
|
|
28
26
|
if (memoKey === undefined) {
|
|
29
|
-
|
|
27
|
+
const [, result] = c.resolveOneByAlias(predicateFn, { lazy });
|
|
28
|
+
return result;
|
|
30
29
|
}
|
|
31
30
|
const memo = c.resolve(exports.IMemoKey);
|
|
32
31
|
const memoized = memo.get(memoKey);
|
|
33
32
|
if (memoized) {
|
|
34
|
-
|
|
33
|
+
const [key] = memoized;
|
|
34
|
+
return c.resolve(key, { lazy });
|
|
35
35
|
}
|
|
36
36
|
const [key, result] = c.resolveOneByAlias(predicateFn, { lazy });
|
|
37
37
|
memo.set(memoKey, [key]);
|
|
@@ -39,7 +39,7 @@ const byAlias = (predicate, { memoize, lazy } = {}) => (c) => {
|
|
|
39
39
|
};
|
|
40
40
|
exports.byAlias = byAlias;
|
|
41
41
|
exports.by = {
|
|
42
|
-
keys: (keys, { lazy } = {}) => (
|
|
42
|
+
keys: (keys, { lazy } = {}) => (c, ...args) => keys.map((t) => c.resolve(t, { args, lazy })),
|
|
43
43
|
key: (key, { args: deps = [], lazy } = {}) => (c, ...args) => c.resolve(key, { args: [...deps, ...args], lazy }),
|
|
44
44
|
instances: (predicate = exports.all) => (c) => {
|
|
45
45
|
const result = new Set(c.getInstances());
|
|
@@ -55,43 +55,3 @@ exports.by = {
|
|
|
55
55
|
create: (options) => (l) => l.createScope(options),
|
|
56
56
|
},
|
|
57
57
|
};
|
|
58
|
-
const isDepKey = (key) => {
|
|
59
|
-
return typeof key === 'object' && key !== null && 'key' in key;
|
|
60
|
-
};
|
|
61
|
-
exports.isDepKey = isDepKey;
|
|
62
|
-
const depKey = (key) => {
|
|
63
|
-
let isValidWhen;
|
|
64
|
-
const mappers = [];
|
|
65
|
-
return {
|
|
66
|
-
key,
|
|
67
|
-
assignTo: (registration) => {
|
|
68
|
-
let reg = registration.pipe(...mappers).fromKey(key);
|
|
69
|
-
if (isValidWhen) {
|
|
70
|
-
reg = registration.when(isValidWhen);
|
|
71
|
-
}
|
|
72
|
-
return reg;
|
|
73
|
-
},
|
|
74
|
-
register: (fn) => {
|
|
75
|
-
let registration = new Registration_1.Registration(() => new Provider_1.Provider(fn), key).pipe(...mappers);
|
|
76
|
-
if (isValidWhen) {
|
|
77
|
-
registration = registration.when(isValidWhen);
|
|
78
|
-
}
|
|
79
|
-
return registration;
|
|
80
|
-
},
|
|
81
|
-
resolve: (s, ...args) => exports.by.key(key)(s, ...args),
|
|
82
|
-
pipe(...values) {
|
|
83
|
-
mappers.push(...values);
|
|
84
|
-
return this;
|
|
85
|
-
},
|
|
86
|
-
to(target) {
|
|
87
|
-
key = target;
|
|
88
|
-
return this;
|
|
89
|
-
},
|
|
90
|
-
when(value) {
|
|
91
|
-
isValidWhen = value;
|
|
92
|
-
return this;
|
|
93
|
-
},
|
|
94
|
-
redirectFrom: (registration) => registration.redirectFrom(key),
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
exports.depKey = depKey;
|
|
@@ -13,12 +13,9 @@ class Container {
|
|
|
13
13
|
this.providers = new Map();
|
|
14
14
|
this.registrations = new Set();
|
|
15
15
|
this.parent = options.parent ?? new EmptyContainer_1.EmptyContainer();
|
|
16
|
-
this.tags = new Set(options.tags);
|
|
16
|
+
this.tags = new Set(options.tags ?? []);
|
|
17
|
+
this.onConstruct = options.onConstruct ?? (() => { });
|
|
17
18
|
this.onDispose = options.onDispose ?? (() => { });
|
|
18
|
-
// apply registrations from parent
|
|
19
|
-
for (const registration of this.parent.getRegistrations()) {
|
|
20
|
-
registration.applyTo(this);
|
|
21
|
-
}
|
|
22
19
|
}
|
|
23
20
|
add(registration) {
|
|
24
21
|
this.registrations.add(registration);
|
|
@@ -35,6 +32,7 @@ class Container {
|
|
|
35
32
|
if ((0, IContainer_1.isConstructor)(token)) {
|
|
36
33
|
const instance = this.injector.resolve(this, token, { args });
|
|
37
34
|
this.instances.add(instance);
|
|
35
|
+
this.onConstruct(instance);
|
|
38
36
|
return instance;
|
|
39
37
|
}
|
|
40
38
|
const provider = this.providers.get(token);
|
|
@@ -45,34 +43,25 @@ class Container {
|
|
|
45
43
|
createScope({ tags = [] } = {}) {
|
|
46
44
|
this.validateContainer();
|
|
47
45
|
const scope = new Container(this.injector, { parent: this, tags, onDispose: this.onDispose });
|
|
46
|
+
scope.applyRegistrationsFrom(this);
|
|
48
47
|
this.scopes.add(scope);
|
|
49
48
|
return scope;
|
|
50
49
|
}
|
|
51
|
-
dispose(
|
|
52
|
-
const { cascade = true } = options;
|
|
50
|
+
dispose() {
|
|
53
51
|
this.validateContainer();
|
|
54
52
|
this.isDisposed = true;
|
|
55
|
-
//
|
|
56
|
-
for (const scope of this.scopes) {
|
|
57
|
-
if (cascade) {
|
|
58
|
-
scope.dispose(options);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
scope.detach();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
// Unbind from parent
|
|
53
|
+
// Detach from parent
|
|
65
54
|
this.parent.removeScope(this);
|
|
66
55
|
this.parent = new EmptyContainer_1.EmptyContainer();
|
|
67
|
-
//
|
|
68
|
-
this.registrations.clear();
|
|
56
|
+
// Reset the state
|
|
69
57
|
this.providers.clear();
|
|
70
|
-
this.instances.forEach((instance) => this.onDispose(instance));
|
|
71
58
|
this.instances.clear();
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
59
|
+
this.registrations.clear();
|
|
60
|
+
this.onDispose(this);
|
|
61
|
+
// Dispose all scopes
|
|
62
|
+
for (const scope of this.scopes) {
|
|
63
|
+
scope.dispose();
|
|
64
|
+
}
|
|
76
65
|
}
|
|
77
66
|
use(module) {
|
|
78
67
|
module.applyTo(this);
|
|
@@ -109,6 +98,14 @@ class Container {
|
|
|
109
98
|
hasTag(tag) {
|
|
110
99
|
return this.tags.has(tag);
|
|
111
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* @private
|
|
103
|
+
*/
|
|
104
|
+
applyRegistrationsFrom(source) {
|
|
105
|
+
for (const registration of source.getRegistrations()) {
|
|
106
|
+
registration.applyTo(this);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
112
109
|
/**
|
|
113
110
|
* @private
|
|
114
111
|
*/
|
|
@@ -4,7 +4,6 @@ exports.EmptyContainer = void 0;
|
|
|
4
4
|
const MethodNotImplementedError_1 = require("../errors/MethodNotImplementedError");
|
|
5
5
|
const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
|
|
6
6
|
class EmptyContainer {
|
|
7
|
-
detach() { }
|
|
8
7
|
get isDisposed() {
|
|
9
8
|
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
10
9
|
}
|
|
@@ -33,7 +32,7 @@ class EmptyContainer {
|
|
|
33
32
|
throw new DependencyNotFoundError_1.DependencyNotFoundError(`Cannot find ${key.toString()}`);
|
|
34
33
|
}
|
|
35
34
|
hasTag(tag) {
|
|
36
|
-
|
|
35
|
+
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
37
36
|
}
|
|
38
37
|
getRegistrations() {
|
|
39
38
|
return [];
|
|
@@ -43,7 +42,7 @@ class EmptyContainer {
|
|
|
43
42
|
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
44
43
|
}
|
|
45
44
|
add(registration) {
|
|
46
|
-
|
|
45
|
+
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
47
46
|
}
|
|
48
47
|
resolveManyByAlias(predicate, options = {}, result = new Map()) {
|
|
49
48
|
return result;
|
package/cjm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
3
|
+
exports.depKey = exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.byAliases = exports.byAlias = exports.IMemoKey = exports.by = exports.HookContext = exports.runOnDisposeHooks = exports.runOnConstructHooks = exports.onConstruct = exports.onDispose = exports.injectProp = exports.runHooksAsync = exports.runHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.UnexpectedHookResultError = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.Registration = exports.redirectFrom = exports.register = exports.scope = exports.key = exports.decorate = exports.SingletonProvider = exports.singleton = exports.Provider = exports.ProviderDecorator = exports.args = exports.argsFn = exports.alias = exports.visible = exports.provider = exports.ProxyInjector = exports.SimpleInjector = exports.MetadataInjector = exports.resolveArgs = exports.inject = exports.AutoMockedContainer = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
4
|
+
exports.MultiCache = exports.multiCache = void 0;
|
|
5
5
|
// Containers
|
|
6
6
|
var IContainer_1 = require("./container/IContainer");
|
|
7
7
|
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
@@ -31,12 +31,9 @@ Object.defineProperty(exports, "args", { enumerable: true, get: function () { re
|
|
|
31
31
|
Object.defineProperty(exports, "ProviderDecorator", { enumerable: true, get: function () { return IProvider_1.ProviderDecorator; } });
|
|
32
32
|
var Provider_1 = require("./provider/Provider");
|
|
33
33
|
Object.defineProperty(exports, "Provider", { enumerable: true, get: function () { return Provider_1.Provider; } });
|
|
34
|
-
var SingletonProvider_1 = require("./provider/
|
|
34
|
+
var SingletonProvider_1 = require("./provider/SingletonProvider");
|
|
35
35
|
Object.defineProperty(exports, "singleton", { enumerable: true, get: function () { return SingletonProvider_1.singleton; } });
|
|
36
36
|
Object.defineProperty(exports, "SingletonProvider", { enumerable: true, get: function () { return SingletonProvider_1.SingletonProvider; } });
|
|
37
|
-
var MultiCache_1 = require("./provider/singleton/MultiCache");
|
|
38
|
-
Object.defineProperty(exports, "MultiCache", { enumerable: true, get: function () { return MultiCache_1.MultiCache; } });
|
|
39
|
-
Object.defineProperty(exports, "multiCache", { enumerable: true, get: function () { return MultiCache_1.multiCache; } });
|
|
40
37
|
var DecoratorProvider_1 = require("./provider/DecoratorProvider");
|
|
41
38
|
Object.defineProperty(exports, "decorate", { enumerable: true, get: function () { return DecoratorProvider_1.decorate; } });
|
|
42
39
|
// Registrations
|
|
@@ -76,7 +73,6 @@ Object.defineProperty(exports, "by", { enumerable: true, get: function () { retu
|
|
|
76
73
|
Object.defineProperty(exports, "IMemoKey", { enumerable: true, get: function () { return by_1.IMemoKey; } });
|
|
77
74
|
Object.defineProperty(exports, "byAlias", { enumerable: true, get: function () { return by_1.byAlias; } });
|
|
78
75
|
Object.defineProperty(exports, "byAliases", { enumerable: true, get: function () { return by_1.byAliases; } });
|
|
79
|
-
Object.defineProperty(exports, "depKey", { enumerable: true, get: function () { return by_1.depKey; } });
|
|
80
76
|
var metadata_1 = require("./metadata");
|
|
81
77
|
Object.defineProperty(exports, "setMetadata", { enumerable: true, get: function () { return metadata_1.setMetadata; } });
|
|
82
78
|
Object.defineProperty(exports, "getMetadata", { enumerable: true, get: function () { return metadata_1.getMetadata; } });
|
|
@@ -84,3 +80,9 @@ Object.defineProperty(exports, "setParameterMetadata", { enumerable: true, get:
|
|
|
84
80
|
Object.defineProperty(exports, "setMethodMetadata", { enumerable: true, get: function () { return metadata_1.setMethodMetadata; } });
|
|
85
81
|
Object.defineProperty(exports, "getMethodMetadata", { enumerable: true, get: function () { return metadata_1.getMethodMetadata; } });
|
|
86
82
|
Object.defineProperty(exports, "getParameterMetadata", { enumerable: true, get: function () { return metadata_1.getParameterMetadata; } });
|
|
83
|
+
var isDepKey_1 = require("./isDepKey");
|
|
84
|
+
Object.defineProperty(exports, "depKey", { enumerable: true, get: function () { return isDepKey_1.depKey; } });
|
|
85
|
+
var Cache_1 = require("./provider/Cache");
|
|
86
|
+
Object.defineProperty(exports, "multiCache", { enumerable: true, get: function () { return Cache_1.multiCache; } });
|
|
87
|
+
var Cache_2 = require("./provider/Cache");
|
|
88
|
+
Object.defineProperty(exports, "MultiCache", { enumerable: true, get: function () { return Cache_2.MultiCache; } });
|
package/cjm/injector/inject.js
CHANGED
|
@@ -4,9 +4,9 @@ exports.resolveArgs = exports.inject = void 0;
|
|
|
4
4
|
const metadata_1 = require("../metadata");
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
6
|
const HookContext_1 = require("../hooks/HookContext");
|
|
7
|
-
const
|
|
7
|
+
const isDepKey_1 = require("../isDepKey");
|
|
8
8
|
const inject = (fn) => (target, propertyKey, parameterIndex) => {
|
|
9
|
-
(0, metadata_1.setParameterMetadata)((0, HookContext_1.hookMetaKey)(propertyKey), (0,
|
|
9
|
+
(0, metadata_1.setParameterMetadata)((0, HookContext_1.hookMetaKey)(propertyKey), (0, isDepKey_1.isDepKey)(fn) ? fn.resolve : fn)((0, utils_1.isInstance)(target) ? target.constructor : target, propertyKey, parameterIndex);
|
|
10
10
|
};
|
|
11
11
|
exports.inject = inject;
|
|
12
12
|
const resolveArgs = (Target, methodName) => {
|
package/cjm/isDepKey.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.depKey = exports.isDepKey = void 0;
|
|
4
|
+
const Registration_1 = require("./registration/Registration");
|
|
5
|
+
const Provider_1 = require("./provider/Provider");
|
|
6
|
+
const by_1 = require("./by");
|
|
7
|
+
const isDepKey = (key) => {
|
|
8
|
+
return typeof key === 'object' && key !== null && 'key' in key;
|
|
9
|
+
};
|
|
10
|
+
exports.isDepKey = isDepKey;
|
|
11
|
+
const depKey = (key) => {
|
|
12
|
+
let isValidWhen;
|
|
13
|
+
const mappers = [];
|
|
14
|
+
return {
|
|
15
|
+
key,
|
|
16
|
+
assignTo: (registration) => {
|
|
17
|
+
let reg = registration.pipe(...mappers).fromKey(key);
|
|
18
|
+
if (isValidWhen) {
|
|
19
|
+
reg = registration.when(isValidWhen);
|
|
20
|
+
}
|
|
21
|
+
return reg;
|
|
22
|
+
},
|
|
23
|
+
register: (fn) => {
|
|
24
|
+
let registration = new Registration_1.Registration(() => new Provider_1.Provider(fn), key).pipe(...mappers);
|
|
25
|
+
if (isValidWhen) {
|
|
26
|
+
registration = registration.when(isValidWhen);
|
|
27
|
+
}
|
|
28
|
+
return registration;
|
|
29
|
+
},
|
|
30
|
+
resolve: (s, ...args) => by_1.by.key(key)(s, ...args),
|
|
31
|
+
pipe(...values) {
|
|
32
|
+
mappers.push(...values);
|
|
33
|
+
return this;
|
|
34
|
+
},
|
|
35
|
+
to(target) {
|
|
36
|
+
key = target;
|
|
37
|
+
return this;
|
|
38
|
+
},
|
|
39
|
+
when(value) {
|
|
40
|
+
isValidWhen = value;
|
|
41
|
+
return this;
|
|
42
|
+
},
|
|
43
|
+
redirectFrom: (registration) => registration.redirectFrom(key),
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.depKey = depKey;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.multiCache = exports.MultiCache = void 0;
|
|
3
|
+
exports.SingleCache = exports.multiCache = exports.MultiCache = void 0;
|
|
4
4
|
class MultiCache {
|
|
5
5
|
constructor(getKey = () => '1') {
|
|
6
6
|
this.getKey = getKey;
|
|
@@ -19,3 +19,18 @@ class MultiCache {
|
|
|
19
19
|
exports.MultiCache = MultiCache;
|
|
20
20
|
const multiCache = (getKey) => new MultiCache(getKey);
|
|
21
21
|
exports.multiCache = multiCache;
|
|
22
|
+
class SingleCache {
|
|
23
|
+
getKey(...args) {
|
|
24
|
+
return '1';
|
|
25
|
+
}
|
|
26
|
+
getValue(key) {
|
|
27
|
+
return this.instance.value;
|
|
28
|
+
}
|
|
29
|
+
hasValue(key) {
|
|
30
|
+
return this.instance !== undefined;
|
|
31
|
+
}
|
|
32
|
+
setValue(key, value) {
|
|
33
|
+
this.instance = { value };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.SingleCache = SingleCache;
|
|
@@ -24,6 +24,12 @@ class ProviderDecorator {
|
|
|
24
24
|
constructor(decorated) {
|
|
25
25
|
this.decorated = decorated;
|
|
26
26
|
}
|
|
27
|
+
get key() {
|
|
28
|
+
return this.decorated.key;
|
|
29
|
+
}
|
|
30
|
+
set key(value) {
|
|
31
|
+
this.decorated.key = value;
|
|
32
|
+
}
|
|
27
33
|
setVisibility(predicate) {
|
|
28
34
|
this.decorated.setVisibility(predicate);
|
|
29
35
|
return this;
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SingletonProvider = void 0;
|
|
4
4
|
exports.singleton = singleton;
|
|
5
|
-
const IProvider_1 = require("
|
|
6
|
-
const
|
|
5
|
+
const IProvider_1 = require("./IProvider");
|
|
6
|
+
const Cache_1 = require("./Cache");
|
|
7
7
|
function singleton(cacheProvider) {
|
|
8
|
-
return (provider) => new SingletonProvider(provider, cacheProvider ? cacheProvider() : new
|
|
8
|
+
return (provider) => new SingletonProvider(provider, cacheProvider ? cacheProvider() : new Cache_1.SingleCache());
|
|
9
9
|
}
|
|
10
10
|
class SingletonProvider extends IProvider_1.ProviderDecorator {
|
|
11
11
|
constructor(provider, cache) {
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.register = exports.getTransformers = exports.scope = exports.redirectFrom = exports.key = void 0;
|
|
4
4
|
const IContainer_1 = require("../container/IContainer");
|
|
5
5
|
const metadata_1 = require("../metadata");
|
|
6
|
-
const
|
|
6
|
+
const isDepKey_1 = require("../isDepKey");
|
|
7
7
|
const key = (...keys) => (r) => {
|
|
8
8
|
const [originalKey, ...redirectKeys] = keys;
|
|
9
9
|
return r.fromKey(originalKey).redirectFrom(...redirectKeys);
|
|
@@ -17,7 +17,7 @@ const METADATA_KEY = 'registration';
|
|
|
17
17
|
const getTransformers = (Target) => (0, metadata_1.getMetadata)(Target, METADATA_KEY) ?? [];
|
|
18
18
|
exports.getTransformers = getTransformers;
|
|
19
19
|
const register = (...mappers) => (0, metadata_1.setMetadata)(METADATA_KEY, mappers.map((m, index) => {
|
|
20
|
-
if ((0,
|
|
20
|
+
if ((0, isDepKey_1.isDepKey)(m)) {
|
|
21
21
|
return index === 0 ? m.assignTo.bind(m) : m.redirectFrom.bind(m);
|
|
22
22
|
}
|
|
23
23
|
if ((0, IContainer_1.isDependencyKey)(m)) {
|
|
@@ -63,8 +63,7 @@ class Registration {
|
|
|
63
63
|
throw new DependencyMissingKeyError_1.DependencyMissingKeyError('No key provided for registration');
|
|
64
64
|
}
|
|
65
65
|
const key = this.key;
|
|
66
|
-
|
|
67
|
-
container.register(key, provider);
|
|
66
|
+
container.register(key, this.createProvider(key).pipe(...this.mappers));
|
|
68
67
|
for (const redirectKey of this.redirectKeys) {
|
|
69
68
|
container.register(redirectKey, new Provider_1.Provider((s) => s.resolve(key)));
|
|
70
69
|
}
|
package/esm/by.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Registration } from './registration/Registration';
|
|
2
|
-
import { Provider } from './provider/Provider';
|
|
3
1
|
export const all = () => true;
|
|
4
2
|
export const IMemoKey = Symbol('IMemo');
|
|
5
3
|
export const byAliases = (predicate, { memoize, lazy } = {}) => (c) => {
|
|
@@ -21,19 +19,21 @@ export const byAlias = (predicate, { memoize, lazy } = {}) => (c) => {
|
|
|
21
19
|
const predicateFn = (aliases) => predicate(aliases, c);
|
|
22
20
|
const memoKey = memoize?.(c);
|
|
23
21
|
if (memoKey === undefined) {
|
|
24
|
-
|
|
22
|
+
const [, result] = c.resolveOneByAlias(predicateFn, { lazy });
|
|
23
|
+
return result;
|
|
25
24
|
}
|
|
26
25
|
const memo = c.resolve(IMemoKey);
|
|
27
26
|
const memoized = memo.get(memoKey);
|
|
28
27
|
if (memoized) {
|
|
29
|
-
|
|
28
|
+
const [key] = memoized;
|
|
29
|
+
return c.resolve(key, { lazy });
|
|
30
30
|
}
|
|
31
31
|
const [key, result] = c.resolveOneByAlias(predicateFn, { lazy });
|
|
32
32
|
memo.set(memoKey, [key]);
|
|
33
33
|
return result;
|
|
34
34
|
};
|
|
35
35
|
export const by = {
|
|
36
|
-
keys: (keys, { lazy } = {}) => (
|
|
36
|
+
keys: (keys, { lazy } = {}) => (c, ...args) => keys.map((t) => c.resolve(t, { args, lazy })),
|
|
37
37
|
key: (key, { args: deps = [], lazy } = {}) => (c, ...args) => c.resolve(key, { args: [...deps, ...args], lazy }),
|
|
38
38
|
instances: (predicate = all) => (c) => {
|
|
39
39
|
const result = new Set(c.getInstances());
|
|
@@ -49,41 +49,3 @@ export const by = {
|
|
|
49
49
|
create: (options) => (l) => l.createScope(options),
|
|
50
50
|
},
|
|
51
51
|
};
|
|
52
|
-
export const isDepKey = (key) => {
|
|
53
|
-
return typeof key === 'object' && key !== null && 'key' in key;
|
|
54
|
-
};
|
|
55
|
-
export const depKey = (key) => {
|
|
56
|
-
let isValidWhen;
|
|
57
|
-
const mappers = [];
|
|
58
|
-
return {
|
|
59
|
-
key,
|
|
60
|
-
assignTo: (registration) => {
|
|
61
|
-
let reg = registration.pipe(...mappers).fromKey(key);
|
|
62
|
-
if (isValidWhen) {
|
|
63
|
-
reg = registration.when(isValidWhen);
|
|
64
|
-
}
|
|
65
|
-
return reg;
|
|
66
|
-
},
|
|
67
|
-
register: (fn) => {
|
|
68
|
-
let registration = new Registration(() => new Provider(fn), key).pipe(...mappers);
|
|
69
|
-
if (isValidWhen) {
|
|
70
|
-
registration = registration.when(isValidWhen);
|
|
71
|
-
}
|
|
72
|
-
return registration;
|
|
73
|
-
},
|
|
74
|
-
resolve: (s, ...args) => by.key(key)(s, ...args),
|
|
75
|
-
pipe(...values) {
|
|
76
|
-
mappers.push(...values);
|
|
77
|
-
return this;
|
|
78
|
-
},
|
|
79
|
-
to(target) {
|
|
80
|
-
key = target;
|
|
81
|
-
return this;
|
|
82
|
-
},
|
|
83
|
-
when(value) {
|
|
84
|
-
isValidWhen = value;
|
|
85
|
-
return this;
|
|
86
|
-
},
|
|
87
|
-
redirectFrom: (registration) => registration.redirectFrom(key),
|
|
88
|
-
};
|
|
89
|
-
};
|
|
@@ -10,12 +10,9 @@ export class Container {
|
|
|
10
10
|
this.providers = new Map();
|
|
11
11
|
this.registrations = new Set();
|
|
12
12
|
this.parent = options.parent ?? new EmptyContainer();
|
|
13
|
-
this.tags = new Set(options.tags);
|
|
13
|
+
this.tags = new Set(options.tags ?? []);
|
|
14
|
+
this.onConstruct = options.onConstruct ?? (() => { });
|
|
14
15
|
this.onDispose = options.onDispose ?? (() => { });
|
|
15
|
-
// apply registrations from parent
|
|
16
|
-
for (const registration of this.parent.getRegistrations()) {
|
|
17
|
-
registration.applyTo(this);
|
|
18
|
-
}
|
|
19
16
|
}
|
|
20
17
|
add(registration) {
|
|
21
18
|
this.registrations.add(registration);
|
|
@@ -32,6 +29,7 @@ export class Container {
|
|
|
32
29
|
if (isConstructor(token)) {
|
|
33
30
|
const instance = this.injector.resolve(this, token, { args });
|
|
34
31
|
this.instances.add(instance);
|
|
32
|
+
this.onConstruct(instance);
|
|
35
33
|
return instance;
|
|
36
34
|
}
|
|
37
35
|
const provider = this.providers.get(token);
|
|
@@ -42,34 +40,25 @@ export class Container {
|
|
|
42
40
|
createScope({ tags = [] } = {}) {
|
|
43
41
|
this.validateContainer();
|
|
44
42
|
const scope = new Container(this.injector, { parent: this, tags, onDispose: this.onDispose });
|
|
43
|
+
scope.applyRegistrationsFrom(this);
|
|
45
44
|
this.scopes.add(scope);
|
|
46
45
|
return scope;
|
|
47
46
|
}
|
|
48
|
-
dispose(
|
|
49
|
-
const { cascade = true } = options;
|
|
47
|
+
dispose() {
|
|
50
48
|
this.validateContainer();
|
|
51
49
|
this.isDisposed = true;
|
|
52
|
-
//
|
|
53
|
-
for (const scope of this.scopes) {
|
|
54
|
-
if (cascade) {
|
|
55
|
-
scope.dispose(options);
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
scope.detach();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
// Unbind from parent
|
|
50
|
+
// Detach from parent
|
|
62
51
|
this.parent.removeScope(this);
|
|
63
52
|
this.parent = new EmptyContainer();
|
|
64
|
-
//
|
|
65
|
-
this.registrations.clear();
|
|
53
|
+
// Reset the state
|
|
66
54
|
this.providers.clear();
|
|
67
|
-
this.instances.forEach((instance) => this.onDispose(instance));
|
|
68
55
|
this.instances.clear();
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
56
|
+
this.registrations.clear();
|
|
57
|
+
this.onDispose(this);
|
|
58
|
+
// Dispose all scopes
|
|
59
|
+
for (const scope of this.scopes) {
|
|
60
|
+
scope.dispose();
|
|
61
|
+
}
|
|
73
62
|
}
|
|
74
63
|
use(module) {
|
|
75
64
|
module.applyTo(this);
|
|
@@ -106,6 +95,14 @@ export class Container {
|
|
|
106
95
|
hasTag(tag) {
|
|
107
96
|
return this.tags.has(tag);
|
|
108
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* @private
|
|
100
|
+
*/
|
|
101
|
+
applyRegistrationsFrom(source) {
|
|
102
|
+
for (const registration of source.getRegistrations()) {
|
|
103
|
+
registration.applyTo(this);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
109
106
|
/**
|
|
110
107
|
* @private
|
|
111
108
|
*/
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { MethodNotImplementedError } from '../errors/MethodNotImplementedError';
|
|
2
2
|
import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
|
|
3
3
|
export class EmptyContainer {
|
|
4
|
-
detach() { }
|
|
5
4
|
get isDisposed() {
|
|
6
5
|
throw new MethodNotImplementedError();
|
|
7
6
|
}
|
|
@@ -30,7 +29,7 @@ export class EmptyContainer {
|
|
|
30
29
|
throw new DependencyNotFoundError(`Cannot find ${key.toString()}`);
|
|
31
30
|
}
|
|
32
31
|
hasTag(tag) {
|
|
33
|
-
|
|
32
|
+
throw new MethodNotImplementedError();
|
|
34
33
|
}
|
|
35
34
|
getRegistrations() {
|
|
36
35
|
return [];
|
|
@@ -40,7 +39,7 @@ export class EmptyContainer {
|
|
|
40
39
|
throw new MethodNotImplementedError();
|
|
41
40
|
}
|
|
42
41
|
add(registration) {
|
|
43
|
-
|
|
42
|
+
throw new MethodNotImplementedError();
|
|
44
43
|
}
|
|
45
44
|
resolveManyByAlias(predicate, options = {}, result = new Map()) {
|
|
46
45
|
return result;
|
package/esm/index.js
CHANGED
|
@@ -11,8 +11,7 @@ export { ProxyInjector } from './injector/ProxyInjector';
|
|
|
11
11
|
// Providers
|
|
12
12
|
export { provider, visible, alias, argsFn, args, ProviderDecorator, } from './provider/IProvider';
|
|
13
13
|
export { Provider } from './provider/Provider';
|
|
14
|
-
export { singleton, SingletonProvider } from './provider/
|
|
15
|
-
export { MultiCache, multiCache } from './provider/singleton/MultiCache';
|
|
14
|
+
export { singleton, SingletonProvider } from './provider/SingletonProvider';
|
|
16
15
|
export { decorate } from './provider/DecoratorProvider';
|
|
17
16
|
// Registrations
|
|
18
17
|
export { key, scope, register, redirectFrom, } from './registration/IRegistration';
|
|
@@ -26,5 +25,8 @@ export { UnexpectedHookResultError } from './errors/UnexpectedHookResultError';
|
|
|
26
25
|
export { getHooks, hook, hasHooks, runHooks, runHooksAsync, injectProp, onDispose, onConstruct, runOnConstructHooks, runOnDisposeHooks, } from './hooks/hook';
|
|
27
26
|
export { HookContext } from './hooks/HookContext';
|
|
28
27
|
// Others
|
|
29
|
-
export { by, IMemoKey, byAlias, byAliases
|
|
28
|
+
export { by, IMemoKey, byAlias, byAliases } from './by';
|
|
30
29
|
export { setMetadata, getMetadata, setParameterMetadata, setMethodMetadata, getMethodMetadata, getParameterMetadata, } from './metadata';
|
|
30
|
+
export { depKey } from './isDepKey';
|
|
31
|
+
export { multiCache } from './provider/Cache';
|
|
32
|
+
export { MultiCache } from './provider/Cache';
|
package/esm/injector/inject.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getParameterMetadata, setParameterMetadata } from '../metadata';
|
|
2
2
|
import { constant, fillEmptyIndexes, isInstance } from '../utils';
|
|
3
3
|
import { hookMetaKey } from '../hooks/HookContext';
|
|
4
|
-
import { isDepKey } from '../
|
|
4
|
+
import { isDepKey } from '../isDepKey';
|
|
5
5
|
export const inject = (fn) => (target, propertyKey, parameterIndex) => {
|
|
6
6
|
setParameterMetadata(hookMetaKey(propertyKey), isDepKey(fn) ? fn.resolve : fn)(isInstance(target) ? target.constructor : target, propertyKey, parameterIndex);
|
|
7
7
|
};
|
package/esm/isDepKey.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Registration } from './registration/Registration';
|
|
2
|
+
import { Provider } from './provider/Provider';
|
|
3
|
+
import { by } from './by';
|
|
4
|
+
export const isDepKey = (key) => {
|
|
5
|
+
return typeof key === 'object' && key !== null && 'key' in key;
|
|
6
|
+
};
|
|
7
|
+
export const depKey = (key) => {
|
|
8
|
+
let isValidWhen;
|
|
9
|
+
const mappers = [];
|
|
10
|
+
return {
|
|
11
|
+
key,
|
|
12
|
+
assignTo: (registration) => {
|
|
13
|
+
let reg = registration.pipe(...mappers).fromKey(key);
|
|
14
|
+
if (isValidWhen) {
|
|
15
|
+
reg = registration.when(isValidWhen);
|
|
16
|
+
}
|
|
17
|
+
return reg;
|
|
18
|
+
},
|
|
19
|
+
register: (fn) => {
|
|
20
|
+
let registration = new Registration(() => new Provider(fn), key).pipe(...mappers);
|
|
21
|
+
if (isValidWhen) {
|
|
22
|
+
registration = registration.when(isValidWhen);
|
|
23
|
+
}
|
|
24
|
+
return registration;
|
|
25
|
+
},
|
|
26
|
+
resolve: (s, ...args) => by.key(key)(s, ...args),
|
|
27
|
+
pipe(...values) {
|
|
28
|
+
mappers.push(...values);
|
|
29
|
+
return this;
|
|
30
|
+
},
|
|
31
|
+
to(target) {
|
|
32
|
+
key = target;
|
|
33
|
+
return this;
|
|
34
|
+
},
|
|
35
|
+
when(value) {
|
|
36
|
+
isValidWhen = value;
|
|
37
|
+
return this;
|
|
38
|
+
},
|
|
39
|
+
redirectFrom: (registration) => registration.redirectFrom(key),
|
|
40
|
+
};
|
|
41
|
+
};
|
|
@@ -14,3 +14,17 @@ export class MultiCache {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
export const multiCache = (getKey) => new MultiCache(getKey);
|
|
17
|
+
export class SingleCache {
|
|
18
|
+
getKey(...args) {
|
|
19
|
+
return '1';
|
|
20
|
+
}
|
|
21
|
+
getValue(key) {
|
|
22
|
+
return this.instance.value;
|
|
23
|
+
}
|
|
24
|
+
hasValue(key) {
|
|
25
|
+
return this.instance !== undefined;
|
|
26
|
+
}
|
|
27
|
+
setValue(key, value) {
|
|
28
|
+
this.instance = { value };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -15,6 +15,12 @@ export class ProviderDecorator {
|
|
|
15
15
|
constructor(decorated) {
|
|
16
16
|
this.decorated = decorated;
|
|
17
17
|
}
|
|
18
|
+
get key() {
|
|
19
|
+
return this.decorated.key;
|
|
20
|
+
}
|
|
21
|
+
set key(value) {
|
|
22
|
+
this.decorated.key = value;
|
|
23
|
+
}
|
|
18
24
|
setVisibility(predicate) {
|
|
19
25
|
this.decorated.setVisibility(predicate);
|
|
20
26
|
return this;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ProviderDecorator } from '
|
|
2
|
-
import { SingleCache } from './
|
|
1
|
+
import { ProviderDecorator } from './IProvider';
|
|
2
|
+
import { SingleCache } from './Cache';
|
|
3
3
|
export function singleton(cacheProvider) {
|
|
4
4
|
return (provider) => new SingletonProvider(provider, cacheProvider ? cacheProvider() : new SingleCache());
|
|
5
5
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isDependencyKey } from '../container/IContainer';
|
|
2
2
|
import { getMetadata, setMetadata } from '../metadata';
|
|
3
|
-
import { isDepKey } from '../
|
|
3
|
+
import { isDepKey } from '../isDepKey';
|
|
4
4
|
export const key = (...keys) => (r) => {
|
|
5
5
|
const [originalKey, ...redirectKeys] = keys;
|
|
6
6
|
return r.fromKey(originalKey).redirectFrom(...redirectKeys);
|
|
@@ -60,8 +60,7 @@ export class Registration {
|
|
|
60
60
|
throw new DependencyMissingKeyError('No key provided for registration');
|
|
61
61
|
}
|
|
62
62
|
const key = this.key;
|
|
63
|
-
|
|
64
|
-
container.register(key, provider);
|
|
63
|
+
container.register(key, this.createProvider(key).pipe(...this.mappers));
|
|
65
64
|
for (const redirectKey of this.redirectKeys) {
|
|
66
65
|
container.register(redirectKey, new Provider((s) => s.resolve(key)));
|
|
67
66
|
}
|
package/package.json
CHANGED
package/typings/by.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { CreateScopeOptions, DependencyKey, IContainer, InjectionToken, Instance } from './container/IContainer';
|
|
2
|
-
import {
|
|
2
|
+
import { ProviderResolveOptions } from './provider/IProvider';
|
|
3
3
|
import { InjectFn } from './hooks/HookContext';
|
|
4
|
-
import { IRegistration, ScopePredicate } from './registration/IRegistration';
|
|
5
|
-
import { MapFn } from './utils';
|
|
6
4
|
export type InstancePredicate = (dep: unknown) => boolean;
|
|
7
5
|
export declare const all: InstancePredicate;
|
|
8
6
|
export type IMemo = Map<string, DependencyKey[]>;
|
|
@@ -15,7 +13,7 @@ type AliasPredicate = (aliases: Set<string>, container: IContainer) => boolean;
|
|
|
15
13
|
export declare const byAliases: (predicate: AliasPredicate, { memoize, lazy }?: AliasOptions) => InjectFn<unknown[]>;
|
|
16
14
|
export declare const byAlias: (predicate: AliasPredicate, { memoize, lazy }?: AliasOptions) => InjectFn;
|
|
17
15
|
export declare const by: {
|
|
18
|
-
keys: (keys: InjectionToken[], { lazy }?: Pick<ProviderResolveOptions, "lazy">) => (
|
|
16
|
+
keys: (keys: InjectionToken[], { lazy }?: Pick<ProviderResolveOptions, "lazy">) => (c: IContainer, ...args: unknown[]) => unknown[];
|
|
19
17
|
key: <T>(key: InjectionToken<T>, { args: deps, lazy }?: {
|
|
20
18
|
args?: unknown[];
|
|
21
19
|
lazy?: boolean;
|
|
@@ -26,16 +24,4 @@ export declare const by: {
|
|
|
26
24
|
create: (options: CreateScopeOptions) => (l: IContainer) => IContainer;
|
|
27
25
|
};
|
|
28
26
|
};
|
|
29
|
-
export type DepKey<T> = {
|
|
30
|
-
key: DependencyKey;
|
|
31
|
-
assignTo: (registration: IRegistration<T>) => IRegistration<T>;
|
|
32
|
-
register: (fn: (s: IContainer, ...args: unknown[]) => T) => IRegistration<T>;
|
|
33
|
-
resolve: (s: IContainer, ...args: unknown[]) => T;
|
|
34
|
-
pipe(...values: MapFn<IProvider<T>>[]): DepKey<T>;
|
|
35
|
-
to(target: DependencyKey): DepKey<T>;
|
|
36
|
-
when(value: ScopePredicate): DepKey<T>;
|
|
37
|
-
redirectFrom: (registration: IRegistration<T>) => IRegistration<T>;
|
|
38
|
-
};
|
|
39
|
-
export declare const isDepKey: <T>(key: unknown) => key is DepKey<T>;
|
|
40
|
-
export declare const depKey: <T>(key: DependencyKey) => DepKey<T>;
|
|
41
27
|
export {};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { AliasPredicate, DependencyKey, IContainer, InjectionToken, Instance, ResolveOptions, Tag } from './IContainer';
|
|
2
2
|
import { IRegistration } from '../registration/IRegistration';
|
|
3
3
|
export declare abstract class AutoMockedContainer implements IContainer {
|
|
4
|
-
detach(): void;
|
|
5
4
|
isDisposed: boolean;
|
|
6
5
|
hasProvider(key: string): boolean;
|
|
7
6
|
createScope(): IContainer;
|
|
@@ -11,20 +11,19 @@ export declare class Container implements IContainer {
|
|
|
11
11
|
private readonly tags;
|
|
12
12
|
private readonly providers;
|
|
13
13
|
private readonly registrations;
|
|
14
|
+
private readonly onConstruct;
|
|
14
15
|
private readonly onDispose;
|
|
15
16
|
constructor(injector: IInjector, options?: {
|
|
16
17
|
parent?: IContainer;
|
|
17
18
|
tags?: Tag[];
|
|
18
|
-
|
|
19
|
+
onConstruct?: (instance: Instance) => void;
|
|
20
|
+
onDispose?: (scope: IContainer) => void;
|
|
19
21
|
});
|
|
20
22
|
add(registration: IRegistration): this;
|
|
21
23
|
register(key: DependencyKey, provider: IProvider): this;
|
|
22
24
|
resolve<T>(token: InjectionToken<T>, { args, child, lazy }?: ResolveOptions): T;
|
|
23
25
|
createScope({ tags }?: CreateScopeOptions): IContainer;
|
|
24
|
-
dispose(
|
|
25
|
-
cascade?: boolean;
|
|
26
|
-
}): void;
|
|
27
|
-
detach(): void;
|
|
26
|
+
dispose(): void;
|
|
28
27
|
use(module: IContainerModule): this;
|
|
29
28
|
hasProvider(key: DependencyKey): boolean;
|
|
30
29
|
resolveManyByAlias(predicate: AliasPredicate, { args, child, lazy }?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
|
|
@@ -33,6 +32,10 @@ export declare class Container implements IContainer {
|
|
|
33
32
|
getScopes(): IContainer[];
|
|
34
33
|
getInstances(): Instance[];
|
|
35
34
|
hasTag(tag: Tag): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
applyRegistrationsFrom(source: Container): void;
|
|
36
39
|
/**
|
|
37
40
|
* @private
|
|
38
41
|
*/
|
|
@@ -2,7 +2,6 @@ import { AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionT
|
|
|
2
2
|
import { IProvider } from '../provider/IProvider';
|
|
3
3
|
import { IRegistration } from '../registration/IRegistration';
|
|
4
4
|
export declare class EmptyContainer implements IContainer {
|
|
5
|
-
detach(): void;
|
|
6
5
|
get isDisposed(): boolean;
|
|
7
6
|
hasProvider(key: string): boolean;
|
|
8
7
|
getParent(): undefined;
|
|
@@ -35,22 +35,13 @@ export interface IContainer extends Resolvable, Tagged {
|
|
|
35
35
|
register(key: DependencyKey, value: IProvider): this;
|
|
36
36
|
add(registration: IRegistration): this;
|
|
37
37
|
removeScope(child: IContainer): void;
|
|
38
|
-
|
|
39
|
-
* @param options - cascade must be true by default
|
|
40
|
-
*/
|
|
41
|
-
dispose(options?: {
|
|
42
|
-
cascade?: boolean;
|
|
43
|
-
}): void;
|
|
38
|
+
dispose(): void;
|
|
44
39
|
use(module: IContainerModule): this;
|
|
45
40
|
getRegistrations(): IRegistration[];
|
|
46
41
|
hasProvider(key: DependencyKey): boolean;
|
|
47
42
|
getParent(): IContainer | undefined;
|
|
48
43
|
getScopes(): IContainer[];
|
|
49
44
|
getInstances(): Instance[];
|
|
50
|
-
/**
|
|
51
|
-
* Detaches container from parent
|
|
52
|
-
*/
|
|
53
|
-
detach(): void;
|
|
54
45
|
resolveManyByAlias(predicate: AliasPredicate, options?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
|
|
55
46
|
resolveOneByAlias<T>(predicate: AliasPredicate, options?: ResolveOptions): [DependencyKey, T];
|
|
56
47
|
}
|
package/typings/index.d.ts
CHANGED
|
@@ -9,9 +9,8 @@ export { SimpleInjector } from './injector/SimpleInjector';
|
|
|
9
9
|
export { ProxyInjector } from './injector/ProxyInjector';
|
|
10
10
|
export { ResolveDependency, IProvider, provider, visible, alias, argsFn, args, ArgsFn, ProviderDecorator, ProviderResolveOptions, } from './provider/IProvider';
|
|
11
11
|
export { Provider } from './provider/Provider';
|
|
12
|
-
export { singleton, SingletonProvider } from './provider/
|
|
13
|
-
export {
|
|
14
|
-
export { Cache } from './provider/singleton/Cache';
|
|
12
|
+
export { singleton, SingletonProvider } from './provider/SingletonProvider';
|
|
13
|
+
export { Cache } from './provider/Cache';
|
|
15
14
|
export { decorate, DecorateFn } from './provider/DecoratorProvider';
|
|
16
15
|
export { key, IRegistration, ReturnTypeOfRegistration, scope, register, redirectFrom, ScopePredicate, } from './registration/IRegistration';
|
|
17
16
|
export { Registration } from './registration/Registration';
|
|
@@ -21,6 +20,10 @@ export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
|
21
20
|
export { UnexpectedHookResultError } from './errors/UnexpectedHookResultError';
|
|
22
21
|
export { getHooks, hook, hasHooks, HookFn, HookClass, runHooks, runHooksAsync, injectProp, onDispose, onConstruct, runOnConstructHooks, runOnDisposeHooks, } from './hooks/hook';
|
|
23
22
|
export { HookContext, InjectFn, IHookContext } from './hooks/HookContext';
|
|
24
|
-
export { by, InstancePredicate, IMemo, IMemoKey, byAlias, byAliases
|
|
23
|
+
export { by, InstancePredicate, IMemo, IMemoKey, byAlias, byAliases } from './by';
|
|
25
24
|
export { constructor, Branded } from './utils';
|
|
26
25
|
export { setMetadata, getMetadata, setParameterMetadata, setMethodMetadata, getMethodMetadata, getParameterMetadata, } from './metadata';
|
|
26
|
+
export { depKey } from './isDepKey';
|
|
27
|
+
export { DepKey } from './isDepKey';
|
|
28
|
+
export { multiCache } from './provider/Cache';
|
|
29
|
+
export { MultiCache } from './provider/Cache';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { constructor } from '../utils';
|
|
2
2
|
import { IContainer } from '../container/IContainer';
|
|
3
3
|
import { InjectFn } from '../hooks/HookContext';
|
|
4
|
-
import { DepKey } from '../
|
|
4
|
+
import { DepKey } from '../isDepKey';
|
|
5
5
|
export declare const inject: <T>(fn: InjectFn | DepKey<T>) => ParameterDecorator;
|
|
6
6
|
export declare const resolveArgs: (Target: constructor<unknown>, methodName?: string) => (scope: IContainer, ...deps: unknown[]) => unknown[];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DependencyKey, IContainer } from './container/IContainer';
|
|
2
|
+
import { MapFn } from './utils';
|
|
3
|
+
import { IRegistration, ScopePredicate } from './registration/IRegistration';
|
|
4
|
+
import { IProvider } from './provider/IProvider';
|
|
5
|
+
export type DepKey<T> = {
|
|
6
|
+
key: DependencyKey;
|
|
7
|
+
assignTo: (registration: IRegistration<T>) => IRegistration<T>;
|
|
8
|
+
register: (fn: (s: IContainer, ...args: unknown[]) => T) => IRegistration<T>;
|
|
9
|
+
resolve: (s: IContainer, ...args: unknown[]) => T;
|
|
10
|
+
pipe(...values: MapFn<IProvider<T>>[]): DepKey<T>;
|
|
11
|
+
to(target: DependencyKey): DepKey<T>;
|
|
12
|
+
when(value: ScopePredicate): DepKey<T>;
|
|
13
|
+
redirectFrom: (registration: IRegistration<T>) => IRegistration<T>;
|
|
14
|
+
};
|
|
15
|
+
export declare const isDepKey: <T>(key: unknown) => key is DepKey<T>;
|
|
16
|
+
export declare const depKey: <T>(key: DependencyKey) => DepKey<T>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface Cache<K, V> {
|
|
2
|
+
getKey(...args: unknown[]): K;
|
|
3
|
+
hasValue(key: K): boolean;
|
|
4
|
+
getValue(key: K): V;
|
|
5
|
+
setValue(key: K, value: V): void;
|
|
6
|
+
}
|
|
7
|
+
export declare class MultiCache<K, V> implements Cache<K, V> {
|
|
8
|
+
readonly getKey: (...args: unknown[]) => K;
|
|
9
|
+
private instances;
|
|
10
|
+
constructor(getKey?: (...args: unknown[]) => K);
|
|
11
|
+
hasValue(token: K): boolean;
|
|
12
|
+
getValue(token: K): V;
|
|
13
|
+
setValue(token: K, value: V): void;
|
|
14
|
+
}
|
|
15
|
+
export declare const multiCache: <K, V>(getKey: (...args: unknown[]) => K) => MultiCache<K, V>;
|
|
16
|
+
export declare class SingleCache<V> implements Cache<string, V> {
|
|
17
|
+
private instance?;
|
|
18
|
+
getKey(...args: unknown[]): string;
|
|
19
|
+
getValue(key: string): V;
|
|
20
|
+
hasValue(key: string): boolean;
|
|
21
|
+
setValue(key: string, value: V): void;
|
|
22
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Alias, AliasPredicate, IContainer, Tagged } from '../container/IContainer';
|
|
1
|
+
import { Alias, AliasPredicate, DependencyKey, IContainer, Tagged } from '../container/IContainer';
|
|
2
2
|
import { constructor, MapFn } from '../utils';
|
|
3
3
|
export type ProviderResolveOptions = {
|
|
4
4
|
args: unknown[];
|
|
@@ -13,6 +13,7 @@ export type ArgsFn = (l: IContainer, ...args: unknown[]) => unknown[];
|
|
|
13
13
|
export declare function args<T = unknown>(...extraArgs: unknown[]): MapFn<IProvider<T>>;
|
|
14
14
|
export declare function argsFn<T = unknown>(fn: ArgsFn): MapFn<IProvider<T>>;
|
|
15
15
|
export interface IProvider<T = any> {
|
|
16
|
+
key?: DependencyKey;
|
|
16
17
|
resolve(container: IContainer, options: ProviderResolveOptions): T;
|
|
17
18
|
isVisible(parent: Tagged, child: Tagged): boolean;
|
|
18
19
|
pipe(...mappers: MapFn<IProvider<T>>[]): IProvider<T>;
|
|
@@ -28,6 +29,8 @@ export declare const visible: (isVisibleWhen: ChildrenVisibilityPredicate) => Ma
|
|
|
28
29
|
export declare abstract class ProviderDecorator<T> implements IProvider<T> {
|
|
29
30
|
private decorated;
|
|
30
31
|
protected constructor(decorated: IProvider<T>);
|
|
32
|
+
get key(): DependencyKey | undefined;
|
|
33
|
+
set key(value: DependencyKey | undefined);
|
|
31
34
|
setVisibility(predicate: ChildrenVisibilityPredicate): this;
|
|
32
35
|
isVisible(parent: IContainer, child: Tagged): boolean;
|
|
33
36
|
resolve(container: IContainer, options: ProviderResolveOptions): T;
|
|
@@ -6,6 +6,7 @@ export declare class Provider<T = any> implements IProvider<T> {
|
|
|
6
6
|
static fromClass<T>(Target: constructor<T>): IProvider<T>;
|
|
7
7
|
static fromValue<T>(value: T): IProvider<T>;
|
|
8
8
|
static fromKey<T>(key: DependencyKey): Provider<T>;
|
|
9
|
+
key?: DependencyKey;
|
|
9
10
|
private readonly aliases;
|
|
10
11
|
private argsFn;
|
|
11
12
|
private isVisibleWhen;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IContainer } from '
|
|
2
|
-
import { IProvider, ProviderDecorator, ProviderResolveOptions } from '
|
|
3
|
-
import { MapFn } from '
|
|
1
|
+
import { IContainer } from '../container/IContainer';
|
|
2
|
+
import { IProvider, ProviderDecorator, ProviderResolveOptions } from './IProvider';
|
|
3
|
+
import { MapFn } from '../utils';
|
|
4
4
|
import { Cache } from './Cache';
|
|
5
5
|
export declare function singleton<T = unknown>(cacheProvider?: () => Cache<unknown, T>): MapFn<IProvider<T>>;
|
|
6
6
|
export declare class SingletonProvider<T> extends ProviderDecorator<T> {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DependencyKey, IContainer, IContainerModule } from '../container/IContainer';
|
|
2
2
|
import { constructor, MapFn } from '../utils';
|
|
3
3
|
import { IProvider } from '../provider/IProvider';
|
|
4
|
-
import { DepKey } from '../
|
|
4
|
+
import { DepKey } from '../isDepKey';
|
|
5
5
|
export type ScopePredicate = (s: IContainer, prev?: boolean) => boolean;
|
|
6
6
|
export interface IRegistration<T = any> extends IContainerModule {
|
|
7
7
|
when(...predicates: ScopePredicate[]): this;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SingleCache = void 0;
|
|
4
|
-
class SingleCache {
|
|
5
|
-
getKey(...args) {
|
|
6
|
-
return '1';
|
|
7
|
-
}
|
|
8
|
-
getValue(key) {
|
|
9
|
-
return this.instance.value;
|
|
10
|
-
}
|
|
11
|
-
hasValue(key) {
|
|
12
|
-
return this.instance !== undefined;
|
|
13
|
-
}
|
|
14
|
-
setValue(key, value) {
|
|
15
|
-
this.instance = { value };
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.SingleCache = SingleCache;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Cache } from './Cache';
|
|
2
|
-
export declare class MultiCache<K, V> implements Cache<K, V> {
|
|
3
|
-
readonly getKey: (...args: unknown[]) => K;
|
|
4
|
-
private instances;
|
|
5
|
-
constructor(getKey?: (...args: unknown[]) => K);
|
|
6
|
-
hasValue(token: K): boolean;
|
|
7
|
-
getValue(token: K): V;
|
|
8
|
-
setValue(token: K, value: V): void;
|
|
9
|
-
}
|
|
10
|
-
export declare const multiCache: <K, V>(getKey: (...args: unknown[]) => K) => MultiCache<K, V>;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Cache } from './Cache';
|
|
2
|
-
export declare class SingleCache<V> implements Cache<string, V> {
|
|
3
|
-
private instance?;
|
|
4
|
-
getKey(...args: unknown[]): string;
|
|
5
|
-
getValue(key: string): V;
|
|
6
|
-
hasValue(key: string): boolean;
|
|
7
|
-
setValue(key: string, value: V): void;
|
|
8
|
-
}
|