react-obsidian 0.0.14 → 0.0.15
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/README.md +23 -3
- package/babel.config.js +1 -1
- package/dist/src/Obsidian.d.ts +1 -0
- package/dist/src/Obsidian.d.ts.map +1 -1
- package/dist/src/Obsidian.js +4 -0
- package/dist/src/Obsidian.js.map +1 -1
- package/dist/src/decorators/inject/Inject.d.ts +1 -1
- package/dist/src/decorators/inject/Inject.d.ts.map +1 -1
- package/dist/src/decorators/inject/Inject.js +3 -3
- package/dist/src/decorators/inject/Inject.js.map +1 -1
- package/dist/src/decorators/inject/LazyInject.d.ts +2 -0
- package/dist/src/decorators/inject/LazyInject.d.ts.map +1 -0
- package/dist/src/decorators/inject/LazyInject.js +15 -0
- package/dist/src/decorators/inject/LazyInject.js.map +1 -0
- package/dist/src/graph/ProviderBinder.d.ts.map +1 -1
- package/dist/src/graph/ProviderBinder.js +2 -3
- package/dist/src/graph/ProviderBinder.js.map +1 -1
- package/dist/src/graph/registry/GraphRegistry.d.ts +2 -0
- package/dist/src/graph/registry/GraphRegistry.d.ts.map +1 -1
- package/dist/src/graph/registry/GraphRegistry.js +6 -0
- package/dist/src/graph/registry/GraphRegistry.js.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/injectors/class/ClassInjector.d.ts.map +1 -1
- package/dist/src/injectors/class/ClassInjector.js +2 -0
- package/dist/src/injectors/class/ClassInjector.js.map +1 -1
- package/dist/src/injectors/class/InjectionMetadata.d.ts +5 -0
- package/dist/src/injectors/class/InjectionMetadata.d.ts.map +1 -1
- package/dist/src/injectors/class/InjectionMetadata.js +16 -3
- package/dist/src/injectors/class/InjectionMetadata.js.map +1 -1
- package/dist/src/injectors/class/LazyInjector.d.ts +8 -0
- package/dist/src/injectors/class/LazyInjector.d.ts.map +1 -0
- package/dist/src/injectors/class/LazyInjector.js +28 -0
- package/dist/src/injectors/class/LazyInjector.js.map +1 -0
- package/dist/transformers/babel-plugin-obsidian/helpers/index.d.ts +3 -2
- package/dist/transformers/babel-plugin-obsidian/helpers/index.d.ts.map +1 -1
- package/dist/transformers/babel-plugin-obsidian/helpers/index.js +8 -2
- package/dist/transformers/babel-plugin-obsidian/helpers/index.js.map +1 -1
- package/dist/transformers/babel-plugin-obsidian/index.d.ts.map +1 -1
- package/dist/transformers/babel-plugin-obsidian/index.js +14 -34
- package/dist/transformers/babel-plugin-obsidian/index.js.map +1 -1
- package/dist/transformers/babel-plugin-obsidian/unmagler/index.d.ts +10 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/index.d.ts.map +1 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/index.js +14 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/index.js.map +1 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/method.d.ts +4 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/method.d.ts.map +1 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/method.js +26 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/method.js.map +1 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/property.d.ts +4 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/property.d.ts.map +1 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/property.js +14 -0
- package/dist/transformers/babel-plugin-obsidian/unmagler/property.js.map +1 -0
- package/example/babel.config.js +1 -1
- package/package.json +1 -1
- package/src/Obsidian.ts +5 -0
- package/src/decorators/inject/Inject.ts +3 -3
- package/src/decorators/inject/LazyInject.ts +8 -0
- package/src/graph/ProviderBinder.ts +2 -3
- package/src/graph/registry/GraphRegistry.ts +7 -0
- package/src/index.ts +1 -0
- package/src/injectors/class/ClassInjector.ts +2 -2
- package/src/injectors/class/InjectionMetadata.ts +27 -2
- package/src/injectors/class/LazyInjector.ts +22 -0
- package/transformers/babel-plugin-obsidian/helpers/index.ts +15 -3
- package/transformers/babel-plugin-obsidian/index.ts +11 -46
- package/transformers/babel-plugin-obsidian/unmagler/index.ts +9 -0
- package/transformers/babel-plugin-obsidian/unmagler/method.ts +31 -0
- package/transformers/babel-plugin-obsidian/unmagler/property.ts +21 -0
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var
|
|
6
|
+
var unmagler_1 = __importDefault(require("./unmagler"));
|
|
4
7
|
var providerArgumentsTransformer = {
|
|
5
8
|
visitor: {
|
|
6
9
|
Program: function (path) {
|
|
@@ -12,52 +15,29 @@ var internalVisitor = {
|
|
|
12
15
|
ClassMethod: {
|
|
13
16
|
enter: function (_a) {
|
|
14
17
|
var node = _a.node;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
unmagler_1.default.saveClassMethod('Provides', node);
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
ClassProperty: {
|
|
22
|
+
enter: function (_a) {
|
|
23
|
+
var node = _a.node;
|
|
24
|
+
unmagler_1.default.saveClassProperty('Inject', node);
|
|
25
|
+
unmagler_1.default.saveClassProperty('LazyInject', node);
|
|
20
26
|
},
|
|
21
27
|
},
|
|
22
28
|
Identifier: {
|
|
23
29
|
enter: function (_a) {
|
|
24
30
|
var node = _a.node;
|
|
25
|
-
|
|
26
|
-
if ((0, helpers_1.getDecoratorName)(decorator) === 'Inject') {
|
|
27
|
-
saveUnmangledParamName(node, decorator);
|
|
28
|
-
}
|
|
31
|
+
unmagler_1.default.saveIdentifier('Inject', node);
|
|
29
32
|
},
|
|
30
33
|
},
|
|
31
34
|
TSParameterProperty: {
|
|
32
35
|
enter: function (_a) {
|
|
33
36
|
var node = _a.node;
|
|
34
|
-
|
|
35
|
-
if ((0, helpers_1.getDecoratorName)(decorator) === 'Inject') {
|
|
36
|
-
saveUnmangledParamName(node, decorator);
|
|
37
|
-
}
|
|
37
|
+
unmagler_1.default.saveTSParameterProperty('Inject', node);
|
|
38
38
|
},
|
|
39
39
|
},
|
|
40
40
|
};
|
|
41
|
-
function saveUnmangledParamName(node, decorator) {
|
|
42
|
-
if (injectIsNotNamed(decorator)) {
|
|
43
|
-
(0, helpers_1.passParamNameAsInjectArgument)(node, decorator);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function injectIsNotNamed(decorator) {
|
|
47
|
-
return (0, helpers_1.getDecoratorArgument)(decorator) === undefined;
|
|
48
|
-
}
|
|
49
|
-
function convertProviderParamsToDestructuringAssignment(node) {
|
|
50
|
-
if (node.params.length === 0)
|
|
51
|
-
return;
|
|
52
|
-
var destructuredParams = (0, helpers_1.paramsToDestructuringAssignment)(node.params);
|
|
53
|
-
node.params.length = 0;
|
|
54
|
-
node.params.push(destructuredParams);
|
|
55
|
-
}
|
|
56
|
-
function saveUnmangledMethodNameInProviderArguments(node, decorator) {
|
|
57
|
-
if ((0, helpers_1.providerIsNotNamed)(decorator)) {
|
|
58
|
-
(0, helpers_1.addNameToProviderArguments)(node, decorator);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
41
|
function plugin() {
|
|
62
42
|
return providerArgumentsTransformer;
|
|
63
43
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../transformers/babel-plugin-obsidian/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../transformers/babel-plugin-obsidian/index.ts"],"names":[],"mappings":";;;;;AASA,wDAAkC;AAElC,IAAM,4BAA4B,GAAc;IAC9C,OAAO,EAAE;QACP,OAAO,EAAP,UAAQ,IAAuB;YAC7B,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC;QACjC,CAAC;KACF;CACF,CAAC;AAEF,IAAM,eAAe,GAAG;IACtB,WAAW,EAAE;QACX,KAAK,EAAL,UAAM,EAA+B;gBAA7B,IAAI,UAAA;YACV,kBAAQ,CAAC,eAAe,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;QAC7C,CAAC;KACF;IACD,aAAa,EAAE;QACb,KAAK,EAAL,UAAM,EAAiC;gBAA/B,IAAI,UAAA;YACV,kBAAQ,CAAC,iBAAiB,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;YAC3C,kBAAQ,CAAC,iBAAiB,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;QACjD,CAAC;KACF;IACD,UAAU,EAAE;QACV,KAAK,EAAL,UAAM,EAA8B;gBAA5B,IAAI,UAAA;YACV,kBAAQ,CAAC,cAAc,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC1C,CAAC;KACF;IACD,mBAAmB,EAAE;QACnB,KAAK,EAAL,UAAM,EAAuC;gBAArC,IAAI,UAAA;YACV,kBAAQ,CAAC,uBAAuB,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QACnD,CAAC;KACF;CACF,CAAC;AAEF,SAAwB,MAAM;IAC5B,OAAO,4BAA4B,CAAC;AACtC,CAAC;AAFD,yBAEC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import saveMethod from './method';
|
|
2
|
+
import saveProperty from './property';
|
|
3
|
+
declare const _default: {
|
|
4
|
+
saveClassMethod: typeof saveMethod;
|
|
5
|
+
saveClassProperty: typeof saveProperty;
|
|
6
|
+
saveIdentifier: typeof saveProperty;
|
|
7
|
+
saveTSParameterProperty: typeof saveProperty;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../transformers/babel-plugin-obsidian/unmagler/index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,UAAU,CAAC;AAClC,OAAO,YAAY,MAAM,YAAY,CAAC;;;;;;;AAEtC,wBAKE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var method_1 = __importDefault(require("./method"));
|
|
7
|
+
var property_1 = __importDefault(require("./property"));
|
|
8
|
+
exports.default = {
|
|
9
|
+
saveClassMethod: method_1.default,
|
|
10
|
+
saveClassProperty: property_1.default,
|
|
11
|
+
saveIdentifier: property_1.default,
|
|
12
|
+
saveTSParameterProperty: property_1.default,
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../transformers/babel-plugin-obsidian/unmagler/index.ts"],"names":[],"mappings":";;;;;AAAA,oDAAkC;AAClC,wDAAsC;AAEtC,kBAAe;IACb,eAAe,EAAE,gBAAU;IAC3B,iBAAiB,EAAE,kBAAY;IAC/B,cAAc,EAAE,kBAAY;IAC5B,uBAAuB,EAAE,kBAAY;CACtC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"method.d.ts","sourceRoot":"","sources":["../../../../transformers/babel-plugin-obsidian/unmagler/method.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAa,MAAM,cAAc,CAAC;AAStD,iBAAS,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,QAMlD;AAeD,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var helpers_1 = require("../helpers");
|
|
4
|
+
function saveMethod(name, node) {
|
|
5
|
+
var decorator = (0, helpers_1.getDecoratorByName)(node.decorators, name);
|
|
6
|
+
if ((0, helpers_1.getDecoratorName)(decorator) === name) {
|
|
7
|
+
convertProviderParamsToDestructuringAssignment(node);
|
|
8
|
+
saveUnmangledMethodNameInProviderArguments(node, decorator);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function convertProviderParamsToDestructuringAssignment(node) {
|
|
12
|
+
if (node.params.length === 0) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
var destructuredParams = (0, helpers_1.paramsToDestructuringAssignment)(node.params);
|
|
16
|
+
// eslint-disable-next-line no-param-reassign
|
|
17
|
+
node.params.length = 0;
|
|
18
|
+
node.params.push(destructuredParams);
|
|
19
|
+
}
|
|
20
|
+
function saveUnmangledMethodNameInProviderArguments(node, decorator) {
|
|
21
|
+
if ((0, helpers_1.providerIsNotNamed)(decorator)) {
|
|
22
|
+
(0, helpers_1.addNameToProviderArguments)(node, decorator);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.default = saveMethod;
|
|
26
|
+
//# sourceMappingURL=method.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"method.js","sourceRoot":"","sources":["../../../../transformers/babel-plugin-obsidian/unmagler/method.ts"],"names":[],"mappings":";;AACA,sCAMoB;AAEpB,SAAS,UAAU,CAAC,IAAY,EAAE,IAAiB;IACjD,IAAM,SAAS,GAAG,IAAA,4BAAkB,EAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IAC5D,IAAI,IAAA,0BAAgB,EAAC,SAAS,CAAC,KAAK,IAAI,EAAE;QACxC,8CAA8C,CAAC,IAAI,CAAC,CAAC;QACrD,0CAA0C,CAAC,IAAI,EAAE,SAAU,CAAC,CAAC;KAC9D;AACH,CAAC;AAED,SAAS,8CAA8C,CAAC,IAAiB;IACvE,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;QAAE,OAAO;KAAE;IACzC,IAAM,kBAAkB,GAAG,IAAA,yCAA+B,EAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACxE,6CAA6C;IAC7C,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;IACvB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;AACvC,CAAC;AAED,SAAS,0CAA0C,CAAC,IAAiB,EAAE,SAAoB;IACzF,IAAI,IAAA,4BAAkB,EAAC,SAAS,CAAC,EAAE;QACjC,IAAA,oCAA0B,EAAC,IAAI,EAAE,SAAS,CAAC,CAAC;KAC7C;AACH,CAAC;AACD,kBAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"property.d.ts","sourceRoot":"","sources":["../../../../transformers/babel-plugin-obsidian/unmagler/property.ts"],"names":[],"mappings":"AACA,OAAO,EAKL,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAEpB,iBAAS,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,QAK7D;AAMD,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var helpers_1 = require("../helpers");
|
|
4
|
+
function savePropertyName(name, node) {
|
|
5
|
+
var decorator = (0, helpers_1.getDecoratorByName)(node.decorators, name);
|
|
6
|
+
if ((0, helpers_1.getDecoratorName)(decorator) === name && injectIsNotNamed(decorator)) {
|
|
7
|
+
(0, helpers_1.passParamNameAsInjectArgument)(node, decorator);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function injectIsNotNamed(decorator) {
|
|
11
|
+
return (0, helpers_1.getDecoratorArgument)(decorator) === undefined;
|
|
12
|
+
}
|
|
13
|
+
exports.default = savePropertyName;
|
|
14
|
+
//# sourceMappingURL=property.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"property.js","sourceRoot":"","sources":["../../../../transformers/babel-plugin-obsidian/unmagler/property.ts"],"names":[],"mappings":";;AACA,sCAMoB;AAEpB,SAAS,gBAAgB,CAAC,IAAY,EAAE,IAAsB;IAC5D,IAAM,SAAS,GAAG,IAAA,4BAAkB,EAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IAC5D,IAAI,IAAA,0BAAgB,EAAC,SAAS,CAAC,KAAK,IAAI,IAAI,gBAAgB,CAAC,SAAU,CAAC,EAAE;QACxE,IAAA,uCAA6B,EAAC,IAAI,EAAE,SAAU,CAAC,CAAC;KACjD;AACH,CAAC;AAED,SAAS,gBAAgB,CAAC,SAAoB;IAC5C,OAAO,IAAA,8BAAoB,EAAC,SAAS,CAAC,KAAK,SAAS,CAAC;AACvD,CAAC;AAED,kBAAe,gBAAgB,CAAC"}
|
package/example/babel.config.js
CHANGED
|
@@ -2,7 +2,7 @@ module.exports = {
|
|
|
2
2
|
presets: ['module:metro-react-native-babel-preset'],
|
|
3
3
|
plugins: [
|
|
4
4
|
['@babel/plugin-proposal-decorators', {legacy: true}],
|
|
5
|
-
['@babel/plugin-proposal-class-properties', {
|
|
5
|
+
['@babel/plugin-proposal-class-properties', {loose: true}],
|
|
6
6
|
[`${__dirname}/../dist/transformers/babel-plugin-obsidian`],
|
|
7
7
|
],
|
|
8
8
|
};
|
package/package.json
CHANGED
package/src/Obsidian.ts
CHANGED
|
@@ -2,6 +2,7 @@ import graphRegistry from './graph/registry/GraphRegistry';
|
|
|
2
2
|
import { ObjectGraph } from './graph/ObjectGraph';
|
|
3
3
|
import { Constructable, GraphInternals, ServiceLocator } from './types';
|
|
4
4
|
import { GraphMiddleware } from './graph/registry/GraphMiddleware';
|
|
5
|
+
import lazyInjector from './injectors/class/LazyInjector';
|
|
5
6
|
|
|
6
7
|
export default class Obsidian {
|
|
7
8
|
obtain<T extends ObjectGraph<P>, P = any>(
|
|
@@ -11,6 +12,10 @@ export default class Obsidian {
|
|
|
11
12
|
return graphRegistry.resolve(Graph, props) as unknown as ServiceLocator<T>;
|
|
12
13
|
}
|
|
13
14
|
|
|
15
|
+
inject<T extends object>(target: T) {
|
|
16
|
+
return lazyInjector.inject(target);
|
|
17
|
+
}
|
|
18
|
+
|
|
14
19
|
addGraphMiddleware(middleware: GraphMiddleware) {
|
|
15
20
|
graphRegistry.addGraphMiddleware(middleware);
|
|
16
21
|
}
|
|
@@ -4,14 +4,14 @@ import InjectionMetadata from '../../injectors/class/InjectionMetadata';
|
|
|
4
4
|
export function Inject(name?: string) {
|
|
5
5
|
return (
|
|
6
6
|
target: Object | any,
|
|
7
|
-
|
|
7
|
+
_propertyKey?: string,
|
|
8
8
|
indexOrPropertyDescriptor?: number | PropertyDescriptor,
|
|
9
9
|
) => {
|
|
10
10
|
const metadata = new InjectionMetadata();
|
|
11
|
-
if (
|
|
11
|
+
if (isNumber(indexOrPropertyDescriptor)) {
|
|
12
12
|
metadata.saveConstructorParamMetadata(target, name!, indexOrPropertyDescriptor);
|
|
13
13
|
} else {
|
|
14
|
-
metadata.savePropertyMetadata(target.constructor,
|
|
14
|
+
metadata.savePropertyMetadata(target.constructor, name!);
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import InjectionMetadata from '../../injectors/class/InjectionMetadata';
|
|
2
|
+
|
|
3
|
+
export function LazyInject(name?: string): any {
|
|
4
|
+
return (target: object) => {
|
|
5
|
+
const metadata = new InjectionMetadata();
|
|
6
|
+
metadata.saveLazyPropertyMetadata(target.constructor, name!);
|
|
7
|
+
};
|
|
8
|
+
}
|
|
@@ -4,9 +4,8 @@ import { Graph } from './Graph';
|
|
|
4
4
|
|
|
5
5
|
export function bindProviders(graph: Graph & Record<string, any>) {
|
|
6
6
|
providedPropertiesStore.getMangledProperties(graph)
|
|
7
|
+
.filter((method) => graph[method])
|
|
7
8
|
.forEach((method) => {
|
|
8
|
-
|
|
9
|
-
graph[method] = graph[method].bind(graph);
|
|
10
|
-
}
|
|
9
|
+
graph[method] = graph[method].bind(graph);
|
|
11
10
|
});
|
|
12
11
|
}
|
|
@@ -6,6 +6,7 @@ import GraphMiddlewareChain from './GraphMiddlewareChain';
|
|
|
6
6
|
export class GraphRegistry {
|
|
7
7
|
private readonly constructorToInstance = new Map<Constructable<Graph>, Set<Graph>>();
|
|
8
8
|
private readonly instanceToConstructor = new Map<Graph, Constructable<Graph>>();
|
|
9
|
+
private readonly nameToInstance = new Map<string, Graph>();
|
|
9
10
|
private readonly graphToSubgraphs = new Map<Constructable<Graph>, Set<Constructable<Graph>>>();
|
|
10
11
|
private readonly graphMiddlewares = new GraphMiddlewareChain();
|
|
11
12
|
|
|
@@ -19,6 +20,10 @@ export class GraphRegistry {
|
|
|
19
20
|
return Array.from(subgraphs).map((G) => this.resolve(G));
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
getGraphInstance(name: string): Graph {
|
|
24
|
+
return this.nameToInstance.get(name)!;
|
|
25
|
+
}
|
|
26
|
+
|
|
22
27
|
resolve<T extends Graph>(Graph: Constructable<T>, props?: any): T {
|
|
23
28
|
if (this.isSingleton(Graph) && this.has(Graph)) {
|
|
24
29
|
return this.getFirst(Graph);
|
|
@@ -41,6 +46,7 @@ export class GraphRegistry {
|
|
|
41
46
|
graphs.add(graph);
|
|
42
47
|
this.constructorToInstance.set(Graph, graphs);
|
|
43
48
|
this.instanceToConstructor.set(graph, Graph);
|
|
49
|
+
this.nameToInstance.set(graph.name, graph);
|
|
44
50
|
}
|
|
45
51
|
|
|
46
52
|
private isSingleton(Graph: Constructable<Graph>): boolean {
|
|
@@ -51,6 +57,7 @@ export class GraphRegistry {
|
|
|
51
57
|
const Graph = this.instanceToConstructor.get(graph)!;
|
|
52
58
|
this.instanceToConstructor.delete(graph);
|
|
53
59
|
this.constructorToInstance.get(Graph)!.delete(graph);
|
|
60
|
+
this.nameToInstance.delete(graph.name);
|
|
54
61
|
}
|
|
55
62
|
|
|
56
63
|
addGraphMiddleware(middleware: Middleware<Graph>) {
|
package/src/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ export { Graph as IGraph } from './graph/Graph';
|
|
|
14
14
|
export { Provides } from './decorators/provides/Provides';
|
|
15
15
|
export { Injectable } from './decorators/inject/Injectable';
|
|
16
16
|
export { Inject } from './decorators/inject/Inject';
|
|
17
|
+
export { LazyInject } from './decorators/inject/LazyInject';
|
|
17
18
|
export { GraphMiddleware } from './graph/registry/GraphMiddleware';
|
|
18
19
|
export { GraphResolveChain as ResolveChain } from './graph/registry/GraphResolveChain';
|
|
19
20
|
export const Obsidian = new _Obsidian();
|
|
@@ -2,6 +2,7 @@ import { Constructable } from '../../types';
|
|
|
2
2
|
import { GraphRegistry } from '../../graph/registry/GraphRegistry';
|
|
3
3
|
import { Graph } from '../../graph/Graph';
|
|
4
4
|
import InjectionMetadata from './InjectionMetadata';
|
|
5
|
+
import { GRAPH_INSTANCE_NAME_KEY } from './LazyInjector';
|
|
5
6
|
|
|
6
7
|
export default class ClassInjector {
|
|
7
8
|
constructor(
|
|
@@ -23,11 +24,10 @@ export default class ClassInjector {
|
|
|
23
24
|
return new class Handler implements ProxyHandler<any> {
|
|
24
25
|
construct(target: any, args: any[], newTarget: Function): any {
|
|
25
26
|
const graph = graphRegistry.resolve(Graph);
|
|
26
|
-
|
|
27
|
+
Reflect.defineMetadata(GRAPH_INSTANCE_NAME_KEY, graph.name, target);
|
|
27
28
|
const argsToInject = this.injectConstructorArgs(args, graph, target);
|
|
28
29
|
const createdObject = Reflect.construct(target, argsToInject, newTarget);
|
|
29
30
|
this.injectProperties(target, createdObject, graph);
|
|
30
|
-
|
|
31
31
|
return createdObject;
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -3,13 +3,18 @@ import { ConstructorArgs } from './ConstructorArgs';
|
|
|
3
3
|
export default class InjectionMetadata {
|
|
4
4
|
private readonly injectionMetadataKey = 'injectionMetadata';
|
|
5
5
|
private readonly injectedConstructorArgsKey = 'injectedConstructorArgsKey';
|
|
6
|
+
private readonly lazyInjectionMetadataKey = 'lazyInjectionMetadataKey';
|
|
6
7
|
|
|
7
8
|
getConstructorArgsToInject(target: any): ConstructorArgs {
|
|
8
9
|
return Reflect.getMetadata(this.injectedConstructorArgsKey, target) ?? new ConstructorArgs();
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
getPropertiesToInject(target: any): Set<string> {
|
|
12
|
-
return
|
|
13
|
+
return this.getProperties(this.injectionMetadataKey, target);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getLazyPropertiesToInject(target: any): Set<string> {
|
|
17
|
+
return this.getProperties(this.lazyInjectionMetadataKey, target);
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
saveConstructorParamMetadata(target: any, paramName: string, index: number) {
|
|
@@ -23,10 +28,30 @@ export default class InjectionMetadata {
|
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
savePropertyMetadata(target: any, property: string) {
|
|
26
|
-
|
|
31
|
+
this.saveProperties(
|
|
27
32
|
this.injectionMetadataKey,
|
|
28
33
|
this.getPropertiesToInject(target).add(property),
|
|
29
34
|
target,
|
|
30
35
|
);
|
|
31
36
|
}
|
|
37
|
+
|
|
38
|
+
saveLazyPropertyMetadata(target: any, property: string) {
|
|
39
|
+
this.saveProperties(
|
|
40
|
+
this.lazyInjectionMetadataKey,
|
|
41
|
+
this.getLazyPropertiesToInject(target).add(property),
|
|
42
|
+
target,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private saveProperties(key: string, properties: Set<string>, target: any) {
|
|
47
|
+
Reflect.defineMetadata(
|
|
48
|
+
key,
|
|
49
|
+
properties,
|
|
50
|
+
target,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private getProperties(key: string, target: any): Set<string> {
|
|
55
|
+
return Reflect.getMetadata(key, target) ?? new Set();
|
|
56
|
+
}
|
|
32
57
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import graphRegistry from '../../graph/registry/GraphRegistry';
|
|
2
|
+
import InjectionMetadata from './InjectionMetadata';
|
|
3
|
+
|
|
4
|
+
export const GRAPH_INSTANCE_NAME_KEY = 'GRAPH_INSTANCE_NAME';
|
|
5
|
+
|
|
6
|
+
class LazyInjector<T extends object> {
|
|
7
|
+
inject(target: T): T {
|
|
8
|
+
const injectionMetadata = new InjectionMetadata();
|
|
9
|
+
const graph = this.getGraphInstance(target);
|
|
10
|
+
injectionMetadata.getLazyPropertiesToInject(target.constructor).forEach((key) => {
|
|
11
|
+
Reflect.set(target, key, graph.retrieve(key));
|
|
12
|
+
});
|
|
13
|
+
return target;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private getGraphInstance(target: T) {
|
|
17
|
+
const graphInstanceName = Reflect.getMetadata(GRAPH_INSTANCE_NAME_KEY, target.constructor);
|
|
18
|
+
return graphRegistry.getGraphInstance(graphInstanceName);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default new LazyInjector();
|
|
@@ -3,6 +3,7 @@ import { types as t } from '@babel/core';
|
|
|
3
3
|
import {
|
|
4
4
|
CallExpression,
|
|
5
5
|
ClassMethod,
|
|
6
|
+
ClassProperty,
|
|
6
7
|
Decorator,
|
|
7
8
|
Identifier,
|
|
8
9
|
ObjectExpression,
|
|
@@ -13,6 +14,8 @@ import { get } from 'lodash';
|
|
|
13
14
|
|
|
14
15
|
const never = '';
|
|
15
16
|
|
|
17
|
+
export type AcceptedNodeType = Identifier | TSParameterProperty | ClassProperty;
|
|
18
|
+
|
|
16
19
|
export function providerIsNotNamed(decorator: Decorator): boolean {
|
|
17
20
|
const argument = getDecoratorArgument(decorator);
|
|
18
21
|
if (t.isObjectExpression(argument)) {
|
|
@@ -64,20 +67,29 @@ export function paramsToDestructuringAssignment(params: (Identifier | any)[]): O
|
|
|
64
67
|
.map((p) => t.objectProperty(t.identifier(p.name), t.identifier(p.name))));
|
|
65
68
|
}
|
|
66
69
|
|
|
67
|
-
export function passParamNameAsInjectArgument(
|
|
70
|
+
export function passParamNameAsInjectArgument(
|
|
71
|
+
node: AcceptedNodeType,
|
|
72
|
+
decorator: Decorator,
|
|
73
|
+
) {
|
|
68
74
|
if (t.isCallExpression(decorator.expression)) {
|
|
69
75
|
decorator.expression.arguments = [
|
|
70
|
-
t.stringLiteral(
|
|
76
|
+
t.stringLiteral(getNodeName(node)),
|
|
71
77
|
];
|
|
72
78
|
}
|
|
73
79
|
}
|
|
74
80
|
|
|
75
|
-
function
|
|
81
|
+
function getNodeName(node: AcceptedNodeType): string {
|
|
76
82
|
if (t.isTSParameterProperty(node)) {
|
|
77
83
|
if (t.isIdentifier(node.parameter)) {
|
|
78
84
|
return node.parameter.name;
|
|
79
85
|
}
|
|
80
86
|
return never;
|
|
81
87
|
}
|
|
88
|
+
if (t.isClassProperty(node)) {
|
|
89
|
+
if (t.isIdentifier(node.key)) {
|
|
90
|
+
return node.key.name;
|
|
91
|
+
}
|
|
92
|
+
return never;
|
|
93
|
+
}
|
|
82
94
|
return node.name;
|
|
83
95
|
}
|
|
@@ -1,21 +1,13 @@
|
|
|
1
1
|
/* eslint-disable no-param-reassign */
|
|
2
2
|
import {
|
|
3
3
|
ClassMethod,
|
|
4
|
+
ClassProperty,
|
|
4
5
|
Identifier,
|
|
5
|
-
Decorator,
|
|
6
6
|
Program,
|
|
7
7
|
TSParameterProperty,
|
|
8
8
|
} from '@babel/types';
|
|
9
9
|
import { NodePath, PluginObj } from '@babel/core';
|
|
10
|
-
import
|
|
11
|
-
addNameToProviderArguments,
|
|
12
|
-
getDecoratorName,
|
|
13
|
-
getDecoratorByName,
|
|
14
|
-
paramsToDestructuringAssignment,
|
|
15
|
-
providerIsNotNamed,
|
|
16
|
-
passParamNameAsInjectArgument,
|
|
17
|
-
getDecoratorArgument,
|
|
18
|
-
} from './helpers';
|
|
10
|
+
import unmagler from './unmagler';
|
|
19
11
|
|
|
20
12
|
const providerArgumentsTransformer: PluginObj = {
|
|
21
13
|
visitor: {
|
|
@@ -28,54 +20,27 @@ const providerArgumentsTransformer: PluginObj = {
|
|
|
28
20
|
const internalVisitor = {
|
|
29
21
|
ClassMethod: {
|
|
30
22
|
enter({ node }: NodePath<ClassMethod>) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
23
|
+
unmagler.saveClassMethod('Provides', node);
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
ClassProperty: {
|
|
27
|
+
enter({ node }: NodePath<ClassProperty>) {
|
|
28
|
+
unmagler.saveClassProperty('Inject', node);
|
|
29
|
+
unmagler.saveClassProperty('LazyInject', node);
|
|
36
30
|
},
|
|
37
31
|
},
|
|
38
32
|
Identifier: {
|
|
39
33
|
enter({ node }: NodePath<Identifier>) {
|
|
40
|
-
|
|
41
|
-
if (getDecoratorName(decorator) === 'Inject') {
|
|
42
|
-
saveUnmangledParamName(node, decorator!);
|
|
43
|
-
}
|
|
34
|
+
unmagler.saveIdentifier('Inject', node);
|
|
44
35
|
},
|
|
45
36
|
},
|
|
46
37
|
TSParameterProperty: {
|
|
47
38
|
enter({ node }: NodePath<TSParameterProperty>) {
|
|
48
|
-
|
|
49
|
-
if (getDecoratorName(decorator) === 'Inject') {
|
|
50
|
-
saveUnmangledParamName(node, decorator!);
|
|
51
|
-
}
|
|
39
|
+
unmagler.saveTSParameterProperty('Inject', node);
|
|
52
40
|
},
|
|
53
41
|
},
|
|
54
42
|
};
|
|
55
43
|
|
|
56
|
-
function saveUnmangledParamName(node: Identifier | TSParameterProperty, decorator: Decorator) {
|
|
57
|
-
if (injectIsNotNamed(decorator)) {
|
|
58
|
-
passParamNameAsInjectArgument(node, decorator);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function injectIsNotNamed(decorator: Decorator): boolean {
|
|
63
|
-
return getDecoratorArgument(decorator) === undefined;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function convertProviderParamsToDestructuringAssignment(node: ClassMethod) {
|
|
67
|
-
if (node.params.length === 0) return;
|
|
68
|
-
const destructuredParams = paramsToDestructuringAssignment(node.params);
|
|
69
|
-
node.params.length = 0;
|
|
70
|
-
node.params.push(destructuredParams);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function saveUnmangledMethodNameInProviderArguments(node: ClassMethod, decorator: Decorator) {
|
|
74
|
-
if (providerIsNotNamed(decorator)) {
|
|
75
|
-
addNameToProviderArguments(node, decorator);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
44
|
export default function plugin() {
|
|
80
45
|
return providerArgumentsTransformer;
|
|
81
46
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ClassMethod, Decorator } from '@babel/types';
|
|
2
|
+
import {
|
|
3
|
+
addNameToProviderArguments,
|
|
4
|
+
getDecoratorName,
|
|
5
|
+
getDecoratorByName,
|
|
6
|
+
paramsToDestructuringAssignment,
|
|
7
|
+
providerIsNotNamed,
|
|
8
|
+
} from '../helpers';
|
|
9
|
+
|
|
10
|
+
function saveMethod(name: string, node: ClassMethod) {
|
|
11
|
+
const decorator = getDecoratorByName(node.decorators, name);
|
|
12
|
+
if (getDecoratorName(decorator) === name) {
|
|
13
|
+
convertProviderParamsToDestructuringAssignment(node);
|
|
14
|
+
saveUnmangledMethodNameInProviderArguments(node, decorator!);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function convertProviderParamsToDestructuringAssignment(node: ClassMethod) {
|
|
19
|
+
if (node.params.length === 0) { return; }
|
|
20
|
+
const destructuredParams = paramsToDestructuringAssignment(node.params);
|
|
21
|
+
// eslint-disable-next-line no-param-reassign
|
|
22
|
+
node.params.length = 0;
|
|
23
|
+
node.params.push(destructuredParams);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function saveUnmangledMethodNameInProviderArguments(node: ClassMethod, decorator: Decorator) {
|
|
27
|
+
if (providerIsNotNamed(decorator)) {
|
|
28
|
+
addNameToProviderArguments(node, decorator);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export default saveMethod;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Decorator } from '@babel/types';
|
|
2
|
+
import {
|
|
3
|
+
getDecoratorName,
|
|
4
|
+
getDecoratorByName,
|
|
5
|
+
passParamNameAsInjectArgument,
|
|
6
|
+
getDecoratorArgument,
|
|
7
|
+
AcceptedNodeType,
|
|
8
|
+
} from '../helpers';
|
|
9
|
+
|
|
10
|
+
function savePropertyName(name: string, node: AcceptedNodeType) {
|
|
11
|
+
const decorator = getDecoratorByName(node.decorators, name);
|
|
12
|
+
if (getDecoratorName(decorator) === name && injectIsNotNamed(decorator!)) {
|
|
13
|
+
passParamNameAsInjectArgument(node, decorator!);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function injectIsNotNamed(decorator: Decorator): boolean {
|
|
18
|
+
return getDecoratorArgument(decorator) === undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default savePropertyName;
|