react-obsidian 0.0.12 → 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 (50) hide show
  1. package/README.md +34 -2
  2. package/dist/src/decorators/Graph.d.ts +2 -3
  3. package/dist/src/decorators/Graph.d.ts.map +1 -1
  4. package/dist/src/decorators/Graph.js +2 -3
  5. package/dist/src/decorators/Graph.js.map +1 -1
  6. package/dist/src/decorators/Singleton.d.ts +4 -0
  7. package/dist/src/decorators/Singleton.d.ts.map +1 -0
  8. package/dist/src/decorators/Singleton.js +12 -0
  9. package/dist/src/decorators/Singleton.js.map +1 -0
  10. package/dist/src/decorators/provides/MemoizeDescriptor.d.ts.map +1 -1
  11. package/dist/src/decorators/provides/MemoizeDescriptor.js +4 -4
  12. package/dist/src/decorators/provides/MemoizeDescriptor.js.map +1 -1
  13. package/dist/src/decorators/provides/Provides.d.ts +0 -2
  14. package/dist/src/decorators/provides/Provides.d.ts.map +1 -1
  15. package/dist/src/decorators/provides/Provides.js.map +1 -1
  16. package/dist/src/graph/ObjectGraph.d.ts +0 -2
  17. package/dist/src/graph/ObjectGraph.d.ts.map +1 -1
  18. package/dist/src/graph/ObjectGraph.js.map +1 -1
  19. package/dist/src/graph/registry/GraphRegistry.d.ts +8 -8
  20. package/dist/src/graph/registry/GraphRegistry.d.ts.map +1 -1
  21. package/dist/src/graph/registry/GraphRegistry.js +23 -22
  22. package/dist/src/graph/registry/GraphRegistry.js.map +1 -1
  23. package/dist/src/index.d.ts +1 -0
  24. package/dist/src/index.d.ts.map +1 -1
  25. package/dist/src/index.js +3 -1
  26. package/dist/src/index.js.map +1 -1
  27. package/dist/src/types/index.d.ts +0 -1
  28. package/dist/src/types/index.d.ts.map +1 -1
  29. package/dist/test/integration/fixtures/SingletonGraph.d.ts +6 -0
  30. package/dist/test/integration/fixtures/SingletonGraph.d.ts.map +1 -0
  31. package/dist/test/integration/fixtures/SingletonGraph.js +53 -0
  32. package/dist/test/integration/fixtures/SingletonGraph.js.map +1 -0
  33. package/dist/test/integration/fixtures/UniqueNumberGraph.d.ts +8 -0
  34. package/dist/test/integration/fixtures/UniqueNumberGraph.d.ts.map +1 -0
  35. package/dist/test/integration/fixtures/UniqueNumberGraph.js +62 -0
  36. package/dist/test/integration/fixtures/UniqueNumberGraph.js.map +1 -0
  37. package/package.json +1 -1
  38. package/src/decorators/Graph.ts +2 -5
  39. package/src/decorators/Singleton.ts +14 -0
  40. package/src/decorators/provides/MemoizeDescriptor.ts +3 -4
  41. package/src/decorators/provides/Provides.ts +0 -2
  42. package/src/graph/ObjectGraph.ts +0 -3
  43. package/src/graph/registry/GraphRegistry.ts +27 -32
  44. package/src/index.ts +1 -0
  45. package/src/types/index.ts +0 -2
  46. package/dist/src/ScopedValuesRegistry.d.ts +0 -11
  47. package/dist/src/ScopedValuesRegistry.d.ts.map +0 -1
  48. package/dist/src/ScopedValuesRegistry.js +0 -25
  49. package/dist/src/ScopedValuesRegistry.js.map +0 -1
  50. package/src/ScopedValuesRegistry.ts +0 -26
package/README.md CHANGED
@@ -15,6 +15,8 @@ React Obsidian is guided by the principles of the Dependency Injection pattern,
15
15
  npm install react-obsidian
16
16
  ```
17
17
 
18
+ See the #Prerequisites section for additional requirements.
19
+
18
20
  ## Usage
19
21
 
20
22
  ### Declare an object graph
@@ -29,7 +31,7 @@ Both functions are annotated by the `@Provides()` annotation. This signals Obsid
29
31
  Notice how the biLogger function receives an `httpClient` as an argument. This means that `biLogger` has a dependency on `httpClient`. Obsidian will create an `httpClient` when `biLogger` is injected.
30
32
 
31
33
  ``` typescript
32
- @Graph()
34
+ @Singleton() @Graph()
33
35
  export default class ApplicationGraph extends ObjectGraph {
34
36
  @Provides()
35
37
  httpClient(): HttpClient {
@@ -118,6 +120,36 @@ Obsidian.obtain(ApplicationGraph).biLogger();
118
120
  > Note: While the function that provides the `biLogger` accepts an argument of type `HttpClient`, when obtaining dependencies directly from the graph, we don't provide dependencies ourselves as they are resolved by Obsidian.
119
121
 
120
122
  ## Advance usage
123
+ ### Singleton graphs and providers
124
+ Graphs and Providers can be marked as singletons with the `@Singleton` decorator. When a graph is marked as a singleton, when an instance of that graph is requested, Obsidian will reuse the existing instance. Graphs that are not annotated with the `@Singleton` decorator will be instantiated each time they are needed for injection.
125
+
126
+ Singleton providers are shared between all instances of a graph.
127
+
128
+ ```typescript
129
+ @Graph()
130
+ class PushedScreenGraph { // A new PushedScreenGraph instance is created each time the corresponding screen is created
131
+ @Provides()
132
+ presenter(): PushedScreenPresenter {
133
+ return new PushedScreenPresenter(); // Created each time PushedGraph is created
134
+ }
135
+
136
+ @Provides() @Singleton()
137
+ someUseCase(): SomeUseCase {
138
+ return new SomeUseCase(); // Created once for all PushedGraph instances
139
+ }
140
+ }
141
+ ```
142
+
143
+ In this example we declared a singleton graph. This means that all of its providers are also singleton.
144
+ ```typescript
145
+ @Singleton() @Graph()
146
+ class ApplicationGraph {
147
+ @Provides()
148
+ biLogger(): BiLogger {
149
+ return new BiLogger() // Created once because the graph is a singleton
150
+ }
151
+ }
152
+ ```
121
153
  ### Graph middlewares
122
154
  When working on large scale applications, a need to hook into various low level operations often arises. Obsidian lets you hook into the graph creation process by adding middlewares.
123
155
 
@@ -134,7 +166,7 @@ The following example demonstrates how to add a middleware that's used for loggi
134
166
 
135
167
  ```typescript
136
168
  const loggingMiddleware = new class extends GraphMiddleware {
137
- resolve<T extends ObjectGraph, Props>(resolveChain: ResolveChain, Graph: Constructable<T>, props?: Props) {
169
+ resolve<Props>(resolveChain: GraphResolveChain, Graph: Constructable<T>, props?: Props) {
138
170
  const t1 = Date.now();
139
171
  const graph = resolveChain.proceed(Graph, props);
140
172
  const t2 = Date.now();
@@ -1,10 +1,9 @@
1
- import { Constructable, Scope } from '../types';
1
+ import { Constructable } from '../types';
2
2
  import 'reflect-metadata';
3
3
  import { ObjectGraph } from '../graph/ObjectGraph';
4
4
  interface GraphParams {
5
- scope: Scope | undefined;
6
5
  subgraphs: Constructable<ObjectGraph>[];
7
6
  }
8
- export declare function Graph({ scope, subgraphs, }?: Partial<GraphParams>): <T extends ObjectGraph<unknown>>(constructor: Constructable<T>) => Constructable<T>;
7
+ export declare function Graph({ subgraphs, }?: Partial<GraphParams>): <T extends ObjectGraph<unknown>>(constructor: Constructable<T>) => Constructable<T>;
9
8
  export {};
10
9
  //# sourceMappingURL=Graph.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Graph.d.ts","sourceRoot":"","sources":["../../../src/decorators/Graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,UAAU,WAAW;IACnB,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;IACzB,SAAS,EAAE,aAAa,CAAC,WAAW,CAAC,EAAE,CAAC;CACzC;AAED,wBAAgB,KAAK,CAAC,EACpB,KAAK,EACL,SAAc,GACf,GAAE,OAAO,CAAC,WAAW,CAAM,uFAM3B"}
1
+ {"version":3,"file":"Graph.d.ts","sourceRoot":"","sources":["../../../src/decorators/Graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,UAAU,WAAW;IACnB,SAAS,EAAE,aAAa,CAAC,WAAW,CAAC,EAAE,CAAC;CACzC;AAED,wBAAgB,KAAK,CAAC,EACpB,SAAc,GACf,GAAE,OAAO,CAAC,WAAW,CAAM,uFAK3B"}
@@ -7,10 +7,9 @@ exports.Graph = void 0;
7
7
  require("reflect-metadata");
8
8
  var GraphRegistry_1 = __importDefault(require("../graph/registry/GraphRegistry"));
9
9
  function Graph(_a) {
10
- var _b = _a === void 0 ? {} : _a, scope = _b.scope, _c = _b.subgraphs, subgraphs = _c === void 0 ? [] : _c;
10
+ var _b = _a === void 0 ? {} : _a, _c = _b.subgraphs, subgraphs = _c === void 0 ? [] : _c;
11
11
  return function (constructor) {
12
- Reflect.defineMetadata('scope', scope, constructor);
13
- GraphRegistry_1.default.register(constructor, scope, subgraphs);
12
+ GraphRegistry_1.default.register(constructor, subgraphs);
14
13
  return constructor;
15
14
  };
16
15
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Graph.js","sourceRoot":"","sources":["../../../src/decorators/Graph.ts"],"names":[],"mappings":";;;;;;AACA,4BAA0B;AAC1B,kFAA4D;AAQ5D,SAAgB,KAAK,CAAC,EAGM;QAHN,qBAGI,EAAE,KAAA,EAF1B,KAAK,WAAA,EACL,iBAAc,EAAd,SAAS,mBAAG,EAAE,KAAA;IAEd,OAAO,UAAwB,WAA6B;QAC1D,OAAO,CAAC,cAAc,CAAC,OAAO,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC;QACpD,uBAAa,CAAC,QAAQ,CAAC,WAAW,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;QACtD,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AATD,sBASC"}
1
+ {"version":3,"file":"Graph.js","sourceRoot":"","sources":["../../../src/decorators/Graph.ts"],"names":[],"mappings":";;;;;;AACA,4BAA0B;AAC1B,kFAA4D;AAO5D,SAAgB,KAAK,CAAC,EAEM;QAFN,qBAEI,EAAE,KAAA,EAD1B,iBAAc,EAAd,SAAS,mBAAG,EAAE,KAAA;IAEd,OAAO,UAAwB,WAA6B;QAC1D,uBAAa,CAAC,QAAQ,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;QAC/C,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAPD,sBAOC"}
@@ -0,0 +1,4 @@
1
+ import { Constructable } from 'src';
2
+ import { ObjectGraph } from '../graph/ObjectGraph';
3
+ export declare function Singleton(): (constructorOrGraph: Constructable<ObjectGraph> | ObjectGraph, _property?: string | undefined, descriptor?: PropertyDescriptor | undefined) => any;
4
+ //# sourceMappingURL=Singleton.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Singleton.d.ts","sourceRoot":"","sources":["../../../src/decorators/Singleton.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,wBAAgB,SAAS,yBAED,cAAc,WAAW,CAAC,GAAG,WAAW,kFAG3D,GAAG,CAKP"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Singleton = void 0;
4
+ function Singleton() {
5
+ return function singleton(constructorOrGraph, _property, descriptor) {
6
+ var target = descriptor || constructorOrGraph;
7
+ Reflect.defineMetadata('isSingleton', true, target);
8
+ return target;
9
+ };
10
+ }
11
+ exports.Singleton = Singleton;
12
+ //# sourceMappingURL=Singleton.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Singleton.js","sourceRoot":"","sources":["../../../src/decorators/Singleton.ts"],"names":[],"mappings":";;;AAGA,SAAgB,SAAS;IACvB,OAAO,SAAS,SAAS,CACvB,kBAA4D,EAC5D,SAAkB,EAClB,UAA+B;QAE/B,IAAM,MAAM,GAAG,UAAU,IAAI,kBAAkB,CAAC;QAChD,OAAO,CAAC,cAAc,CAAC,aAAa,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QACpD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAVD,8BAUC"}
@@ -1 +1 @@
1
- {"version":3,"file":"MemoizeDescriptor.d.ts","sourceRoot":"","sources":["../../../../src/decorators/provides/MemoizeDescriptor.ts"],"names":[],"mappings":"AACA,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,kBAAkB,GAAG,kBAAkB,CAYzG"}
1
+ {"version":3,"file":"MemoizeDescriptor.d.ts","sourceRoot":"","sources":["../../../../src/decorators/provides/MemoizeDescriptor.ts"],"names":[],"mappings":"AACA,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,kBAAkB,GAAG,kBAAkB,CAWzG"}
@@ -9,12 +9,12 @@ function memoizeDescriptor(propertyKey, descriptor) {
9
9
  for (var _i = 0; _i < arguments.length; _i++) {
10
10
  args[_i] = arguments[_i];
11
11
  }
12
+ var memoizationTarget = Reflect.getMetadata('isSingleton', descriptor) ? descriptor : this;
12
13
  var key = "memoized".concat(propertyKey);
13
- if (Reflect.hasMetadata(key, this)) {
14
- return Reflect.getMetadata(key, this);
15
- }
14
+ if (Reflect.hasMetadata(key, memoizationTarget))
15
+ return Reflect.getMetadata(key, memoizationTarget);
16
16
  var result = originalValue.apply(this, args);
17
- Reflect.defineMetadata(key, result, this);
17
+ Reflect.defineMetadata(key, result, memoizationTarget);
18
18
  return result;
19
19
  };
20
20
  return descriptor;
@@ -1 +1 @@
1
- {"version":3,"file":"MemoizeDescriptor.js","sourceRoot":"","sources":["../../../../src/decorators/provides/MemoizeDescriptor.ts"],"names":[],"mappings":";;;AAAA,sCAAsC;AACtC,SAAgB,iBAAiB,CAAC,WAAmB,EAAE,UAA8B;IACnF,IAAM,aAAa,GAAG,UAAU,CAAC,KAAK,CAAC;IACvC,UAAU,CAAC,KAAK,GAAG,SAAS,KAAK;QAAC,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,yBAAc;;QAC9C,IAAM,GAAG,GAAG,kBAAW,WAAW,CAAE,CAAC;QACrC,IAAI,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE;YAClC,OAAO,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;SACvC;QACD,IAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC/C,OAAO,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QAC1C,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IACF,OAAO,UAAU,CAAC;AACpB,CAAC;AAZD,8CAYC"}
1
+ {"version":3,"file":"MemoizeDescriptor.js","sourceRoot":"","sources":["../../../../src/decorators/provides/MemoizeDescriptor.ts"],"names":[],"mappings":";;;AAAA,sCAAsC;AACtC,SAAgB,iBAAiB,CAAC,WAAmB,EAAE,UAA8B;IACnF,IAAM,aAAa,GAAG,UAAU,CAAC,KAAK,CAAC;IACvC,UAAU,CAAC,KAAK,GAAG,SAAS,KAAK;QAAC,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,yBAAc;;QAC9C,IAAM,iBAAiB,GAAG,OAAO,CAAC,WAAW,CAAC,aAAa,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC;QAC7F,IAAM,GAAG,GAAG,kBAAW,WAAW,CAAE,CAAC;QACrC,IAAI,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,iBAAiB,CAAC;YAAE,OAAO,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC;QACpG,IAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC/C,OAAO,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,iBAAiB,CAAC,CAAC;QACvD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IACF,OAAO,UAAU,CAAC;AACpB,CAAC;AAXD,8CAWC"}
@@ -1,7 +1,5 @@
1
- import { Scope } from '../../types';
2
1
  import { Graph } from '../../graph/Graph';
3
2
  interface ProvidesParams {
4
- scope?: Scope;
5
3
  name: string;
6
4
  }
7
5
  export declare function Provides({ name }?: Partial<ProvidesParams>): (graph: Graph, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
@@ -1 +1 @@
1
- {"version":3,"file":"Provides.d.ts","sourceRoot":"","sources":["../../../../src/decorators/provides/Provides.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAI1C,UAAU,cAAc;IACtB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wBAAgB,QAAQ,CAAC,EAAE,IAAI,EAAE,GAAE,OAAO,CAAC,cAAc,CAAM,WAC9B,KAAK,eAAe,MAAM,cAAc,kBAAkB,wBAI1F"}
1
+ {"version":3,"file":"Provides.d.ts","sourceRoot":"","sources":["../../../../src/decorators/provides/Provides.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAI1C,UAAU,cAAc;IACtB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wBAAgB,QAAQ,CAAC,EAAE,IAAI,EAAE,GAAE,OAAO,CAAC,cAAc,CAAM,WAC9B,KAAK,eAAe,MAAM,cAAc,kBAAkB,wBAI1F"}
@@ -1 +1 @@
1
- {"version":3,"file":"Provides.js","sourceRoot":"","sources":["../../../../src/decorators/provides/Provides.ts"],"names":[],"mappings":";;;;;;AAEA,0FAAoE;AACpE,yDAAwD;AAOxD,SAAgB,QAAQ,CAAC,EAAsC;QAAtC,qBAAoC,EAAE,KAAA,EAApC,IAAI,UAAA;IAC7B,OAAO,SAAS,OAAO,CAAC,KAAY,EAAE,WAAmB,EAAE,UAA8B;QACvF,iCAAuB,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,EAAE,IAAK,CAAC,CAAC;QACvD,OAAO,IAAA,qCAAiB,EAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACpD,CAAC,CAAC;AACJ,CAAC;AALD,4BAKC"}
1
+ {"version":3,"file":"Provides.js","sourceRoot":"","sources":["../../../../src/decorators/provides/Provides.ts"],"names":[],"mappings":";;;;;;AACA,0FAAoE;AACpE,yDAAwD;AAMxD,SAAgB,QAAQ,CAAC,EAAsC;QAAtC,qBAAoC,EAAE,KAAA,EAApC,IAAI,UAAA;IAC7B,OAAO,SAAS,OAAO,CAAC,KAAY,EAAE,WAAmB,EAAE,UAA8B;QACvF,iCAAuB,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,EAAE,IAAK,CAAC,CAAC;QACvD,OAAO,IAAA,qCAAiB,EAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACpD,CAAC,CAAC;AACJ,CAAC;AALD,4BAKC"}
@@ -1,8 +1,6 @@
1
- import { Scope } from '../types';
2
1
  import { Graph } from './Graph';
3
2
  export declare abstract class ObjectGraph<T = unknown> implements Graph {
4
3
  protected _props?: T | undefined;
5
- scope: Scope;
6
4
  private propertyRetriever;
7
5
  get name(): string;
8
6
  constructor(_props?: T | undefined);
@@ -1 +1 @@
1
- {"version":3,"file":"ObjectGraph.d.ts","sourceRoot":"","sources":["../../../src/graph/ObjectGraph.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAGhC,8BAAsB,WAAW,CAAC,CAAC,GAAG,OAAO,CAAE,YAAW,KAAK;IAUjD,SAAS,CAAC,MAAM,CAAC;IARtB,KAAK,EAAG,KAAK,CAAC;IACrB,OAAO,CAAC,iBAAiB,CAA+B;IAExD,IACI,IAAI,IAAI,MAAM,CAEjB;gBAEqB,MAAM,CAAC,eAAG;IAIhC,QAAQ,CAAC,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS;CAGnF"}
1
+ {"version":3,"file":"ObjectGraph.d.ts","sourceRoot":"","sources":["../../../src/graph/ObjectGraph.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAGhC,8BAAsB,WAAW,CAAC,CAAC,GAAG,OAAO,CAAE,YAAW,KAAK;IAQjD,SAAS,CAAC,MAAM,CAAC;IAP7B,OAAO,CAAC,iBAAiB,CAA+B;IAExD,IACI,IAAI,IAAI,MAAM,CAEjB;gBAEqB,MAAM,CAAC,eAAG;IAIhC,QAAQ,CAAC,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS;CAGnF"}
@@ -1 +1 @@
1
- {"version":3,"file":"ObjectGraph.js","sourceRoot":"","sources":["../../../src/graph/ObjectGraph.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,iCAAkC;AAClC,kEAA4C;AAE5C,mDAAiD;AAEjD,0EAAoD;AAEpD;IAUE,qBAAsB,MAAU;QAAV,WAAM,GAAN,MAAM,CAAI;QAPxB,sBAAiB,GAAG,IAAI,2BAAiB,CAAC,IAAI,CAAC,CAAC;QAQtD,IAAA,8BAAa,EAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAND,sBAAI,6BAAI;aAAR;YACE,OAAO,IAAA,iBAAQ,EAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACzC,CAAC;;;OAAA;IAMD,8BAAQ,GAAR,UAAqB,QAAgB,EAAE,QAAkB;QACvD,OAAO,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAA2B,CAAC;IACvF,CAAC;IAVD;QADC,IAAA,iBAAO,GAAE;;;2CAGT;IASH,kBAAC;CAAA,AAjBD,IAiBC;AAjBqB,kCAAW"}
1
+ {"version":3,"file":"ObjectGraph.js","sourceRoot":"","sources":["../../../src/graph/ObjectGraph.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,iCAAkC;AAClC,kEAA4C;AAC5C,mDAAiD;AAEjD,0EAAoD;AAEpD;IAQE,qBAAsB,MAAU;QAAV,WAAM,GAAN,MAAM,CAAI;QAPxB,sBAAiB,GAAG,IAAI,2BAAiB,CAAC,IAAI,CAAC,CAAC;QAQtD,IAAA,8BAAa,EAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAND,sBAAI,6BAAI;aAAR;YACE,OAAO,IAAA,iBAAQ,EAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACzC,CAAC;;;OAAA;IAMD,8BAAQ,GAAR,UAAqB,QAAgB,EAAE,QAAkB;QACvD,OAAO,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAA2B,CAAC;IACvF,CAAC;IAVD;QADC,IAAA,iBAAO,GAAE;;;2CAGT;IASH,kBAAC;CAAA,AAfD,IAeC;AAfqB,kCAAW"}
@@ -1,18 +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
- declare class GraphRegistry {
5
- private readonly scopedGraphs;
4
+ export declare class GraphRegistry {
6
5
  private readonly constructorToInstance;
7
6
  private readonly instanceToConstructor;
8
7
  private readonly graphToSubgraphs;
9
- private graphMiddlewares;
10
- register(constructor: Constructable<Graph>, scope?: Scope | undefined, subgraphs?: Constructable<Graph>[]): void;
11
- has(Graph: Constructable<Graph>): boolean;
12
- get<T extends Graph>(Graph: Constructable<T>): T;
13
- set(Graph: Constructable<Graph>, graph: Graph): void;
8
+ private readonly graphMiddlewares;
9
+ register(constructor: Constructable<Graph>, subgraphs?: Constructable<Graph>[]): void;
14
10
  getSubgraphs(graph: Graph): Graph[];
15
11
  resolve<T extends Graph>(Graph: Constructable<T>, props?: any): T;
12
+ private has;
13
+ private getFirst;
14
+ private set;
15
+ private isSingleton;
16
16
  clear(graph: Graph): void;
17
17
  addGraphMiddleware(middleware: Middleware<Graph>): void;
18
18
  clearGraphMiddlewares(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"GraphRegistry.d.ts","sourceRoot":"","sources":["../../../../src/graph/registry/GraphRegistry.ts"],"names":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AO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG1C,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAA+C;IACrF,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAA0C;IAChF,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA8D;IAC/F,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA8B;IAE/D,QAAQ,CAAC,WAAW,EAAE,aAAa,CAAC,KAAK,CAAC,EAAE,SAAS,GAAE,aAAa,CAAC,KAAK,CAAC,EAAO;IAIlF,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,EAAE;IAMnC,OAAO,CAAC,CAAC,SAAS,KAAK,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,GAAG,GAAG,CAAC;IASjE,OAAO,CAAC,GAAG;IAIX,OAAO,CAAC,QAAQ;IAIhB,OAAO,CAAC,GAAG;IAOX,OAAO,CAAC,WAAW;IAInB,KAAK,CAAC,KAAK,EAAE,KAAK;IAMlB,kBAAkB,CAAC,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC;IAIhD,qBAAqB;CAGtB;;AAED,wBAAmC"}
@@ -3,32 +3,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.GraphRegistry = void 0;
6
7
  var GraphMiddlewareChain_1 = __importDefault(require("./GraphMiddlewareChain"));
7
8
  var GraphRegistry = /** @class */ (function () {
8
9
  function GraphRegistry() {
9
- this.scopedGraphs = {};
10
10
  this.constructorToInstance = new Map();
11
11
  this.instanceToConstructor = new Map();
12
12
  this.graphToSubgraphs = new Map();
13
13
  this.graphMiddlewares = new GraphMiddlewareChain_1.default();
14
14
  }
15
- GraphRegistry.prototype.register = function (constructor, scope, subgraphs) {
16
- if (scope === void 0) { scope = undefined; }
15
+ GraphRegistry.prototype.register = function (constructor, subgraphs) {
17
16
  if (subgraphs === void 0) { subgraphs = []; }
18
- if (scope)
19
- this.scopedGraphs[scope] = constructor;
20
17
  this.graphToSubgraphs.set(constructor, new Set(subgraphs));
21
18
  };
22
- GraphRegistry.prototype.has = function (Graph) {
23
- return this.constructorToInstance.has(Graph);
24
- };
25
- GraphRegistry.prototype.get = function (Graph) {
26
- return this.constructorToInstance.get(Graph);
27
- };
28
- GraphRegistry.prototype.set = function (Graph, graph) {
29
- this.constructorToInstance.set(Graph, graph);
30
- this.instanceToConstructor.set(graph, Graph);
31
- };
32
19
  GraphRegistry.prototype.getSubgraphs = function (graph) {
33
20
  var _this = this;
34
21
  var _a;
@@ -37,21 +24,34 @@ var GraphRegistry = /** @class */ (function () {
37
24
  return Array.from(subgraphs).map(function (G) { return _this.resolve(G); });
38
25
  };
39
26
  GraphRegistry.prototype.resolve = function (Graph, props) {
40
- if (this.has(Graph)) {
41
- return this.get(Graph);
42
- // const graph: T = this.get(Graph);
43
- // const scope = Reflect.getMetadata('scope', Graph);
44
- // if (scope) return graph;
45
- // this.set(Graph, new Graph(props));
27
+ if (this.isSingleton(Graph) && this.has(Graph)) {
28
+ return this.getFirst(Graph);
46
29
  }
47
30
  var graph = this.graphMiddlewares.resolve(Graph, props);
48
31
  this.set(Graph, graph);
49
32
  return graph;
50
33
  };
34
+ GraphRegistry.prototype.has = function (Graph) {
35
+ return this.constructorToInstance.has(Graph);
36
+ };
37
+ GraphRegistry.prototype.getFirst = function (Graph) {
38
+ return this.constructorToInstance.get(Graph).values().next().value;
39
+ };
40
+ GraphRegistry.prototype.set = function (Graph, graph) {
41
+ var _a;
42
+ var graphs = (_a = this.constructorToInstance.get(Graph)) !== null && _a !== void 0 ? _a : new Set();
43
+ graphs.add(graph);
44
+ this.constructorToInstance.set(Graph, graphs);
45
+ this.instanceToConstructor.set(graph, Graph);
46
+ };
47
+ GraphRegistry.prototype.isSingleton = function (Graph) {
48
+ var _a;
49
+ return (_a = Reflect.getMetadata('isSingleton', Graph)) !== null && _a !== void 0 ? _a : false;
50
+ };
51
51
  GraphRegistry.prototype.clear = function (graph) {
52
52
  var Graph = this.instanceToConstructor.get(graph);
53
53
  this.instanceToConstructor.delete(graph);
54
- this.constructorToInstance.delete(Graph);
54
+ this.constructorToInstance.get(Graph).delete(graph);
55
55
  };
56
56
  GraphRegistry.prototype.addGraphMiddleware = function (middleware) {
57
57
  this.graphMiddlewares.add(middleware);
@@ -61,5 +61,6 @@ var GraphRegistry = /** @class */ (function () {
61
61
  };
62
62
  return GraphRegistry;
63
63
  }());
64
+ exports.GraphRegistry = GraphRegistry;
64
65
  exports.default = new GraphRegistry();
65
66
  //# sourceMappingURL=GraphRegistry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"GraphRegistry.js","sourceRoot":"","sources":["../../../../src/graph/registry/GraphRegistry.ts"],"names":[],"mappings":";;;;;AAGA,gFAA0D;AAE1D;IAAA;QACmB,iBAAY,GAAwC,EAAE,CAAC;QACvD,0BAAqB,GAAG,IAAI,GAAG,EAA+B,CAAC;QAC/D,0BAAqB,GAAG,IAAI,GAAG,EAA+B,CAAC;QAC/D,qBAAgB,GAAG,IAAI,GAAG,EAAmD,CAAC;QACvF,qBAAgB,GAAG,IAAI,8BAAoB,EAAE,CAAC;IAyDxD,CAAC;IAvDC,gCAAQ,GAAR,UACE,WAAiC,EACjC,KAAoC,EACpC,SAAsC;QADtC,sBAAA,EAAA,iBAAoC;QACpC,0BAAA,EAAA,cAAsC;QAEtC,IAAI,KAAK;YAAE,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,GAAG,WAAW,CAAC;QAClD,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,WAAW,EAAE,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;IAC7D,CAAC;IAED,2BAAG,GAAH,UAAI,KAA2B;QAC7B,OAAO,IAAI,C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,KAA2B;;QAC7C,OAAO,MAAA,OAAO,CAAC,WAAW,CAAC,aAAa,EAAE,KAAK,CAAC,mCAAI,KAAK,CAAC;IAC5D,CAAC;IAED,6BAAK,GAAL,UAAM,KAAY;QAChB,IAAM,KAAK,GAAG,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC;QACrD,IAAI,CAAC,qBAAqB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACzC,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvD,CAAC;IAED,0CAAkB,GAAlB,UAAmB,UAA6B;QAC9C,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;IAED,6CAAqB,GAArB;QACE,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;IAChC,CAAC;IACH,oBAAC;AAAD,CAAC,AAzDD,IAyDC;AAzDY,sCAAa;AA2D1B,kBAAe,IAAI,aAAa,EAAE,CAAC"}
@@ -4,6 +4,7 @@ import _Obsidian from './Obsidian';
4
4
  import { Constructable } from './types';
5
5
  export * from './types';
6
6
  export { Graph } from './decorators/Graph';
7
+ export { Singleton } from './decorators/Singleton';
7
8
  export { ObjectGraph } from './graph/ObjectGraph';
8
9
  export { Graph as IGraph } from './graph/Graph';
9
10
  export { Provides } from './decorators/provides/Provides';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,WAAW,IAAI,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAElE,OAAO,SAAS,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,cAAc,SAAS,CAAC;AAExB,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,C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,kCAAkC,CAAC;AACnE,OAAO,EAAE,iBAAiB,IAAI,YAAY,EAAE,MAAM,oCAAoC,CAAC;AACvF,eAAO,MAAM,QAAQ,WAAkB,CAAC;AAGxC,eAAO,MAAM,eAAe,iDAEnB,cAAc,YAAY,CAAC,wCACQ,CAAC;AAG7C,eAAO,MAAM,UAAU,sDAEd,cAAc,YAAY,CAAC,iDACC,CAAC"}
package/dist/src/index.js CHANGED
@@ -13,13 +13,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
14
  };
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.injectHook = exports.injectComponent = exports.Obsidian = exports.GraphMiddleware = exports.Inject = exports.Injectable = exports.Provides = exports.ObjectGraph = exports.Graph = void 0;
16
+ exports.injectHook = exports.injectComponent = exports.Obsidian = exports.GraphMiddleware = exports.Inject = exports.Injectable = exports.Provides = exports.ObjectGraph = exports.Singleton = exports.Graph = void 0;
17
17
  var ComponentInjector_1 = __importDefault(require("./injectors/components/ComponentInjector"));
18
18
  var Obsidian_1 = __importDefault(require("./Obsidian"));
19
19
  var HookInjector_1 = __importDefault(require("./injectors/hooks/HookInjector"));
20
20
  __exportStar(require("./types"), exports);
21
21
  var Graph_1 = require("./decorators/Graph");
22
22
  Object.defineProperty(exports, "Graph", { enumerable: true, get: function () { return Graph_1.Graph; } });
23
+ var Singleton_1 = require("./decorators/Singleton");
24
+ Object.defineProperty(exports, "Singleton", { enumerable: true, get: function () { return Singleton_1.Singleton; } });
23
25
  var ObjectGraph_1 = require("./graph/ObjectGraph");
24
26
  Object.defineProperty(exports, "ObjectGraph", { enumerable: true, get: function () { return ObjectGraph_1.ObjectGraph; } });
25
27
  var Provides_1 = require("./decorators/provides/Provides");
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA,+FAAyE;AACzE,wDAAmC;AAC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ExC,IAAM,iBAAiB,GAAG,IAAI,2BAAiB,EAAE,CAAC;AAC3C,IAAM,eAAe,GAAG,UAC7B,MAAkC,EAClC,KAAkC,IAC/B,OAAA,iBAAiB,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAAvC,CAAuC,CAAC;AAHhC,QAAA,eAAe,mBAGiB;AAE7C,IAAM,YAAY,GAAG,IAAI,sBAAY,EAAE,CAAC;AACjC,IAAM,UAAU,GAAG,UACxB,IAA4B,EAC5B,KAAkC,IAC/B,OAAA,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,EAAhC,CAAgC,CAAC;AAHzB,QAAA,UAAU,cAGe"}
@@ -1,7 +1,6 @@
1
1
  export declare type Constructable<T> = {
2
2
  new (...args: any): T;
3
3
  };
4
- export declare type Scope = string | number | symbol;
5
4
  export declare type Constructor = {
6
5
  new (...args: any[]): any;
7
6
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa,CAAC,CAAC,IAAI;IAC7B,KAAI,GAAG,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC;CACtB,CAAC;AAEF,oBAAY,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAE7C,oBAAY,WAAW,GAAG;IAAE,KAAI,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;CAAE,CAAC;AAEvD,oBAAY,cAAc,CAAC,KAAK,IAAI;KACjC,GAAG,IAAI,MAAM,KAAK,GAAG,QAAQ;CAC/B,CAAC;AAEF,oBAAY,cAAc,GAAG,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa,CAAC,CAAC,IAAI;IAC7B,KAAI,GAAG,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC;CACtB,CAAC;AAEF,oBAAY,WAAW,GAAG;IAAE,KAAI,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;CAAE,CAAC;AAEvD,oBAAY,cAAc,CAAC,KAAK,IAAI;KACjC,GAAG,IAAI,MAAM,KAAK,GAAG,QAAQ;CAC/B,CAAC;AAEF,oBAAY,cAAc,GAAG,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { ObjectGraph } from '../../../src';
2
+ export default class SingletonGraph extends ObjectGraph {
3
+ private id;
4
+ instanceId(): string;
5
+ }
6
+ //# sourceMappingURL=SingletonGraph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SingletonGraph.d.ts","sourceRoot":"","sources":["../../../../test/integration/fixtures/SingletonGraph.ts"],"names":[],"mappings":"AAEA,OAAO,EAAS,WAAW,EAAY,MAAM,cAAc,CAAC;AAG5D,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,WAAW;IACrD,OAAO,CAAC,EAAE,CAAc;IAGxB,UAAU,IAAI,MAAM;CAGrB"}
@@ -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"}
@@ -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.12",
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": {
@@ -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
+ }
@@ -2,12 +2,11 @@
2
2
  export function memoizeDescriptor(propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor {
3
3
  const originalValue = descriptor.value;
4
4
  descriptor.value = function value(...args: any[]) {
5
+ const memoizationTarget = Reflect.getMetadata('isSingleton', descriptor) ? descriptor : this;
5
6
  const key = `memoized${propertyKey}`;
6
- if (Reflect.hasMetadata(key, this)) {
7
- return Reflect.getMetadata(key, this);
8
- }
7
+ if (Reflect.hasMetadata(key, memoizationTarget)) return Reflect.getMetadata(key, memoizationTarget);
9
8
  const result = originalValue.apply(this, args);
10
- Reflect.defineMetadata(key, result, this);
9
+ Reflect.defineMetadata(key, result, memoizationTarget);
11
10
  return result;
12
11
  };
13
12
  return descriptor;
@@ -1,10 +1,8 @@
1
- import { Scope } from '../../types';
2
1
  import { Graph } from '../../graph/Graph';
3
2
  import providedPropertiesStore from '../../ProvidedPropertiesStore';
4
3
  import { memoizeDescriptor } from './MemoizeDescriptor';
5
4
 
6
5
  interface ProvidesParams {
7
- scope?: Scope;
8
6
  name: string;
9
7
  }
10
8
 
@@ -1,13 +1,10 @@
1
1
  import { uniqueId } from 'lodash';
2
2
  import Memoize from '../decorators/Memoize';
3
- import { Scope } from '../types';
4
3
  import { bindProviders } from './ProviderBinder';
5
4
  import { Graph } from './Graph';
6
5
  import PropertyRetriever from './PropertyRetriever';
7
6
 
8
7
  export abstract class ObjectGraph<T = unknown> implements Graph {
9
- // TODO? rename scope to singleInstance
10
- public scope!: Scope;
11
8
  private propertyRetriever = new PropertyRetriever(this);
12
9
 
13
10
  @Memoize()
@@ -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,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();