react-obsidian 0.0.9 → 0.0.13

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.
Files changed (77) hide show
  1. package/README.md +34 -2
  2. package/dist/src/GraphProperties.d.ts +1 -0
  3. package/dist/src/GraphProperties.d.ts.map +1 -1
  4. package/dist/src/GraphProperties.js +3 -0
  5. package/dist/src/GraphProperties.js.map +1 -1
  6. package/dist/src/ProvidedPropertiesStore.d.ts +1 -0
  7. package/dist/src/ProvidedPropertiesStore.d.ts.map +1 -1
  8. package/dist/src/ProvidedPropertiesStore.js +4 -0
  9. package/dist/src/ProvidedPropertiesStore.js.map +1 -1
  10. package/dist/src/decorators/Graph.d.ts +2 -3
  11. package/dist/src/decorators/Graph.d.ts.map +1 -1
  12. package/dist/src/decorators/Graph.js +2 -3
  13. package/dist/src/decorators/Graph.js.map +1 -1
  14. package/dist/src/decorators/Singleton.d.ts +4 -0
  15. package/dist/src/decorators/Singleton.d.ts.map +1 -0
  16. package/dist/src/decorators/Singleton.js +12 -0
  17. package/dist/src/decorators/Singleton.js.map +1 -0
  18. package/dist/src/decorators/provides/MemoizeDescriptor.d.ts +2 -0
  19. package/dist/src/decorators/provides/MemoizeDescriptor.d.ts.map +1 -0
  20. package/dist/src/decorators/provides/MemoizeDescriptor.js +23 -0
  21. package/dist/src/decorators/provides/MemoizeDescriptor.js.map +1 -0
  22. package/dist/src/decorators/provides/Provides.d.ts +0 -2
  23. package/dist/src/decorators/provides/Provides.d.ts.map +1 -1
  24. package/dist/src/decorators/provides/Provides.js +2 -2
  25. package/dist/src/decorators/provides/Provides.js.map +1 -1
  26. package/dist/src/graph/ObjectGraph.d.ts +2 -4
  27. package/dist/src/graph/ObjectGraph.d.ts.map +1 -1
  28. package/dist/src/graph/ObjectGraph.js +4 -3
  29. package/dist/src/graph/ObjectGraph.js.map +1 -1
  30. package/dist/src/graph/ProviderBinder.d.ts +3 -0
  31. package/dist/src/graph/ProviderBinder.d.ts.map +1 -0
  32. package/dist/src/graph/ProviderBinder.js +18 -0
  33. package/dist/src/graph/ProviderBinder.js.map +1 -0
  34. package/dist/src/graph/registry/GraphRegistry.d.ts +8 -8
  35. package/dist/src/graph/registry/GraphRegistry.d.ts.map +1 -1
  36. package/dist/src/graph/registry/GraphRegistry.js +23 -22
  37. package/dist/src/graph/registry/GraphRegistry.js.map +1 -1
  38. package/dist/src/index.d.ts +1 -0
  39. package/dist/src/index.d.ts.map +1 -1
  40. package/dist/src/index.js +3 -1
  41. package/dist/src/index.js.map +1 -1
  42. package/dist/src/types/index.d.ts +0 -1
  43. package/dist/src/types/index.d.ts.map +1 -1
  44. package/dist/test/integration/fixtures/SingletonGraph.d.ts +6 -0
  45. package/dist/test/integration/fixtures/SingletonGraph.d.ts.map +1 -0
  46. package/dist/test/integration/fixtures/SingletonGraph.js +53 -0
  47. package/dist/test/integration/fixtures/SingletonGraph.js.map +1 -0
  48. package/dist/test/integration/fixtures/Subgraph.d.ts +1 -0
  49. package/dist/test/integration/fixtures/Subgraph.d.ts.map +1 -1
  50. package/dist/test/integration/fixtures/Subgraph.js +11 -1
  51. package/dist/test/integration/fixtures/Subgraph.js.map +1 -1
  52. package/dist/test/integration/fixtures/UniqueNumberGraph.d.ts +8 -0
  53. package/dist/test/integration/fixtures/UniqueNumberGraph.d.ts.map +1 -0
  54. package/dist/test/integration/fixtures/UniqueNumberGraph.js +62 -0
  55. package/dist/test/integration/fixtures/UniqueNumberGraph.js.map +1 -0
  56. package/package.json +1 -1
  57. package/src/GraphProperties.ts +4 -0
  58. package/src/ProvidedPropertiesStore.ts +4 -0
  59. package/src/decorators/Graph.ts +2 -5
  60. package/src/decorators/Singleton.ts +14 -0
  61. package/src/decorators/provides/MemoizeDescriptor.ts +13 -0
  62. package/src/decorators/provides/Provides.ts +2 -4
  63. package/src/graph/ObjectGraph.ts +4 -5
  64. package/src/graph/ProviderBinder.ts +12 -0
  65. package/src/graph/registry/GraphRegistry.ts +27 -32
  66. package/src/index.ts +1 -0
  67. package/src/types/index.ts +0 -2
  68. package/dist/src/ScopedValuesRegistry.d.ts +0 -11
  69. package/dist/src/ScopedValuesRegistry.d.ts.map +0 -1
  70. package/dist/src/ScopedValuesRegistry.js +0 -25
  71. package/dist/src/ScopedValuesRegistry.js.map +0 -1
  72. package/dist/src/decorators/provides/PropertyDescriptorToSingleton.d.ts +0 -2
  73. package/dist/src/decorators/provides/PropertyDescriptorToSingleton.d.ts.map +0 -1
  74. package/dist/src/decorators/provides/PropertyDescriptorToSingleton.js +0 -20
  75. package/dist/src/decorators/provides/PropertyDescriptorToSingleton.js.map +0 -1
  76. package/src/ScopedValuesRegistry.ts +0 -26
  77. package/src/decorators/provides/PropertyDescriptorToSingleton.ts +0 -11
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
22
+ };
23
+ var __metadata = (this && this.__metadata) || function (k, v) {
24
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ var lodash_1 = require("lodash");
28
+ var Singleton_1 = require("../../../src/decorators/Singleton");
29
+ var src_1 = require("../../../src");
30
+ var SingletonGraph = /** @class */ (function (_super) {
31
+ __extends(SingletonGraph, _super);
32
+ function SingletonGraph() {
33
+ var _this = _super !== null && _super.apply(this, arguments) || this;
34
+ _this.id = (0, lodash_1.uniqueId)();
35
+ return _this;
36
+ }
37
+ SingletonGraph.prototype.instanceId = function () {
38
+ return "graph".concat(this.id);
39
+ };
40
+ __decorate([
41
+ (0, src_1.Provides)(),
42
+ __metadata("design:type", Function),
43
+ __metadata("design:paramtypes", []),
44
+ __metadata("design:returntype", String)
45
+ ], SingletonGraph.prototype, "instanceId", null);
46
+ SingletonGraph = __decorate([
47
+ (0, Singleton_1.Singleton)(),
48
+ (0, src_1.Graph)()
49
+ ], SingletonGraph);
50
+ return SingletonGraph;
51
+ }(src_1.ObjectGraph));
52
+ exports.default = SingletonGraph;
53
+ //# sourceMappingURL=SingletonGraph.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SingletonGraph.js","sourceRoot":"","sources":["../../../../test/integration/fixtures/SingletonGraph.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAkC;AAClC,+DAA8D;AAC9D,oCAA4D;AAG5D;IAA4C,kCAAW;IAAvD;QAAA,qEAOC;QANS,QAAE,GAAG,IAAA,iBAAQ,GAAE,CAAC;;IAM1B,CAAC;IAHC,mCAAU,GAAV;QACE,OAAO,eAAQ,IAAI,CAAC,EAAE,CAAE,CAAC;IAC3B,CAAC;IAFD;QADC,IAAA,cAAQ,GAAE;;;;oDAGV;IANkB,cAAc;QADlC,IAAA,qBAAS,GAAE;QAAE,IAAA,WAAK,GAAE;OACA,cAAc,CAOlC;IAAD,qBAAC;CAAA,AAPD,CAA4C,iBAAW,GAOtD;kBAPoB,cAAc"}
@@ -4,5 +4,6 @@ export default class Subgraph extends ObjectGraph {
4
4
  stringProvider(): StringProvider;
5
5
  stringFromSubgraph(): string;
6
6
  unusedDependency(): string;
7
+ instanceId(): string;
7
8
  }
8
9
  //# sourceMappingURL=Subgraph.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Subgraph.d.ts","sourceRoot":"","sources":["../../../../test/integration/fixtures/Subgraph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,WAAW,EAAY,MAAM,cAAc,CAAC;AAE5D,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAG9C,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,WAAW;IAE/C,cAAc,IAAI,cAAc;IAKhC,kBAAkB,IAAI,MAAM;IAK5B,gBAAgB,IAAI,MAAM;CAG3B"}
1
+ {"version":3,"file":"Subgraph.d.ts","sourceRoot":"","sources":["../../../../test/integration/fixtures/Subgraph.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,WAAW,EAAY,MAAM,cAAc,CAAC;AAE5D,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAG9C,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,WAAW;IAE/C,cAAc,IAAI,cAAc;IAKhC,kBAAkB,IAAI,MAAM;IAK5B,gBAAgB,IAAI,MAAM;IAK1B,UAAU,IAAI,MAAM;CAGrB"}
@@ -27,6 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
+ var lodash_1 = require("lodash");
30
31
  var src_1 = require("../../../src");
31
32
  var injectedValues_1 = __importDefault(require("./injectedValues"));
32
33
  var StringProvider_1 = __importDefault(require("./StringProvider"));
@@ -42,7 +43,10 @@ var Subgraph = /** @class */ (function (_super) {
42
43
  return injectedValues_1.default.fromSubgraph;
43
44
  };
44
45
  Subgraph.prototype.unusedDependency = function () {
45
- throw Error('This dependency should not have been resolved since it not required by anyone.');
46
+ throw Error('This dependency should not have been resolved since it is not required by anyone.');
47
+ };
48
+ Subgraph.prototype.instanceId = function () {
49
+ return (0, lodash_1.uniqueId)('graph');
46
50
  };
47
51
  __decorate([
48
52
  (0, src_1.Provides)(),
@@ -62,6 +66,12 @@ var Subgraph = /** @class */ (function (_super) {
62
66
  __metadata("design:paramtypes", []),
63
67
  __metadata("design:returntype", String)
64
68
  ], Subgraph.prototype, "unusedDependency", null);
69
+ __decorate([
70
+ (0, src_1.Provides)(),
71
+ __metadata("design:type", Function),
72
+ __metadata("design:paramtypes", []),
73
+ __metadata("design:returntype", String)
74
+ ], Subgraph.prototype, "instanceId", null);
65
75
  Subgraph = __decorate([
66
76
  (0, src_1.Graph)()
67
77
  ], Subgraph);
@@ -1 +1 @@
1
- {"version":3,"file":"Subgraph.js","sourceRoot":"","sources":["../../../../test/integration/fixtures/Subgraph.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oCAA4D;AAC5D,oEAA8C;AAC9C,oEAA8C;AAG9C;IAAsC,4BAAW;IAAjD;;IAeA,CAAC;IAbC,iCAAc,GAAd;QACE,OAAO,IAAI,wBAAc,EAAE,CAAC;IAC9B,CAAC;IAGD,qCAAkB,GAAlB;QACE,OAAO,wBAAc,CAAC,YAAY,CAAC;IACrC,CAAC;IAGD,mCAAgB,GAAhB;QACE,MAAM,KAAK,CAAC,gFAAgF,CAAC,CAAC;IAChG,CAAC;IAZD;QADC,IAAA,cAAQ,GAAE;;;wCACO,wBAAc;kDAE/B;IAGD;QADC,IAAA,cAAQ,GAAE;;;;sDAGV;IAGD;QADC,IAAA,cAAQ,GAAE;;;;oDAGV;IAdkB,QAAQ;QAD5B,IAAA,WAAK,GAAE;OACa,QAAQ,CAe5B;IAAD,eAAC;CAAA,AAfD,CAAsC,iBAAW,GAehD;kBAfoB,QAAQ"}
1
+ {"version":3,"file":"Subgraph.js","sourceRoot":"","sources":["../../../../test/integration/fixtures/Subgraph.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAkC;AAClC,oCAA4D;AAC5D,oEAA8C;AAC9C,oEAA8C;AAG9C;IAAsC,4BAAW;IAAjD;;IAoBA,CAAC;IAlBC,iCAAc,GAAd;QACE,OAAO,IAAI,wBAAc,EAAE,CAAC;IAC9B,CAAC;IAGD,qCAAkB,GAAlB;QACE,OAAO,wBAAc,CAAC,YAAY,CAAC;IACrC,CAAC;IAGD,mCAAgB,GAAhB;QACE,MAAM,KAAK,CAAC,mFAAmF,CAAC,CAAC;IACnG,CAAC;IAGD,6BAAU,GAAV;QACE,OAAO,IAAA,iBAAQ,EAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IAjBD;QADC,IAAA,cAAQ,GAAE;;;wCACO,wBAAc;kDAE/B;IAGD;QADC,IAAA,cAAQ,GAAE;;;;sDAGV;IAGD;QADC,IAAA,cAAQ,GAAE;;;;oDAGV;IAGD;QADC,IAAA,cAAQ,GAAE;;;;8CAGV;IAnBkB,QAAQ;QAD5B,IAAA,WAAK,GAAE;OACa,QAAQ,CAoB5B;IAAD,eAAC;CAAA,AApBD,CAAsC,iBAAW,GAoBhD;kBApBoB,QAAQ"}
@@ -0,0 +1,8 @@
1
+ import { ObjectGraph } from '../../../src';
2
+ export declare class UniqueNumberGraph extends ObjectGraph {
3
+ private uniqueNumberGenerator;
4
+ constructor(uniqueNumberGenerator: () => number);
5
+ singletonNumber(): number;
6
+ instanceNumber(): number;
7
+ }
8
+ //# sourceMappingURL=UniqueNumberGraph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UniqueNumberGraph.d.ts","sourceRoot":"","sources":["../../../../test/integration/fixtures/UniqueNumberGraph.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,WAAW,EAGZ,MAAM,cAAc,CAAC;AAEtB,qBACa,iBAAkB,SAAQ,WAAW;IACpC,OAAO,CAAC,qBAAqB;gBAArB,qBAAqB,EAAE,MAAM,MAAM;IAKvD,eAAe,IAAI,MAAM;IAKzB,cAAc,IAAI,MAAM;CAGzB"}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
22
+ };
23
+ var __metadata = (this && this.__metadata) || function (k, v) {
24
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.UniqueNumberGraph = void 0;
28
+ var src_1 = require("../../../src");
29
+ var UniqueNumberGraph = /** @class */ (function (_super) {
30
+ __extends(UniqueNumberGraph, _super);
31
+ function UniqueNumberGraph(uniqueNumberGenerator) {
32
+ var _this = _super.call(this) || this;
33
+ _this.uniqueNumberGenerator = uniqueNumberGenerator;
34
+ return _this;
35
+ }
36
+ UniqueNumberGraph.prototype.singletonNumber = function () {
37
+ return this.uniqueNumberGenerator();
38
+ };
39
+ UniqueNumberGraph.prototype.instanceNumber = function () {
40
+ return this.uniqueNumberGenerator();
41
+ };
42
+ __decorate([
43
+ (0, src_1.Provides)(),
44
+ (0, src_1.Singleton)(),
45
+ __metadata("design:type", Function),
46
+ __metadata("design:paramtypes", []),
47
+ __metadata("design:returntype", Number)
48
+ ], UniqueNumberGraph.prototype, "singletonNumber", null);
49
+ __decorate([
50
+ (0, src_1.Provides)(),
51
+ __metadata("design:type", Function),
52
+ __metadata("design:paramtypes", []),
53
+ __metadata("design:returntype", Number)
54
+ ], UniqueNumberGraph.prototype, "instanceNumber", null);
55
+ UniqueNumberGraph = __decorate([
56
+ (0, src_1.Graph)(),
57
+ __metadata("design:paramtypes", [Function])
58
+ ], UniqueNumberGraph);
59
+ return UniqueNumberGraph;
60
+ }(src_1.ObjectGraph));
61
+ exports.UniqueNumberGraph = UniqueNumberGraph;
62
+ //# sourceMappingURL=UniqueNumberGraph.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UniqueNumberGraph.js","sourceRoot":"","sources":["../../../../test/integration/fixtures/UniqueNumberGraph.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oCAKsB;AAGtB;IAAuC,qCAAW;IAChD,2BAAoB,qBAAmC;QAAvD,YACE,iBAAO,SACR;QAFmB,2BAAqB,GAArB,qBAAqB,CAAc;;IAEvD,CAAC;IAGD,2CAAe,GAAf;QACE,OAAO,IAAI,CAAC,qBAAqB,EAAE,CAAC;IACtC,CAAC;IAGD,0CAAc,GAAd;QACE,OAAO,IAAI,CAAC,qBAAqB,EAAE,CAAC;IACtC,CAAC;IAPD;QADC,IAAA,cAAQ,GAAE;QAAE,IAAA,eAAS,GAAE;;;;4DAGvB;IAGD;QADC,IAAA,cAAQ,GAAE;;;;2DAGV;IAbU,iBAAiB;QAD7B,IAAA,WAAK,GAAE;;OACK,iBAAiB,CAc7B;IAAD,wBAAC;CAAA,AAdD,CAAuC,iBAAW,GAcjD;AAdY,8CAAiB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-obsidian",
3
- "version": "0.0.9",
3
+ "version": "0.0.13",
4
4
  "description": "Dependency injection framework for React and React Native applications",
5
5
  "main": "src/index.ts",
6
6
  "directories": {
@@ -8,4 +8,8 @@ export default class GraphProperties {
8
8
  getMangledProperty(unmangledProperty: string): string | undefined {
9
9
  return this.unmangledToMangledPropKeys.get(unmangledProperty);
10
10
  }
11
+
12
+ getMangledProperties(): string[] {
13
+ return Array.from(this.unmangledToMangledPropKeys.values());
14
+ }
11
15
  }
@@ -14,6 +14,10 @@ class ProvidedPropertiesStore {
14
14
  this.providedPropertiesForGraph.set(graph.name, graphProperties);
15
15
  }
16
16
 
17
+ getMangledProperties(graph: Graph): string[] {
18
+ return this.providedPropertiesForGraph.get(graph.name)?.getMangledProperties() ?? [];
19
+ }
20
+
17
21
  clear(graph: Graph) {
18
22
  this.providedPropertiesForGraph.delete(graph.name);
19
23
  }
@@ -1,20 +1,17 @@
1
- import { Constructable, Scope } from '../types';
1
+ import { Constructable } from '../types';
2
2
  import 'reflect-metadata';
3
3
  import graphRegistry from '../graph/registry/GraphRegistry';
4
4
  import { ObjectGraph } from '../graph/ObjectGraph';
5
5
 
6
6
  interface GraphParams {
7
- scope: Scope | undefined;
8
7
  subgraphs: Constructable<ObjectGraph>[];
9
8
  }
10
9
 
11
10
  export function Graph({
12
- scope,
13
11
  subgraphs = [],
14
12
  }: Partial<GraphParams> = {}) {
15
13
  return <T extends ObjectGraph>(constructor: Constructable<T>) => {
16
- Reflect.defineMetadata('scope', scope, constructor);
17
- graphRegistry.register(constructor, scope, subgraphs);
14
+ graphRegistry.register(constructor, subgraphs);
18
15
  return constructor;
19
16
  };
20
17
  }
@@ -0,0 +1,14 @@
1
+ import { Constructable } from 'src';
2
+ import { ObjectGraph } from '../graph/ObjectGraph';
3
+
4
+ export function Singleton() {
5
+ return function singleton(
6
+ constructorOrGraph: Constructable<ObjectGraph> | ObjectGraph,
7
+ _property?: string,
8
+ descriptor?: PropertyDescriptor,
9
+ ): any {
10
+ const target = descriptor || constructorOrGraph;
11
+ Reflect.defineMetadata('isSingleton', true, target);
12
+ return target;
13
+ };
14
+ }
@@ -0,0 +1,13 @@
1
+ /* eslint-disable no-param-reassign */
2
+ export function memoizeDescriptor(propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor {
3
+ const originalValue = descriptor.value;
4
+ descriptor.value = function value(...args: any[]) {
5
+ const memoizationTarget = Reflect.getMetadata('isSingleton', descriptor) ? descriptor : this;
6
+ const key = `memoized${propertyKey}`;
7
+ if (Reflect.hasMetadata(key, memoizationTarget)) return Reflect.getMetadata(key, memoizationTarget);
8
+ const result = originalValue.apply(this, args);
9
+ Reflect.defineMetadata(key, result, memoizationTarget);
10
+ return result;
11
+ };
12
+ return descriptor;
13
+ }
@@ -1,16 +1,14 @@
1
- import { Scope } from '../../types';
2
1
  import { Graph } from '../../graph/Graph';
3
2
  import providedPropertiesStore from '../../ProvidedPropertiesStore';
4
- import makeSingleton from './PropertyDescriptorToSingleton';
3
+ import { memoizeDescriptor } from './MemoizeDescriptor';
5
4
 
6
5
  interface ProvidesParams {
7
- scope?: Scope;
8
6
  name: string;
9
7
  }
10
8
 
11
9
  export function Provides({ name }: Partial<ProvidesParams> = {}) {
12
10
  return function provide(graph: Graph, propertyKey: string, descriptor: PropertyDescriptor) {
13
11
  providedPropertiesStore.set(graph, propertyKey, name!);
14
- return makeSingleton(descriptor);
12
+ return memoizeDescriptor(propertyKey, descriptor);
15
13
  };
16
14
  }
@@ -1,12 +1,10 @@
1
1
  import { uniqueId } from 'lodash';
2
2
  import Memoize from '../decorators/Memoize';
3
- import { Scope } from '../types';
3
+ import { bindProviders } from './ProviderBinder';
4
4
  import { Graph } from './Graph';
5
5
  import PropertyRetriever from './PropertyRetriever';
6
6
 
7
7
  export abstract class ObjectGraph<T = unknown> implements Graph {
8
- // TODO? rename scope to singleInstance
9
- public scope!: Scope;
10
8
  private propertyRetriever = new PropertyRetriever(this);
11
9
 
12
10
  @Memoize()
@@ -14,8 +12,9 @@ export abstract class ObjectGraph<T = unknown> implements Graph {
14
12
  return uniqueId(this.constructor.name);
15
13
  }
16
14
 
17
- // eslint-disable-next-line no-useless-constructor, no-unused-vars, no-empty-function
18
- constructor(protected props?: T) {}
15
+ constructor(protected _props?: T) {
16
+ bindProviders(this);
17
+ }
19
18
 
20
19
  retrieve<Dependency>(property: string, receiver?: unknown): Dependency | undefined {
21
20
  return this.propertyRetriever.retrieve(property, receiver) as Dependency | undefined;
@@ -0,0 +1,12 @@
1
+ /* eslint-disable no-param-reassign */
2
+ import providedPropertiesStore from '../ProvidedPropertiesStore';
3
+ import { Graph } from './Graph';
4
+
5
+ export function bindProviders(graph: Graph & Record<string, any>) {
6
+ providedPropertiesStore.getMangledProperties(graph)
7
+ .forEach((method) => {
8
+ if (graph[method] !== undefined) {
9
+ graph[method] = graph[method].bind(graph);
10
+ }
11
+ });
12
+ }
@@ -1,37 +1,18 @@
1
- import { Constructable, Scope } from '../../types';
1
+ import { Constructable } from '../../types';
2
2
  import { Graph } from '../Graph';
3
3
  import { Middleware } from './Middleware';
4
4
  import GraphMiddlewareChain from './GraphMiddlewareChain';
5
5
 
6
- class GraphRegistry {
7
- private readonly scopedGraphs: Record<Scope, Constructable<Graph>> = {};
8
- private readonly constructorToInstance = new Map<Constructable<Graph>, Graph>();
6
+ export class GraphRegistry {
7
+ private readonly constructorToInstance = new Map<Constructable<Graph>, Set<Graph>>();
9
8
  private readonly instanceToConstructor = new Map<Graph, Constructable<Graph>>();
10
9
  private readonly graphToSubgraphs = new Map<Constructable<Graph>, Set<Constructable<Graph>>>();
11
- private graphMiddlewares = new GraphMiddlewareChain();
10
+ private readonly graphMiddlewares = new GraphMiddlewareChain();
12
11
 
13
- register(
14
- constructor: Constructable<Graph>,
15
- scope: Scope | undefined = undefined,
16
- subgraphs: Constructable<Graph>[] = [],
17
- ) {
18
- if (scope) this.scopedGraphs[scope] = constructor;
12
+ register(constructor: Constructable<Graph>, subgraphs: Constructable<Graph>[] = []) {
19
13
  this.graphToSubgraphs.set(constructor, new Set(subgraphs));
20
14
  }
21
15
 
22
- has(Graph: Constructable<Graph>) {
23
- return this.constructorToInstance.has(Graph);
24
- }
25
-
26
- get<T extends Graph>(Graph: Constructable<T>): T {
27
- return this.constructorToInstance.get(Graph)! as unknown as T;
28
- }
29
-
30
- set(Graph: Constructable<Graph>, graph: Graph) {
31
- this.constructorToInstance.set(Graph, graph);
32
- this.instanceToConstructor.set(graph, Graph);
33
- }
34
-
35
16
  getSubgraphs(graph: Graph): Graph[] {
36
17
  const Graph = this.instanceToConstructor.get(graph)!;
37
18
  const subgraphs = this.graphToSubgraphs.get(Graph) ?? new Set();
@@ -39,23 +20,37 @@ class GraphRegistry {
39
20
  }
40
21
 
41
22
  resolve<T extends Graph>(Graph: Constructable<T>, props?: any): T {
42
- if (this.has(Graph)) {
43
- return this.get(Graph);
44
- // const graph: T = this.get(Graph);
45
- // const scope = Reflect.getMetadata('scope', Graph);
46
- // if (scope) return graph;
47
-
48
- // this.set(Graph, new Graph(props));
23
+ if (this.isSingleton(Graph) && this.has(Graph)) {
24
+ return this.getFirst(Graph);
49
25
  }
50
26
  const graph = this.graphMiddlewares.resolve(Graph, props);
51
27
  this.set(Graph, graph);
52
28
  return graph as T;
53
29
  }
54
30
 
31
+ private has(Graph: Constructable<Graph>) {
32
+ return this.constructorToInstance.has(Graph);
33
+ }
34
+
35
+ private getFirst<T extends Graph>(Graph: Constructable<T>): T {
36
+ return this.constructorToInstance.get(Graph)!.values().next().value;
37
+ }
38
+
39
+ private set(Graph: Constructable<Graph>, graph: Graph) {
40
+ const graphs = this.constructorToInstance.get(Graph) ?? new Set();
41
+ graphs.add(graph);
42
+ this.constructorToInstance.set(Graph, graphs);
43
+ this.instanceToConstructor.set(graph, Graph);
44
+ }
45
+
46
+ private isSingleton(Graph: Constructable<Graph>): boolean {
47
+ return Reflect.getMetadata('isSingleton', Graph) ?? false;
48
+ }
49
+
55
50
  clear(graph: Graph) {
56
51
  const Graph = this.instanceToConstructor.get(graph)!;
57
52
  this.instanceToConstructor.delete(graph);
58
- this.constructorToInstance.delete(Graph);
53
+ this.constructorToInstance.get(Graph)!.delete(graph);
59
54
  }
60
55
 
61
56
  addGraphMiddleware(middleware: Middleware<Graph>) {
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ import { Constructable } from './types';
8
8
  export * from './types';
9
9
 
10
10
  export { Graph } from './decorators/Graph';
11
+ export { Singleton } from './decorators/Singleton';
11
12
  export { ObjectGraph } from './graph/ObjectGraph';
12
13
  export { Graph as IGraph } from './graph/Graph';
13
14
  export { Provides } from './decorators/provides/Provides';
@@ -2,8 +2,6 @@ export type Constructable<T> = {
2
2
  new(...args: any): T;
3
3
  };
4
4
 
5
- export type Scope = string | number | symbol;
6
-
7
5
  export type Constructor = { new(...args: any[]): any };
8
6
 
9
7
  export type ServiceLocator<Clazz> = {
@@ -1,11 +0,0 @@
1
- import { Scope } from './types';
2
- declare class ScopedValuesRegistry {
3
- private readonly values;
4
- has(scope: Scope, property: string): boolean;
5
- get(scope: Scope, property: string): any;
6
- set(scope: Scope, property: string, value: any): void;
7
- clear(scope: Scope, property: string): void;
8
- }
9
- declare const _default: ScopedValuesRegistry;
10
- export default _default;
11
- //# sourceMappingURL=ScopedValuesRegistry.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ScopedValuesRegistry.d.ts","sourceRoot":"","sources":["../../src/ScopedValuesRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,cAAM,oBAAoB;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA+B;IAEtD,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO;IAI5C,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,GAAG,GAAG;IAOxC,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG;IAI9C,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM;CAGrC;;AAED,wBAA0C"}
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var ScopedValuesRegistry = /** @class */ (function () {
4
- function ScopedValuesRegistry() {
5
- this.values = new Map();
6
- }
7
- ScopedValuesRegistry.prototype.has = function (scope, property) {
8
- return this.values.has(scope.toString() + property);
9
- };
10
- ScopedValuesRegistry.prototype.get = function (scope, property) {
11
- if (this.values.has(scope.toString() + property)) {
12
- return this.values.get(scope.toString() + property);
13
- }
14
- throw new Error("Property ".concat(property, " does not exist"));
15
- };
16
- ScopedValuesRegistry.prototype.set = function (scope, property, value) {
17
- this.values.set(scope.toString() + property, value);
18
- };
19
- ScopedValuesRegistry.prototype.clear = function (scope, property) {
20
- this.values.delete(scope.toString() + property);
21
- };
22
- return ScopedValuesRegistry;
23
- }());
24
- exports.default = new ScopedValuesRegistry();
25
- //# sourceMappingURL=ScopedValuesRegistry.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ScopedValuesRegistry.js","sourceRoot":"","sources":["../../src/ScopedValuesRegistry.ts"],"names":[],"mappings":";;AAEA;IAAA;QACmB,WAAM,GAAqB,IAAI,GAAG,EAAE,CAAC;IAoBxD,CAAC;IAlBC,kCAAG,GAAH,UAAI,KAAY,EAAE,QAAgB;QAChC,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,QAAQ,CAAC,CAAC;IACtD,CAAC;IAED,kCAAG,GAAH,UAAI,KAAY,EAAE,QAAgB;QAChC,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,QAAQ,CAAC,EAAE;YAChD,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,QAAQ,CAAC,CAAC;SACrD;QACD,MAAM,IAAI,KAAK,CAAC,mBAAY,QAAQ,oBAAiB,CAAC,CAAC;IACzD,CAAC;IAED,kCAAG,GAAH,UAAI,KAAY,EAAE,QAAgB,EAAE,KAAU;QAC5C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,QAAQ,EAAE,KAAK,CAAC,CAAC;IACtD,CAAC;IAED,oCAAK,GAAL,UAAM,KAAY,EAAE,QAAgB;QAClC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,QAAQ,CAAC,CAAC;IAClD,CAAC;IACH,2BAAC;AAAD,CAAC,AArBD,IAqBC;AAED,kBAAe,IAAI,oBAAoB,EAAE,CAAC"}
@@ -1,2 +0,0 @@
1
- export default function makeSingleton(descriptor: PropertyDescriptor): PropertyDescriptor;
2
- //# sourceMappingURL=PropertyDescriptorToSingleton.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PropertyDescriptorToSingleton.d.ts","sourceRoot":"","sources":["../../../../src/decorators/provides/PropertyDescriptorToSingleton.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,UAAU,EAAE,kBAAkB,GAAG,kBAAkB,CASxF"}
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /* eslint-disable no-param-reassign */
4
- function makeSingleton(descriptor) {
5
- var originalValue = descriptor.value;
6
- var createdDependency;
7
- descriptor.value = function value() {
8
- var args = [];
9
- for (var _i = 0; _i < arguments.length; _i++) {
10
- args[_i] = arguments[_i];
11
- }
12
- if (createdDependency)
13
- return createdDependency;
14
- createdDependency = originalValue.apply(this, args);
15
- return createdDependency;
16
- };
17
- return descriptor;
18
- }
19
- exports.default = makeSingleton;
20
- //# sourceMappingURL=PropertyDescriptorToSingleton.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PropertyDescriptorToSingleton.js","sourceRoot":"","sources":["../../../../src/decorators/provides/PropertyDescriptorToSingleton.ts"],"names":[],"mappings":";;AAAA,sCAAsC;AACtC,SAAwB,aAAa,CAAC,UAA8B;IAClE,IAAM,aAAa,GAAG,UAAU,CAAC,KAAK,CAAC;IACvC,IAAI,iBAAsB,CAAC;IAC3B,UAAU,CAAC,KAAK,GAAG,SAAS,KAAK;QAAC,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,yBAAc;;QAC9C,IAAI,iBAAiB;YAAE,OAAO,iBAAiB,CAAC;QAChD,iBAAiB,GAAG,aAAa,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACpD,OAAO,iBAAiB,CAAC;IAC3B,CAAC,CAAC;IACF,OAAO,UAAU,CAAC;AACpB,CAAC;AATD,gCASC"}
@@ -1,26 +0,0 @@
1
- import { Scope } from './types';
2
-
3
- class ScopedValuesRegistry {
4
- private readonly values: Map<string, any> = new Map();
5
-
6
- has(scope: Scope, property: string): boolean {
7
- return this.values.has(scope.toString() + property);
8
- }
9
-
10
- get(scope: Scope, property: string): any {
11
- if (this.values.has(scope.toString() + property)) {
12
- return this.values.get(scope.toString() + property);
13
- }
14
- throw new Error(`Property ${property} does not exist`);
15
- }
16
-
17
- set(scope: Scope, property: string, value: any) {
18
- this.values.set(scope.toString() + property, value);
19
- }
20
-
21
- clear(scope: Scope, property: string) {
22
- this.values.delete(scope.toString() + property);
23
- }
24
- }
25
-
26
- export default new ScopedValuesRegistry();
@@ -1,11 +0,0 @@
1
- /* eslint-disable no-param-reassign */
2
- export default function makeSingleton(descriptor: PropertyDescriptor): PropertyDescriptor {
3
- const originalValue = descriptor.value;
4
- let createdDependency: any;
5
- descriptor.value = function value(...args: any[]) {
6
- if (createdDependency) return createdDependency;
7
- createdDependency = originalValue.apply(this, args);
8
- return createdDependency;
9
- };
10
- return descriptor;
11
- }