react-obsidian 0.0.18 → 0.0.21

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 CHANGED
@@ -1,3 +1,5 @@
1
+ [![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://vshymanskyy.github.io/StandWithUkraine)
2
+
1
3
  # react-obsidian
2
4
  > Dependency injection framework for React and React Native applications
3
5
 
@@ -5,11 +7,10 @@
5
7
  <br>⚠️ <b>Until we hit v1, Obsidian is not semver-compliant and all APIs are subject to change.</b></h5>
6
8
 
7
9
  ## Introduction
8
- Applications are made of objects that communicate with each other. An object can depend on other objects so it can perform its responsibilities. For an object to function properly, its dependencies must be fulfilled when the object is constructed.
9
10
 
10
11
  React Obsidian is a dependency injection framework for React and React Native applications. It allows you to inject dependencies effortlessly into hooks, components or classes. Separating the construction and consumption of dependencies is crucial to maintaining a readable and testable codebase.
11
12
 
12
- React Obsidian is guided by the principles of the Dependency Injection pattern, but does not strictly follow it. We allowed ourselves a degree of freedom when designing the library in order to reduce boilerplate code and reduce library footprint.
13
+ React Obsidian is guided by the principles of the Dependency Injection pattern, but does not strictly follow them. We allowed ourselves a degree of freedom when designing the library in order to reduce boilerplate code and library footprint.
13
14
 
14
15
  * [Installation](https://github.com/wix-incubator/react-obsidian#installation)
15
16
  * [Prerequisites](https://github.com/wix-incubator/react-obsidian#prerequisites)
@@ -24,7 +25,8 @@ React Obsidian is guided by the principles of the Dependency Injection pattern,
24
25
  * [Advance usage](https://github.com/wix-incubator/react-obsidian#advance-usage)
25
26
  * [Accessing props in graphs](https://github.com/wix-incubator/react-obsidian#accessing-props-in-graphs)
26
27
  * [Singleton graphs and providers](https://github.com/wix-incubator/react-obsidian#singleton-graphs-and-providers)
27
- * [Graph middlewares](https://github.com/wix-incubator/react-obsidian#graph-middlewares)
28
+ * [Graph middleware](https://github.com/wix-incubator/react-obsidian#graph-middleware)
29
+ * [Clear graphs](https://github.com/wix-incubator/react-obsidian#clear-graphs)
28
30
 
29
31
 
30
32
 
@@ -49,7 +51,7 @@ In the `ApplicationGraph` example below, we declare two dependencies:
49
51
 
50
52
  Both functions are annotated by the `@Provides()` annotation. This signals Obsidian that the results of these functions are provided by the graph and can be injected.
51
53
 
52
- 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.
54
+ Notice how the biLogger function receives an `httpClient` as an argument. This means that `biLogger` is dependent on `httpClient`. Obsidian will create an `httpClient` when `biLogger` is injected.
53
55
 
54
56
  ``` typescript
55
57
  @Singleton() @Graph()
@@ -106,7 +108,7 @@ const useButtonClick = ({ biLogger }: UseButtonPressProps): UseButtonPress => {
106
108
  // Dependencies are injected from ApplicationGraph
107
109
  export default injectHook(usePress, ApplicationGraph);
108
110
 
109
- // Now that exported the injected hook, we can use it in a component without needed so provide it's dependencies manually
111
+ // Now that we exported the injected hook, we can use it in a component without the needed so provide its dependencies manually
110
112
  const Component = () => (
111
113
  // No need to specify dependencies as they are injected automatically
112
114
  const { onClick } = useButtonClick();
@@ -139,7 +141,7 @@ class Presenter {
139
141
  }
140
142
  ```
141
143
 
142
- TypeScript's compiler won't let you construct the class without providing the argument `biLogger` as it's not optional.
144
+ The TypeScript compiler won't let you construct the class without providing the `biLogger` argument as it's not optional.
143
145
  If you want to be able to instantiate the class yourself without providing arguments, you'll also need to declare a constructor overload that receives optional arguments.
144
146
 
145
147
  ```typescript
@@ -160,11 +162,11 @@ Dependencies can also be obtained by accessing the graph that provides them.
160
162
  Obsidian.obtain(ApplicationGraph).biLogger();
161
163
  ```
162
164
 
163
- > 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.
165
+ > Note: While the function that provides the `biLogger` accepts an argument of type `HttpClient`, we don't provide dependencies ourselves when obtaining dependencies directly from the graph, as they are resolved by Obsidian.
164
166
 
165
- ## Advance usage
167
+ ## Advanced usage
166
168
  ### Accessing props in graphs
167
- If a graph is instantiated in order to inject a component, then it receives the component's props in the constructor.
169
+ If a graph is instantiated in order to inject a component, then it will receive the component's props in the constructor.
168
170
  ```typescript
169
171
  @Graph()
170
172
  class ProfileScreenGraph extends ObjectGraph<ProfileScreenProps> {
@@ -181,8 +183,9 @@ class ProfileScreenGraph extends ObjectGraph<ProfileScreenProps> {
181
183
  }
182
184
  }
183
185
  ```
186
+
184
187
  ### Singleton graphs and providers
185
- 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.
188
+ Graphs and Providers can be marked as singletons with the `@Singleton` decorator. If a graph is marked as a singleton, when an instance of such 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.
186
189
 
187
190
  Singleton providers are shared between all instances of a graph.
188
191
 
@@ -211,16 +214,17 @@ class ApplicationGraph {
211
214
  }
212
215
  }
213
216
  ```
214
- ### Graph middlewares
215
- 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.
216
217
 
217
- Middlewares are invoked in LIFO order and can be used for various purposes:
218
- 1. Create a graph yourself instead of letting Obsidian instantiate it.
218
+ ### Graph middleware
219
+ When working on large scale applications, we often need to to hook into various low level operations. Obsidian lets you hook into the graph creation process by adding middleware(s).
220
+
221
+ Those middleware are invoked in LIFO order and can be used for various purposes:
222
+ 1. Create a graph yourself instead of letting Obsidian to instantiate it.
219
223
  2. Add logging to graph creation.
220
224
  3. Handle errors when Obsidian instantiates graphs.
221
225
  4. Replace graphs with mocks for testing purposes.
222
226
 
223
- Middlewares follow the Chain of Responsibility pattern and therefor must always return a graph, either by creating one explicitly or by returning the instance created by another member in the resolve chain.
227
+ Middleware follow the Chain of Responsibility pattern and therefore must always return a graph, either by creating one explicitly or by returning the instance created by another member in the resolve chain.
224
228
 
225
229
  #### Adding a logging middleware
226
230
  The following example demonstrates how to add a middleware that's used for logging purposes.
@@ -237,6 +241,16 @@ const loggingMiddleware = new class extends GraphMiddleware {
237
241
  }();
238
242
  Obsidian.addGraphMiddleware(loggingMiddleware);
239
243
  ```
244
+
245
+ ### Clear graphs
246
+ Graphs can be cleared by invoking `Obsidian.clearGraphs()`. This is useful in tests or when you need to reset the system to it's original state, for example when a user logs out.
247
+
248
+ #### Clearing graphs automatically during execution of Jest tests
249
+ Create a `jest.setup.js` file and add it to [setupFilesAfterEnv](https://jestjs.io/docs/configuration#setupfilesafterenv-array). Then, import the following file when ensures graphs are cleared before each test.
250
+ ```javascript
251
+ import 'react-obsidian/clearGraphs';
252
+ ```
253
+
240
254
  ## Prerequisites
241
255
  Obsidian is highly opinionated and is developed with a specific environment in mind. Therefore, it has a few prerequisites for projects that want to integrate it.
242
256
 
@@ -263,7 +277,7 @@ Add the following options to your `tsconfig.json` file.
263
277
  ```
264
278
 
265
279
  ### Babel
266
- Obsidian relies on reflection to resolve dependencies. Production code is typically mangled to reduce bundle size. This means that some names Obsidian expects are changed during the mangling process. To workaround this, Obsidian persists the names of methods annotated with the `@Provides` decorator with a Babel transformer.
280
+ Obsidian relies on reflection to resolve dependencies. Production code is typically mangled to reduce bundle size. This means that some names Obsidian expects are changed during the mangling process. To work around this, Obsidian persists the names of methods annotated with the `@Provides` decorator with a Babel transformer.
267
281
 
268
282
  ### Add Obsidian's babel transformer
269
283
  Add the transformer to the list of plugins in your `.babel` file.
package/clearGraphs.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { Obsidian } from './src';
2
+
3
+ beforeEach(() => {
4
+ Obsidian.clearGraphs();
5
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=clearGraphs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clearGraphs.d.ts","sourceRoot":"","sources":["../clearGraphs.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var src_1 = require("./src");
4
+ beforeEach(function () {
5
+ src_1.Obsidian.clearGraphs();
6
+ });
7
+ //# sourceMappingURL=clearGraphs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clearGraphs.js","sourceRoot":"","sources":["../clearGraphs.ts"],"names":[],"mappings":";;AAAA,6BAAiC;AAEjC,UAAU,CAAC;IACT,cAAQ,CAAC,WAAW,EAAE,CAAC;AACzB,CAAC,CAAC,CAAC"}
@@ -6,5 +6,6 @@ export default class Obsidian {
6
6
  inject<T extends object>(target: T, graph?: ObjectGraph): object;
7
7
  addGraphMiddleware(middleware: GraphMiddleware): void;
8
8
  clearGraphMiddlewares(): void;
9
+ clearGraphs(): void;
9
10
  }
10
11
  //# sourceMappingURL=Obsidian.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Obsidian.d.ts","sourceRoot":"","sources":["../../src/Obsidian.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAGnE,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC3B,MAAM,CAAC,CAAC,SAAS,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,EACtC,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EACvB,KAAK,CAAC,EAAE,CAAC,GACR,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC;IAI1C,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,WAAW;IAIvD,kBAAkB,CAAC,UAAU,EAAE,eAAe;IAI9C,qBAAqB;CAGtB"}
1
+ {"version":3,"file":"Obsidian.d.ts","sourceRoot":"","sources":["../../src/Obsidian.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAInE,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC3B,MAAM,CAAC,CAAC,SAAS,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,EACtC,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EACvB,KAAK,CAAC,EAAE,CAAC,GACR,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC;IAI1C,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,WAAW;IAIvD,kBAAkB,CAAC,UAAU,EAAE,eAAe;IAI9C,qBAAqB;IAIrB,WAAW;CAIZ"}
@@ -5,11 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var GraphRegistry_1 = __importDefault(require("./graph/registry/GraphRegistry"));
7
7
  var LazyInjector_1 = __importDefault(require("./injectors/class/LazyInjector"));
8
+ var ServiceLocatorFactory_1 = __importDefault(require("./graph/ServiceLocatorFactory"));
8
9
  var Obsidian = /** @class */ (function () {
9
10
  function Obsidian() {
10
11
  }
11
12
  Obsidian.prototype.obtain = function (Graph, props) {
12
- return GraphRegistry_1.default.resolve(Graph, props);
13
+ return ServiceLocatorFactory_1.default.fromGraph(Graph, props);
13
14
  };
14
15
  Obsidian.prototype.inject = function (target, graph) {
15
16
  return LazyInjector_1.default.inject(target, graph);
@@ -20,6 +21,10 @@ var Obsidian = /** @class */ (function () {
20
21
  Obsidian.prototype.clearGraphMiddlewares = function () {
21
22
  GraphRegistry_1.default.clearGraphMiddlewares();
22
23
  };
24
+ Obsidian.prototype.clearGraphs = function () {
25
+ GraphRegistry_1.default.clearGraphMiddlewares();
26
+ GraphRegistry_1.default.clearAll();
27
+ };
23
28
  return Obsidian;
24
29
  }());
25
30
  exports.default = Obsidian;
@@ -1 +1 @@
1
- {"version":3,"file":"Obsidian.js","sourceRoot":"","sources":["../../src/Obsidian.ts"],"names":[],"mappings":";;;;;AAAA,iFAA2D;AAI3D,gFAA0D;AAE1D;IAAA;IAmBA,CAAC;IAlBC,yBAAM,GAAN,UACE,KAAuB,EACvB,KAAS;QAET,OAAO,uBAAa,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAiC,CAAC;IAC7E,CAAC;IAED,yBAAM,GAAN,UAAyB,MAAS,EAAE,KAAmB;QACrD,OAAO,sBAAY,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC;IAED,qCAAkB,GAAlB,UAAmB,UAA2B;QAC5C,uBAAa,CAAC,kBAAkB,CAAC,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED,wCAAqB,GAArB;QACE,uBAAa,CAAC,qBAAqB,EAAE,CAAC;IACxC,CAAC;IACH,eAAC;AAAD,CAAC,AAnBD,IAmBC"}
1
+ {"version":3,"file":"Obsidian.js","sourceRoot":"","sources":["../../src/Obsidian.ts"],"names":[],"mappings":";;;;;AAAA,iFAA2D;AAI3D,gFAA0D;AAC1D,wFAAkE;AAElE;IAAA;IAwBA,CAAC;IAvBC,yBAAM,GAAN,UACE,KAAuB,EACvB,KAAS;QAET,OAAO,+BAAqB,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACvD,CAAC;IAED,yBAAM,GAAN,UAAyB,MAAS,EAAE,KAAmB;QACrD,OAAO,sBAAY,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC;IAED,qCAAkB,GAAlB,UAAmB,UAA2B;QAC5C,uBAAa,CAAC,kBAAkB,CAAC,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED,wCAAqB,GAArB;QACE,uBAAa,CAAC,qBAAqB,EAAE,CAAC;IACxC,CAAC;IAED,8BAAW,GAAX;QACE,uBAAa,CAAC,qBAAqB,EAAE,CAAC;QACtC,uBAAa,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;IACH,eAAC;AAAD,CAAC,AAxBD,IAwBC"}
@@ -0,0 +1,6 @@
1
+ import { ObjectGraph } from './ObjectGraph';
2
+ import { Constructable, ServiceLocator as ServiceLocatorType } from '../types';
3
+ export default class ServiceLocatorFactory {
4
+ static fromGraph<T extends ObjectGraph<P>, P = any>(Graph: Constructable<T>, props?: P): ServiceLocatorType<T>;
5
+ }
6
+ //# sourceMappingURL=ServiceLocatorFactory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ServiceLocatorFactory.d.ts","sourceRoot":"","sources":["../../../src/graph/ServiceLocatorFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,cAAc,IAAI,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAG/E,MAAM,CAAC,OAAO,OAAO,qBAAqB;IACxC,MAAM,CAAC,SAAS,CAAC,CAAC,SAAS,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC;CASvF"}
@@ -0,0 +1,22 @@
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 GraphRegistry_1 = __importDefault(require("./registry/GraphRegistry"));
7
+ var ServiceLocatorFactory = /** @class */ (function () {
8
+ function ServiceLocatorFactory() {
9
+ }
10
+ ServiceLocatorFactory.fromGraph = function (Graph, props) {
11
+ var resolved = GraphRegistry_1.default.resolve(Graph, props);
12
+ var wrapped = new Proxy(resolved, {
13
+ get: function (_target, property, receiver) {
14
+ return function () { return resolved.retrieve(property, receiver); };
15
+ },
16
+ });
17
+ return wrapped;
18
+ };
19
+ return ServiceLocatorFactory;
20
+ }());
21
+ exports.default = ServiceLocatorFactory;
22
+ //# sourceMappingURL=ServiceLocatorFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ServiceLocatorFactory.js","sourceRoot":"","sources":["../../../src/graph/ServiceLocatorFactory.ts"],"names":[],"mappings":";;;;;AAEA,2EAAqD;AAErD;IAAA;IAUA,CAAC;IATQ,+BAAS,GAAhB,UAAoD,KAAuB,EAAE,KAAS;QACpF,IAAM,QAAQ,GAAG,uBAAa,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACrD,IAAM,OAAO,GAAG,IAAI,KAAK,CAAC,QAAQ,EAAE;YAClC,GAAG,EAAH,UAAI,OAAY,EAAE,QAAgB,EAAE,QAAa;gBAC/C,OAAO,cAAM,OAAA,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,EAArC,CAAqC,CAAC;YACrD,CAAC;SACF,CAAC,CAAC;QACH,OAAO,OAA2C,CAAC;IACrD,CAAC;IACH,4BAAC;AAAD,CAAC,AAVD,IAUC"}
@@ -18,7 +18,7 @@ export declare class GraphRegistry {
18
18
  clear(graph: Graph): void;
19
19
  addGraphMiddleware(middleware: Middleware<Graph>): void;
20
20
  clearGraphMiddlewares(): void;
21
- reset(): void;
21
+ clearAll(): void;
22
22
  }
23
23
  declare const _default: GraphRegistry;
24
24
  export default _default;
@@ -1 +1 @@
1
- {"version":3,"file":"GraphRegistry.d.ts","sourceRoot":"","sources":["../../../../src/graph/registry/GraphRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,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AAO,CAAC,QAAQ,CAAC,cAAc,CAA4B;IAC3D,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,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK;IAIrC,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;IAQX,OAAO,CAAC,WAAW;IAInB,KAAK,CAAC,KAAK,EAAE,KAAK;IAOlB,kBAAkB,CAAC,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC;IAIhD,qBAAqB;IAIrB,QAAQ;CAKT;;AAED,wBAAmC"}
@@ -65,7 +65,7 @@ var GraphRegistry = /** @class */ (function () {
65
65
  GraphRegistry.prototype.clearGraphMiddlewares = function () {
66
66
  this.graphMiddlewares.clear();
67
67
  };
68
- GraphRegistry.prototype.reset = function () {
68
+ GraphRegistry.prototype.clearAll = function () {
69
69
  this.instanceToConstructor.clear();
70
70
  this.constructorToInstance.clear();
71
71
  this.nameToInstance.clear();
@@ -1 +1 @@
1
- {"version":3,"file":"GraphRegistry.js","sourceRoot":"","sources":["../../../../src/graph/registry/GraphRegistry.ts"],"names":[],"mappings":";;;;;;AAGA,gFAA0D;AAE1D;IAAA;QACmB,0BAAqB,GAAG,IAAI,GAAG,EAAoC,CAAC;QACpE,0BAAqB,GAAG,IAAI,GAAG,EAA+B,CAAC;QAC/D,mBAAc,GAAG,IAAI,GAAG,EAAiB,CAAC;QAC1C,q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CAAC;AAEhE,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAQ7C,cAAM,OAAO;IACJ,UAAU,CAAC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,GAAG,KAAK,WAAW,CAAC,CAAC;CAY/G;AAED,eAAO,MAAM,OAAO,SAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../testkit/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAW,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAEhE,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAG7C,cAAM,OAAO;IACJ,UAAU,CAAC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,GAAG,KAAK,WAAW,CAAC,CAAC;CAY/G;AAED,eAAO,MAAM,OAAO,SAAgB,CAAC"}
@@ -22,10 +22,6 @@ exports.testKit = void 0;
22
22
  var ObjectGraph_1 = require("../src/graph/ObjectGraph");
23
23
  var GraphMiddleware_1 = require("../src/graph/registry/GraphMiddleware");
24
24
  var GraphRegistry_1 = __importDefault(require("../src/graph/registry/GraphRegistry"));
25
- beforeEach(function () {
26
- GraphRegistry_1.default.clearGraphMiddlewares();
27
- GraphRegistry_1.default.reset();
28
- });
29
25
  var TestKit = /** @class */ (function () {
30
26
  function TestKit() {
31
27
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../testkit/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,wDAAgE;AAChE,yEAAwE;AAExE,sFAAgE;AAEhE,UAAU,CAAC;IACT,uBAAa,CAAC,qBAAqB,EAAE,CAAC;IACtC,uBAAa,CAAC,KAAK,EAAE,CAAC;AACxB,CAAC,CAAC,CAAC;AAEH;IAAA;IAaA,CAAC;IAZQ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C,IAAI,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;iBAC1F;gBACD,OAAO,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YAC5C,CAAC;YACH,cAAC;QAAD,CAAC,AAR2B,CAAc,iCAAe,IAQtD,CAAC;QACJ,uBAAa,CAAC,kBAAkB,CAAC,eAAe,CAAC,CAAC;IACpD,CAAC;IACH,cAAC;AAAD,CAAC,AAbD,IAaC;AAEY,QAAA,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC"}
@@ -1 +1,4 @@
1
1
  require('setimmediate');
2
+ require('reflect-metadata');
3
+ require('./clearGraphs');
4
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-obsidian",
3
- "version": "0.0.18",
3
+ "version": "0.0.21",
4
4
  "description": "Dependency injection framework for React and React Native applications",
5
5
  "scripts": {
6
6
  "prepack": "npm run lint && tsc --project tsconfig.prod.json",
package/src/Obsidian.ts CHANGED
@@ -3,13 +3,14 @@ import { ObjectGraph } from './graph/ObjectGraph';
3
3
  import { Constructable, GraphInternals, ServiceLocator } from './types';
4
4
  import { GraphMiddleware } from './graph/registry/GraphMiddleware';
5
5
  import lazyInjector from './injectors/class/LazyInjector';
6
+ import serviceLocatorFactory from './graph/ServiceLocatorFactory';
6
7
 
7
8
  export default class Obsidian {
8
9
  obtain<T extends ObjectGraph<P>, P = any>(
9
10
  Graph: Constructable<T>,
10
11
  props?: P,
11
12
  ): ServiceLocator<Omit<T, GraphInternals>> {
12
- return graphRegistry.resolve(Graph, props) as unknown as ServiceLocator<T>;
13
+ return serviceLocatorFactory.fromGraph(Graph, props);
13
14
  }
14
15
 
15
16
  inject<T extends object>(target: T, graph?: ObjectGraph) {
@@ -23,4 +24,9 @@ export default class Obsidian {
23
24
  clearGraphMiddlewares() {
24
25
  graphRegistry.clearGraphMiddlewares();
25
26
  }
27
+
28
+ clearGraphs() {
29
+ graphRegistry.clearGraphMiddlewares();
30
+ graphRegistry.clearAll();
31
+ }
26
32
  }
@@ -0,0 +1,15 @@
1
+ import { ObjectGraph } from './ObjectGraph';
2
+ import { Constructable, ServiceLocator as ServiceLocatorType } from '../types';
3
+ import graphRegistry from './registry/GraphRegistry';
4
+
5
+ export default class ServiceLocatorFactory {
6
+ static fromGraph<T extends ObjectGraph<P>, P = any>(Graph: Constructable<T>, props?: P) {
7
+ const resolved = graphRegistry.resolve(Graph, props);
8
+ const wrapped = new Proxy(resolved, {
9
+ get(_target: any, property: string, receiver: any) {
10
+ return () => resolved.retrieve(property, receiver);
11
+ },
12
+ });
13
+ return wrapped as unknown as ServiceLocatorType<T>;
14
+ }
15
+ }
@@ -68,7 +68,7 @@ export class GraphRegistry {
68
68
  this.graphMiddlewares.clear();
69
69
  }
70
70
 
71
- reset() {
71
+ clearAll() {
72
72
  this.instanceToConstructor.clear();
73
73
  this.constructorToInstance.clear();
74
74
  this.nameToInstance.clear();
package/testkit/index.ts CHANGED
@@ -4,11 +4,6 @@ import { GraphMiddleware } from '../src/graph/registry/GraphMiddleware';
4
4
  import { Constructable } from '../src/types';
5
5
  import graphRegistry from '../src/graph/registry/GraphRegistry';
6
6
 
7
- beforeEach(() => {
8
- graphRegistry.clearGraphMiddlewares();
9
- graphRegistry.reset();
10
- });
11
-
12
7
  class TestKit {
13
8
  public mockGraphs(graphNameToGraph: Record<string, Constructable<ObjectGraph> | ((props: any) => ObjectGraph)>) {
14
9
  const graphMiddleware = new class extends GraphMiddleware {
@@ -2,7 +2,8 @@
2
2
  "include": [
3
3
  "src/**/*",
4
4
  "transformers/**/*",
5
- "test/**/*"
5
+ "test/**/*",
6
+ "./clearGraphs.ts"
6
7
  ],
7
8
  "exclude": [
8
9
  "node_modules",
package/jest.setup.ts DELETED
@@ -1 +0,0 @@
1
- import 'reflect-metadata';