react-obsidian 0.0.23 → 0.0.24

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
@@ -32,8 +32,10 @@ React Obsidian is guided by the principles of the Dependency Injection pattern,
32
32
  * [Advance usage](https://github.com/wix-incubator/react-obsidian#advance-usage)
33
33
  * [Accessing props in graphs](https://github.com/wix-incubator/react-obsidian#accessing-props-in-graphs)
34
34
  * [Singleton graphs and providers](https://github.com/wix-incubator/react-obsidian#singleton-graphs-and-providers)
35
+ * [Lazy property injection](https://github.com/wix-incubator/react-obsidian#lazy-property-injection)
36
+ * [Typing graphs](https://github.com/wix-incubator/react-obsidian#typing-graphs)
35
37
  * [Graph middleware](https://github.com/wix-incubator/react-obsidian#graph-middleware)
36
- * [Clear graphs](https://github.com/wix-incubator/react-obsidian#clear-graphs)
38
+ * [Clearing graphs](https://github.com/wix-incubator/react-obsidian#clearing-graphs)
37
39
 
38
40
 
39
41
 
@@ -222,6 +224,63 @@ class ApplicationGraph {
222
224
  }
223
225
  ```
224
226
 
227
+ ### Lazy property injection
228
+ Class properties can be injected lazily by using the `@LazyInject()` decorator. This is useful when the injection should be done in a lifecycle method instead of in the class constructor.
229
+
230
+ ```ts
231
+ @Injectable(ApplicationGraph)
232
+ class Foo {
233
+ @LazyInject() private bar!: Bar;
234
+
235
+ constructor() {
236
+ console.log(bar) // undefined
237
+ Obsidian.inject(this);
238
+ console.log(bar) // Bar {}
239
+ }
240
+ }
241
+ ```
242
+
243
+ In the example above, the dependencies are resolved from the graph declared in the `@Injectable()` decorator. Declaring the graph in advance isn't always desirable. When using the `@LazyInject()` decorator, we can omit the `@Injectable()` decorator and specify the graph to inject from in the call to ``Obsidian.inject()`.
244
+
245
+ ```ts
246
+ class Foo {
247
+ @LazyInject() private bar!: Bar;
248
+
249
+ constructor() {
250
+ console.log(bar) // undefined
251
+ Obsidian.inject(this, () => new ApplicationGraph());
252
+ console.log(bar) // Bar {}
253
+ }
254
+ }
255
+ ```
256
+
257
+ ### Typing graphs
258
+ Obsidian exposes a utility `type` that represents the dependencies provided by a graph. This type is useful when typing props passed to components or hooks. It's especially useful when you need to differentiate between "own" props and injected props.
259
+
260
+ ```ts
261
+ type InjectedDependencies = DependenciesOf<ApplicationGraph> // { biLogger: BiLogger, httpClient: HttpClient }
262
+
263
+ // These are props our hook requires from the calling scope
264
+ interface OwnProps {
265
+ someArgs: string
266
+ }
267
+
268
+ // This interface represents the object returned from the hook
269
+ interface Result {
270
+ //
271
+ }
272
+
273
+ // Our hook requires two dependencies:
274
+ // 1. someArg - passed down from the calling scope
275
+ // 2. biLogger - injected from the ApplicationGraph
276
+ const hook = ({someArg, biLogger}: OwnProps & InjectedDependencies): Result => {
277
+ // ...
278
+ }
279
+
280
+ // When invoking the hook, it must be provided with `someArg` since OwnProps aren't optional
281
+ const injectedHook: (props: OwnProps & Partial<InjectedDependencies>) => Result = injectHookWithArguments<InjectedDependencies, OwnProps, Result>(hook, MainApplication);
282
+ ```
283
+
225
284
  ### Graph middleware
226
285
  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).
227
286
 
@@ -249,7 +308,7 @@ const loggingMiddleware = new class extends GraphMiddleware {
249
308
  Obsidian.addGraphMiddleware(loggingMiddleware);
250
309
  ```
251
310
 
252
- ### Clear graphs
311
+ ### Clearing graphs
253
312
  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.
254
313
 
255
314
  #### Clearing graphs automatically during execution of Jest tests
@@ -1,5 +1,5 @@
1
1
  import { ObjectGraph } from '../../graph/ObjectGraph';
2
2
  import { Constructable } from '../../types';
3
- export declare function injectHookWithArguments<Result, Injected, Args>(hook: (args: Injected & Args) => Result, Graph: Constructable<ObjectGraph>): (props: Args & Partial<Injected>) => Result;
4
- export declare function injectHook<Result, Injected>(hook: (args: Injected) => Result, Graph: Constructable<ObjectGraph>): (props?: Partial<Injected>) => Result;
3
+ export declare function injectHookWithArguments<Injected, Own, Result = {}>(hook: (args: Injected & Own) => Result, Graph: Constructable<ObjectGraph>): (props: Own & Partial<Injected>) => Result;
4
+ export declare function injectHook<Injected, Result = {}>(hook: (args: Injected) => Result, Graph: Constructable<ObjectGraph>): (props?: Partial<Injected>) => Result;
5
5
  //# sourceMappingURL=InjectHook.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"InjectHook.d.ts","sourceRoot":"","sources":["../../../../src/injectors/hooks/InjectHook.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAU5C,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAC5D,IAAI,EAAE,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,KAAK,MAAM,EACvC,KAAK,EAAE,aAAa,CAAC,WAAW,CAAC,GAChC,CAAC,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,MAAM,CAE7C;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,QAAQ,EACzC,IAAI,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,MAAM,EAChC,KAAK,EAAE,aAAa,CAAC,WAAW,CAAC,GAChC,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,KAAK,MAAM,CAEvC"}
1
+ {"version":3,"file":"InjectHook.d.ts","sourceRoot":"","sources":["../../../../src/injectors/hooks/InjectHook.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAU5C,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,EAAE,EAChE,IAAI,EAAE,CAAC,IAAI,EAAE,QAAQ,GAAG,GAAG,KAAK,MAAM,EACtC,KAAK,EAAE,aAAa,CAAC,WAAW,CAAC,GAChC,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,MAAM,CAE5C;AAED,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,EAAE,EAC9C,IAAI,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,MAAM,EAChC,KAAK,EAAE,aAAa,CAAC,WAAW,CAAC,GAChC,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,KAAK,MAAM,CAEvC"}
@@ -1 +1 @@
1
- {"version":3,"file":"InjectHook.js","sourceRoot":"","sources":["../../../../src/injectors/hooks/InjectHook.ts"],"names":[],"mappings":";;;;;;AAGA,gEAA0C;AAE1C,IAAM,YAAY,GAAG,IAAI,sBAAY,EAAE,CAAC;AAExC,6HAA6H;AAC7H,0FAA0F;AAC1F,uHAAuH;AACvH,yEAAyE;AAEzE,SAAgB,uBAAuB,CACrC,IAAuC,EACvC,KAAiC;IAEjC,OAAO,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC1C,CAAC;AALD,0DAKC;AAED,SAAgB,UAAU,CACxB,IAAgC,EAChC,KAAiC;IAEjC,OAAO,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC1C,CAAC;AALD,gCAKC"}
1
+ {"version":3,"file":"InjectHook.js","sourceRoot":"","sources":["../../../../src/injectors/hooks/InjectHook.ts"],"names":[],"mappings":";;;;;;AAGA,gEAA0C;AAE1C,IAAM,YAAY,GAAG,IAAI,sBAAY,EAAE,CAAC;AAExC,6HAA6H;AAC7H,0FAA0F;AAC1F,uHAAuH;AACvH,yEAAyE;AAEzE,SAAgB,uBAAuB,CACrC,IAAsC,EACtC,KAAiC;IAEjC,OAAO,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC1C,CAAC;AALD,0DAKC;AAED,SAAgB,UAAU,CACxB,IAAgC,EAChC,KAAiC;IAEjC,OAAO,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC1C,CAAC;AALD,gCAKC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-obsidian",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
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",
@@ -10,14 +10,14 @@ const hookInjector = new HookInjector();
10
10
  // 1. injectHookWithArguments: Should be used when a hook requires parameters in addition to the injected dependencies.
11
11
  // 2. injectHook: Should be used when a hook does not require parameters.
12
12
 
13
- export function injectHookWithArguments<Result, Injected, Args>(
14
- hook: (args: Injected & Args) => Result,
13
+ export function injectHookWithArguments<Injected, Own, Result = {}>(
14
+ hook: (args: Injected & Own) => Result,
15
15
  Graph: Constructable<ObjectGraph>,
16
- ): (props: Args & Partial<Injected>) => Result {
16
+ ): (props: Own & Partial<Injected>) => Result {
17
17
  return hookInjector.inject(hook, Graph);
18
18
  }
19
19
 
20
- export function injectHook<Result, Injected>(
20
+ export function injectHook<Injected, Result = {}>(
21
21
  hook: (args: Injected) => Result,
22
22
  Graph: Constructable<ObjectGraph>,
23
23
  ): (props?: Partial<Injected>) => Result {